QuiverSphere QUIVERSPHERE SUBSCRIBE
QuiverSphere
← Blog

Creating self-hosted HTTP tunnels using SSH and nginx

Learn how to set up self-hosted HTTP tunnels with SSH and nginx for secure and private connections.

16 October 2026 · 7 min read
Creating self-hosted HTTP tunnels using SSH and nginx

When collaborating digitally, there are moments when a project needs to be shared without exposing all its inner workings. Consider a scenario where you want to proofread a work-in-progress blog post, but the preview is only accessible through localhost:8080. While several commercial solutions exist, such as ngrok and Cloudflare Quick Tunnels, there are also self-hosted alternatives that provide functionality without the associated costs or dependencies on security-incident/">third-party services. In this article, we will explore how to create a self-hosted HTTP tunnel using OpenSSH and nginx, providing a secure, private way to share your projects.

Understanding HTTP tunneling

HTTP tunneling involves creating a pathway through which web traffic can be forwarded from one server to another. Essentially, this setup enables users to expose a local service running on their local machine to the internet through a remote server. Self-hosting an HTTP tunnel offers greater control and minimizes potential security risks compared to using commercial solutions.

A primary aim of HTTP tunneling is to provide secure access to local resources, making it a common practice among developers, testers, and remote teams. By using SSH for this purpose, one can leverage stronger investigation-into-ai-security-breaches-following-hugging-face-incident/">security protocols, ensuring that the exposed service remains confined and accessible only to authorized users.

Prerequisites for setting up the tunnel

Before diving into the implementation details, certain prerequisites must be fulfilled. Here's what you need:

  • An SSH server (preferably OpenSSH) installed and running on a remote server.
  • nginx installed on the remote server to handle incoming HTTP requests and proxy them to the local service.
  • Preference for a secure secret key to protect access to the service.
  • Basic knowledge of command-line operations to work with the SSH client and server.

Once the necessary software is in place, you can proceed with the actual setup of the HTTP tunnel.

Setting up the SSH tunnel

The first step in creating a self-hosted HTTP tunnel involves forwarding connections from the remote server to your local service. This can be done with a straightforward SSH command that utilizes the -R flag, which stands for remote forwarding. Here’s a basic example of how to set this up:

$ ssh -R 0:localhost:8080 your_remote_server

The command shown above instructs the SSH client to allocate a random free port on the remote server and forward traffic to your local service listening on localhost:8080. The server assigns an available port, allowing you to connect without specifying a predetermined port number.

Upon executing this command, you will see a confirmation indicating which port has been allocated for remote forwarding. For instance:

Allocated port 41535 for remote forward to localhost:8080

This feedback provides the necessary information to configure nginx properly, as the next step involves setting up the server to proxy requests to the allocated port.

Configuring nginx for HTTP tunneling

Once the SSH tunnel is established, it’s time to configure nginx to act as a reverse proxy for the connections reaching the designated port. The following nginx configuration snippet illustrates how to set this up:

server {
    listen 80;
    server_name *.ssh.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:41535;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

This nginx configuration allows the server to listen to incoming requests on port 80 and forwards them to the local service through the randomly assigned port, providing a simple yet effective solution for HTTP tunneling.

Securing the tunnel with authentication

To safeguard the exposed service further, it is prudent to introduce authentication mechanisms. The SSH tunnel inherently provides a layer of security, but we will add HTTP basic authentication for an additional protective barrier. The HTTP basic authentication approach requires users to present valid credentials before accessing the service.

The username and associated secret key should ensure that only authorized users can access the content served through the tunnel. Here’s how to generate the username with an expiration token:

$ expires=$(( $(date +%s) + 86400 ))
$ port=41535
$ secret='YourSecret!'  
$ token=$(printf '%s %s %s' "$expires" "$port" "$secret" | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)
https://$token--$expires@p$port.ssh.yourdomain.com/

In this setup, the request includes a token that consists of an expiration timestamp and other parameters. This token will be used by clients to authenticate themselves, permitting access to the tunnel. The expiration ensures that links do not remain active indefinitely, which could pose security risks if shared unknowingly.

Creating a helper script to simplify usage

To streamline the tunneling process, creating a helper script is recommended. This will eliminate the need to remember complex commands and facilitate easier usage. Here is an outline of what such a script could include, focusing on key functionalities:

lifetime=86400
secret='YourSecret!'
expires=$(( $(date +%s) + lifetime ))
for port in $ports; do
    token=$(printf '%s %s %s' "$expires" "$port" "$secret" | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)
    echo "https://$token--$expires@p$port.ssh.yourdomain.com/"
done
sleep infinity

This script calculates the expiration timestamp and dynamically generates the URLs based on available ports. Users can simply run this script, and it will output the URLs needed to access the tunneled service, significantly simplifying the workflow.

Additionally, you can add this script to your ~/.ssh/config file for quick access. Example:

Host http-tunnel
    Hostname your_remote_server
    RemoteCommand your_helper_script
    ControlPath none

Now, executing the command to create the tunnel is as easy as typing ssh http-tunnel in your terminal.

Exploring the advantages of self-hosting

By opting for a self-hosted HTTP tunnel, users gain numerous advantages. First and foremost, enhanced privacy plays a significant role, as data does not transit through external servers. Additionally, self-hosting fosters knowledge and understanding of networking and server management, enhancing your technical skills over time.

With self-hosting, you also rely solely on OpenSSH and nginx, eliminating the dependency on third-party services that may come with hidden costs or service restrictions. This flexibility empowers users to maintain control over their projects and how they share access.

While initial setups may require some technical expertise, the long-term benefits of having a secure, private tunneling solution can significantly outweigh the hassle. As with any setup, thorough testing and careful configuration ensure seamless access to services without exposing them to unnecessary risks.

Looking ahead in HTTP tunneling

With the rapid evolution of technology and networking, self-hosted solutions like HTTP tunneling will continue to gain traction among developers, teams, and organizations that prioritize security and privacy. OpenSSH and nginx provide robust, reliable platforms for tackling the needs of interconnecting services, especially in remote and collaboration-focused environments.

As we advance into an era where secure connections are paramount, understanding and implementing tools like SSH and nginx will be essential. This knowledge not only reinforces the capability to manage secure tunnels but also nurtures a mindset geared towards innovation and self-sufficiency in digital collaboration.

Frequently asked questions

What is an HTTP tunnel?

An HTTP tunnel is a way to forward web traffic from one server to another, allowing remote access to local services.

How does SSH improve the security of HTTP tunneling?

SSH uses encryption and secure protocols to protect data in transit, minimizing the risk of interception and unauthorized access.

Can I use other services besides OpenSSH and nginx for tunneling?

Yes, while OpenSSH and nginx are robust options, there are other tools available such as frp and localtunnel, but they may come with different challenges and requirements.