My search engine project hosted its own git server, a Gitea instance on one of its four machines. Until July, the only way to reach it was an SSH tunnel I started by hand in a terminal, which disappeared whenever that terminal did. In July, it got a proper public address, with HTTPS from Caddy and SSH for clone and push. The setup took an afternoon; the details worth writing down are the two protocols taking different paths, and the small mistakes in the last mile.
The starting point
Gitea listened only on the machine's loopback interface: the web interface on port 3000 and its built-in SSH server on port 2222. Nothing outside that machine could reach either. The project's public frontend, on a different machine, already ran Caddy for HTTPS and already used small tunnel containers to reach services on other machines: one for MySQL, one for OpenSearch.
One more tunnel container
The fix added a third: a gitea-tunnel container running autossh on the frontend machine, forwarding both of Gitea's ports from the other machine. It got its own SSH key, following the project's rule of one key per cross-machine link, so revoking one link never breaks another. Being a container managed by Docker Compose, it restarts on its own, which is exactly what the hand-started tunnel couldn't do.
HTTP through Caddy, SSH straight through
The two forwarded ports then take different routes:
- HTTP (3000) is never published on the host. Caddy reaches it over the Docker network as
gitea-tunnel:3000, with a new site block and its own Let's Encrypt certificate. Visitors only ever talk to Caddy. - SSH (2222) is published on the host directly, as
2222:2222. Git over SSH is a raw TCP protocol, and Caddy's reverse proxy handles HTTP; raw TCP needs a separate, experimental layer-4 app for Caddy, which this setup didn't use.
Gitea also needed to know its new public name. Its DOMAIN and ROOT_URL settings were updated, so the clone URLs it shows in its web interface are the real ones and not localhost addresses. SSH_PORT stayed at 2222, because the tunnel forwards that exact port end to end.
The firewall step that wasn't needed
The plan listed two steps only a person with root could do: point the DNS name at the frontend machine, and open port 2222 in its ufw firewall. The DNS record was needed: until it pointed at the right machine, Caddy kept retrying the certificate request every minute and failing.
The firewall step turned out to be unnecessary: port 2222 was reachable from outside without anyone changing ufw. The note at the time said either it was already allowed or ufw wasn't gating it. By Docker's own documentation, ufw couldn't have been gating it either way. Docker's documentation says published ports are routed in the nat table before packets reach the chains ufw uses, so ufw never sees them. The same project's deploy notes had a warning to exactly that effect, and I wrote up what that meant for its monitoring ports separately. A port published by a container is open unless something in Docker's own chain filters it.
Two bugs in one clone URL
The last step was pointing my local repository at the new remote. The first attempt was:
gitea@HOST:2222/lars/search-engine.git
That has two problems. Gitea's SSH user for git operations is git, not gitea; ssh -T against the server greets you by your account name when the user is right. And the scp-like form, user@host:path, has no place for a port. Git's documentation lists ssh://[user@]host[:port]/path as the form that takes a port, and [user@]host:/path as the scp-like alternative, without one. In the scp-like form, 2222 is just the beginning of the path, and the connection goes to port 22. The working remote was:
ssh://git@HOST:2222/lars/search-engine.git
With that, git ls-remote and git push both worked over SSH.
What generalises
For any self-hosted service behind a reverse proxy: send HTTP through the proxy and keep the backend port unpublished; give other protocols their own published port, and know that on a Docker host, ufw won't be what decides whether that port is open. And when a git remote uses a non-standard SSH port, use the ssh:// URL form; the shorter form can't express it.
Leave a comment
Comments are reviewed before they appear.