Self-hosted HTTP tunnels with SSH and Nginx
Article URL: https://vincent.bernat.ch/en/blog/2026-http-over-ssh Comments URL: https://news.ycombinator.com/item?id=49958569 Points: 165 # Comments: 34
To create a self-hosted HTTP tunnel using SSH and Nginx, a friend can provide feedback on a work-in-progress blog post. There are various tools available for this purpose, including commercial services like ngrok and Cloudflare Quick Tunnels, as well as self-hostable options such as frp and localtunnel. However, some solutions require a specific client and rely on a specific SSH server, like sish.
In this case, the focus is on implementing a self-hosted solution using only OpenSSH and nginx. The process begins by forwarding connections from a port on a remote server to a local service. When specifying 0 as the remote port, the server assigns a free port. The nginx server is then configured to proxy requests from https://p41535.ssh.luffy.cx to http://127.0.0.1:41535.
To ensure confidentiality, a secret is used as a "key" that keeps the content secure. This secret is used in the ngx_http_secure_link_module, which computes a hash based on a set of values, including the secret. This hash is then included in the URL as a username, along with an expiration timestamp. The client sends this username to the server using HTTP basic authentication.
Nginx exposes the username in the $remote_user variable, allowing the secure_link module to extract the hash and expiration timestamp. The module expects the hash and timestamp to be separated by a comma in the $remote_user variable. The module is provided with the string to hash, which includes the expiration timestamp, port, and a secret. If the hash is incorrect or missing, a 401 error is returned, and if the link has expired, a 410 error is returned.
To remove the Authorization header before forwarding the request, a few directives are added for proxying WebSocket connections. The complete configuration is provided, and a helper script is written to automate the process. The script determines the ephemeral port allocated by OpenSSH, as it is not present in environment variables.
After setting up the server, a short command can be used to generate a self-hosted tunnel and obtain a URL to share. This solution relies solely on OpenSSH and nginx, which are already running on the server. The helper script is installed on the server, and an entry is added to the ~/.ssh/config file. With this setup, a self-hosted tunnel and a URL can be created with just one command.
Written by urgent.news from Hacker News Best's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.
- Self-hosted HTTP tunnels with SSH and Nginx vincent.bernat.ch