The whole setup is a web server that listens only on 127.0.0.1 and a tor daemon that forwards onion traffic to it. Nginx never touches the internet directly. Getting a page to load takes about ten minutes. Getting it to load without quietly revealing where the box lives takes a little more care, and that's mostly what this walkthrough is about.

I'm assuming Debian 12 or a recent Ubuntu, root or sudo, and a fresh VPS or a machine at home. Commands are the same on either.

Install tor from the Tor Project's repository

Distribution packages of tor tend to lag. For an onion service that's more than a minor annoyance, because the onion-service DoS defences have improved a lot in recent releases. The Tor Project runs its own apt repository; the current instructions live at support.torproject.org/apt. After adding it:

sudo apt update
sudo apt install tor deb.torproject.org-keyring nginx

Check the version with tor --version. Anything older than 0.4.8 lacks proof-of-work defences, which matters if your site ever gets popular or annoys someone.

Bind Nginx to localhost only

This is the step people skip. The default Debian Nginx site listens on port 80 on every interface, which means your onion content may also be reachable on the server's public IP. Internet-wide scanners crawl every IPv4 address constantly, and if your onion page shows up at a public IP, matching the two is trivial.

Remove the default site and write your own:

sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-available/onion
server {
    listen 127.0.0.1:8080;
    server_name _;

    root /var/www/onion;
    index index.html;

    server_tokens off;
    access_log off;
    error_log /var/log/nginx/onion-error.log crit;

    add_header X-Content-Type-Options nosniff;
    add_header Referrer-Policy no-referrer;

    location / {
        try_files $uri $uri/ =404;
    }
}
sudo ln -s /etc/nginx/sites-available/onion /etc/nginx/sites-enabled/
sudo mkdir -p /var/www/onion
echo '<h1>It works</h1>' | sudo tee /var/www/onion/index.html
sudo nginx -t && sudo systemctl reload nginx

A few choices there deserve explanation. server_tokens off stops Nginx advertising its exact version in headers and error pages. Turning off the access log is a judgment call: every request arrives from 127.0.0.1 anyway, so the logs tell you nothing about visitors, but they're a record of what was requested and when, which you might not want sitting on disk. Keep the error log, at a low level, so you can debug.

Then confirm nothing is listening publicly:

sudo ss -tlnp | grep nginx

You want to see 127.0.0.1:8080 and nothing on 0.0.0.0:80 or [::]:80. Also check other files under /etc/nginx/conf.d/, since some packages drop their own server blocks in there.

Configure the onion service

Edit /etc/tor/torrc and add:

HiddenServiceDir /var/lib/tor/onion-site/
HiddenServicePort 80 127.0.0.1:8080

HiddenServicePort takes the virtual port (what visitors connect to on the onion address) and a target. Visitors type http://youraddress.onion, which means port 80, and tor hands the connection to Nginx on 8080. You can list several HiddenServicePort lines under one directory if you need more ports. For the full list of options, the torrc man page is the reference, and we cover the useful ones in The torrc File: Options Worth Knowing.

Restart tor and read the address:

sudo systemctl restart tor
sudo cat /var/lib/tor/onion-site/hostname

On Debian, the actual daemon runs under the tor@default unit, and logs go to the journal: journalctl -u tor@default -f. If tor complains about permissions, the directory must be owned by debian-tor and mode 700. Tor creates it correctly itself, so permission errors usually mean someone created the directory by hand first.

Open the address in Tor Browser. The first load might take ten or twenty seconds while the descriptor propagates to the HSDirs. After that it should be quick.

Optional: a Unix socket instead of a TCP port

Pointing tor at a Unix socket means there's no TCP port at all, not even on localhost. Other local processes can't stumble onto it, and you avoid a class of mistakes where some proxy or app on the same box treats 127.0.0.1 traffic as trusted. In torrc:

HiddenServicePort 80 unix:/var/run/tor-onion/nginx.sock

And in Nginx, listen unix:/var/run/tor-onion/nginx.sock;. The catch is permissions: the tor user needs to be able to connect to the socket, and on Debian and Ubuntu tor runs under an AppArmor profile that restricts which paths it can touch. If tor logs a permission error on the socket, check /var/log/syslog or the journal for AppArmor denials before blaming Nginx. It's more fiddly than TCP, and TCP on 127.0.0.1 is fine for most people.

Back up the keys

Everything that makes your address yours lives in hs_ed25519_secret_key inside the service directory. Copy the whole directory somewhere safe and encrypted:

sudo tar czf onion-keys.tgz -C /var/lib/tor onion-site
gpg -c onion-keys.tgz && shred -u onion-keys.tgz

Lose the key and the address is gone for good. Leak it and someone else can impersonate you. There is no third option.

Things that will give you away

The onion address protects the transport. It does nothing about what your application says. A few classics:

Absolute links and assets pointing at a clearnet domain. If your HTML loads a font from Google or a script from a CDN, Tor Browser will fetch it over an exit, which is mostly a privacy issue for visitors, but if those URLs contain your own clearnet domain you've just published the connection between the two.

Outbound requests from the server. Contact forms that send email, image proxies, link previews, webhooks, software update checks: all of these go out over the server's real connection unless you route them through tor. A visitor who can make your server fetch a URL they control gets your IP.

Error pages and default content. A stock "Welcome to nginx" page, a PHP error with a full file path, a status page. Nginx doesn't ship an exposed status page by default, but Apache's mod_status famously did, and it unmasked real onion sites more than once.

There's a longer list with examples in How Onion Services Leak Their Real IP. If you're running something where the server location genuinely must stay secret, read it, and consider running the web server in a separate VM or container whose only network route is through tor.

A note on HTTPS

You don't need TLS for an onion site. The connection is already end-to-end encrypted and authenticated by the address itself, and Tor Browser treats http:// onion pages as secure. There are cases where a certificate still makes sense, which we cover in HTTPS on .onion. For a first site, plain HTTP on port 80 is the normal setup.

If you're running a public site and want clearnet visitors to find the onion version, the next step is adding an Onion-Location header on the clearnet side. If it ever comes under load, look at proof-of-work defences before you look at anything else.

#nginx #onion-site #hosting #torrc #debian