How to Serve a Small Website as a Tor Onion Service Beside HTTPS
Adding a .onion address to an existing website takes two lines of Tor config. The fiddly part is everything around them: a socket that Tor is allowed to open, a web server that doesn't bounce visitors back to the public site, and a key you really don't want to lose.
This guide assumes Debian or Ubuntu, nginx, and a static site already working over HTTPS at example.com. The public site stays exactly where it is.
Know what you are getting
This setup gives your visitors privacy but gives you no anonymity. Tor users can reach the site without leaving the Tor network through an exit relay, and without relying on DNS or a certificate authority to find it.
Your server is still publicly known, because it serves the same pages at example.com and you are about to advertise the link between the two. If you need to hide who runs a site or where it is, this is the wrong guide.
Install Tor and make a home for the socket
Install the tor package. The Tor Project runs its own apt repository and advises against using Ubuntu's universe package.
Tor will pass visitors to nginx over a Unix socket. A loopback port such as 127.0.0.1:8080 also works, but any local process can connect to it. With a socket, directory permissions decide who gets in.
Don't put it in /run/tor, tempting as that is. That directory belongs to the Tor service, which sets it up itself. Define a separate one in /etc/tmpfiles.d/onion-site.conf, so it comes back after a reboot:
d /run/onion-site 0750 root debian-tor -
sudo systemd-tmpfiles --create /etc/tmpfiles.d/onion-site.conf
The nginx master process runs as root and can create the socket there. Tor, running as debian-tor, can pass through the directory. Other unprivileged users cannot.
Tell Tor about the service
Add these lines to /etc/tor/torrc:
HiddenServiceDir /var/lib/tor/my-site/
HiddenServicePort 80 unix:/run/onion-site/http.sock
The 80 is the port visitors ask for on the onion address. The rest is where Tor sends them. You don't need an inbound firewall rule or port forward, because Tor only makes outbound connections.
Restart Tor and read your new address:
sudo systemctl restart tor
sudo cat /var/lib/tor/my-site/hostname
If the file isn't there, check journalctl -u tor@default. On Debian that unit is the real daemon; tor.service is a wrapper around it.
Quick detour, because this bit is lovely: the 56-character address is the service's public key, plus a checksum and version number, in base32. There's no registrar and nobody to ask. Tor Browser can check it is talking to the holder of the matching private key using nothing but the name. The downside is that no human will ever remember it.
Give nginx a separate server block
Create a new site file for the onion, using the address from hostname:
server {
listen unix:/run/onion-site/http.sock;
server_name YOURADDRESS.onion;
root /var/www/example;
index index.html;
server_tokens off;
absolute_redirect off;
access_log off;
}
Enable it, then test and reload with sudo nginx -t && sudo systemctl reload nginx.
Here is what the last three directives do:
server_tokens offstops nginx advertising its version.absolute_redirect offkeeps nginx's own redirects relative, such as the one that adds a trailing slash to a directory.access_log offis my preference for a small static site. Every request arrives from the local socket, so there is no client address to learn anyway. Error logs still record failing requests.
Do not copy across your HTTPS redirect or Strict-Transport-Security header. This listener speaks plain HTTP inside Tor's encrypted, authenticated connection. The Tor Project's HTTPS guidance lists cases where a certificate still helps, but a small static site usually isn't one.
Let AppArmor open the socket
Correct file permissions are not enough if AppArmor is confining Tor. Debian's system_tor profile has no rule for a socket you invented five minutes ago.
Add this line to /etc/apparmor.d/local/system_tor:
/{,run/}onion-site/http.sock rw,
Then reload the profile:
sudo apparmor_parser -r /etc/apparmor.d/system_tor
The odd braces match the path with or without /run, since the service's mount namespace can affect what AppArmor sees. If connections still fail, run sudo journalctl -k | grep DENIED and compare the name= field with your rule.
Test it through Tor, not around it
The Tor daemon normally offers a SOCKS proxy on port 9050, so you can test from the server itself:
curl -I --socks5-hostname 127.0.0.1:9050 http://YOURADDRESS.onion/
You want a 200. The --socks5-hostname option matters because it hands the name to Tor to resolve, and ordinary DNS knows nothing about .onion. A new service can take a little while to become reachable, so wait a minute before dismantling your config.
Then open it in Tor Browser and click around, because curl won't notice a stylesheet that loads from the wrong place.
Hunt down links that leave the onion
A page can load from your onion address and then send the visitor straight to the public site on the first click. Search the built files:
grep -rnE '(https?:)?//(www\.)?example\.com' /var/www/example
Check what turns up:
- Navigation links, images, scripts and stylesheets should use paths such as
/css/site.css. - Canonical URLs and feed entries can reasonably keep the public name.
- Third-party fonts, analytics and embeds are worth removing, since they bring other parties into a visit that was meant to be private.
- Error pages and response headers shouldn't reveal internal hostnames, paths or software details.
Advertise it from the HTTPS site
The Onion-Location header goes on the public HTTPS site, not the onion one. Add this to the existing port 443 server block:
add_header Onion-Location http://YOURADDRESS.onion$request_uri;
Tor Browser then shows an ".onion available" prompt in the address bar. Including $request_uri means the visitor lands on the matching page instead of the home page.
There's an nginx trap here. If a location block has any add_header of its own, it normally stops inheriting the ones from the server block. Your header can work on / and silently vanish elsewhere, so check a few paths with curl -I.
Hang on, why insist on HTTPS for this header? Picture the attack: on plain HTTP, anyone able to alter the response could advertise their onion address. HTTPS ties the recommendation to whoever holds the certificate for your domain.
Back up the key before you publish the address
Losing hs_ed25519_secret_key means losing the address for good. Anyone who copies it can impersonate your service. It lives in /var/lib/tor/my-site/.
Back up that whole directory, encrypted, and treat it like an SSH or TLS private key. To move servers, restore it with ownership by debian-tor and mode 0700 before starting Tor with the service configured. Otherwise you may find yourself the proud owner of a brand-new address.
Leave the extra knobs alone until needed
Tor has a proof-of-work defence for services being flooded with connection attempts. It is off by default for each service. First run tor --list-modules and look for pow: yes, then add this below your HiddenServicePort line:
HiddenServicePoWDefensesEnabled 1
It makes clients spend computation to get priority when the service is busy. It won't cure server overload in general. The DoS guidance covers the build requirements and other options.
You may also see HiddenServiceSingleHopMode, which shortens the service's side of the path by giving up server anonymity. That sounds suitable here, but the torrc manual marks it experimental, requires further non-anonymous settings and disables client use of that Tor instance. For a small site, I'd stick with the default.
Finally, reboot once at a convenient moment and repeat the curl test. This proves that the socket directory, AppArmor rule, nginx and Tor all come back without you, which a test on the afternoon you typed them in can't tell you.