Most self-hosted services fail in the same way. Someone installs a dashboard, photo library, or wiki, forwards a port on the router so they can reach it from work, and then forgets about it. Within days, automated scanners are probing the login page. The safer pattern is to keep the application off the public internet entirely, reachable only through an encrypted tunnel that authenticates each device before it can send a single packet to the service. This guide shows how to build that pattern with WireGuard, a tightly scoped firewall, and a hardened nginx reverse proxy. It is written for home-lab operators and small-team admins who are comfortable with a Linux shell and a text editor.
Before you start
- A Linux host (Debian, Ubuntu, or similar) that will run the tunnel endpoint, the reverse proxy, and the application. A small VPS or a home server both work.
- Root or sudo access on that host, and on each client device you plan to use.
- A public IP address or a dynamic DNS name for the host, referred to below as YOUR_DOMAIN. Replace every occurrence with your real hostname.
- The ability to forward or open one UDP port (51820) on any upstream firewall or router.
- A working application listening on a local port. This guide uses 127.0.0.1:8080 as an example.
- WireGuard tools (
wireguard-tools), nginx, and OpenSSL installed on the host.
1. Plan the addresses and the trust boundary
Before touching any config, decide what is allowed to reach what. A simple and effective plan uses a private tunnel subnet, such as 10.8.0.0/24, where the server is 10.8.0.1 and each client gets its own address. Only the server’s tunnel address will accept HTTPS. The application itself stays bound to the loopback interface, so even a misconfigured proxy cannot expose it directly.
Write the plan down as a short table: device name, tunnel address, what it may reach, and who owns it. This takes five minutes and prevents most later confusion about why a rule exists.
2. Generate keys and configure the WireGuard server
WireGuard authenticates peers by public key rather than by passwords or certificates. Each machine needs its own key pair. Generate the server keys with restrictive permissions:
sudo mkdir -p /etc/wireguard
cd /etc/wireguard
umask 077
wg genkey | sudo tee server.key | wg pubkey | sudo tee server.pub
Create /etc/wireguard/wg0.conf on the server. The ListenPort is the only port you will open to the internet, and WireGuard silently drops any packet that does not come from a known key, so unauthenticated scanners get no response at all.
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
[Peer]
# laptop-yourname
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32
Replace SERVER_PRIVATE_KEY with the contents of server.key. Notice that each peer’s AllowedIPs is a single /32 address. That value controls both routing and source filtering: WireGuard drops any packet from that peer whose source address does not match. Keeping it narrow means a compromised client cannot impersonate another device on the tunnel.
Bring the interface up and enable it at boot:
sudo chmod 600 /etc/wireguard/wg0.conf
sudo systemctl enable --now wg-quick@wg0
sudo wg show
If this host only terminates connections for its own services, you do not need IP forwarding. Only enable net.ipv4.ip_forward if you deliberately want peers to route traffic through the server to other networks.
3. Configure each client with minimal routes
On each client device, generate a key pair the same way, then add the client as a peer on the server with its public key. On the client, create its WireGuard configuration:
[Interface]
Address = 10.8.0.2/32
PrivateKey = CLIENT_PRIVATE_KEY
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = YOUR_DOMAIN:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25
AllowedIPs = 10.8.0.0/24 sends only tunnel traffic through the VPN, so ordinary browsing still uses the normal connection. This is called split tunneling, and it is usually what you want for a service-access tunnel. If you want all traffic routed through the server, use 0.0.0.0/0, but understand that you are then trusting the server with your traffic.
PersistentKeepalive = 25 keeps NAT mappings alive on home routers and mobile networks. Without it, connections from behind NAT often go quiet and stop working.
4. Lock down the host firewall
The firewall is what turns a tunnel into a boundary. The goal is a default-deny inbound policy that allows only loopback traffic, established connections, WireGuard’s UDP port, and HTTPS arriving over the tunnel interface. Here is an nftables ruleset, saved as /etc/nftables.conf:
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
iif lo accept
meta l4proto icmp accept
udp dport 51820 accept
iifname wg0 tcp dport 443 accept
iifname wg0 tcp dport 22 accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
Load and persist it:
sudo nft -c -f /etc/nftables.conf
sudo nft -f /etc/nftables.conf
sudo systemctl enable nftables
Note what is absent. There is no rule for port 80, no rule for port 443 from the public interface, and no rule for port 22 from the internet. SSH is reachable only over the tunnel. If you already run ufw or firewalld, pick one firewall manager and make sure it does not conflict with these rules. Two competing managers is a common source of accidental exposure.
5. Bind the application to loopback only
Check where your application is listening. Many apps default to 0.0.0.0, which means every interface, including the public one. Change the listen address to 127.0.0.1 in the application’s own configuration, then confirm:
sudo ss -tlnp | grep 8080
The local address column should read 127.0.0.1:8080. If it reads 0.0.0.0:8080 or *:8080, fix the app before continuing. The reverse proxy will be the only component that talks to it.
6. Create TLS certificates for the tunnel address
Traffic inside WireGuard is already encrypted, but HTTPS gives you certificate-based identity, protection for any hop you did not design, and compatibility with browsers and tools that expect TLS. For a service that is only reachable over the tunnel, a private certificate authority is simple and avoids any public exposure. Create the CA and a server certificate that covers both the internal hostname and the tunnel IP:
cd /etc/nginx/tls
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -days 3650 -nodes -keyout ca.key -out ca.crt -subj '/CN=YOUR_DOMAIN internal CA'
openssl req -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -keyout app.key -out app.csr -subj '/CN=app.YOUR_DOMAIN'
openssl x509 -req -in app.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 825 -copy_extensions copyall -out app.crt -extfile <(echo subjectAltName=DNS:app.YOUR_DOMAIN,IP:10.8.0.1)
sudo chmod 600 ca.key app.key
Keep ca.key offline or in a password manager if you can. Install ca.crt on each client device so the browser trusts the certificate. If you already own a public domain, you can instead issue a real certificate using the DNS-01 challenge with your ACME client, pointing an A record at the tunnel address. Either approach keeps the service unreachable from the internet.
7. Harden the reverse proxy
Create /etc/nginx/sites-available/app.conf. This configuration listens only on the tunnel address, enforces modern TLS, sets security headers, restricts access by source address, and logs requests:
server_tokens off;
client_max_body_size 10m;
server {
listen 10.8.0.1:443 ssl;
server_name app.YOUR_DOMAIN;
ssl_certificate /etc/nginx/tls/app.crt;
ssl_certificate_key /etc/nginx/tls/app.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_tickets off;
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains' always;
add_header X-Content-Type-Options nosniff always;
add_header X-Frame-Options DENY always;
add_header Referrer-Policy no-referrer always;
add_header Content-Security-Policy "default-src 'self'" always;
allow 10.8.0.0/24;
deny all;
access_log /var/log/nginx/app.access.log;
error_log /var/log/nginx/app.error.log warn;
location / {
proxy_pass http://127.0.0.1:8080;
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;
}
}
Enable and test it:
sudo ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
A few details matter here. The always keyword ensures headers are sent on error responses as well as successful ones. The Content-Security-Policy shown is a conservative starting point, and some applications will need adjustment, so test it in a browser's developer console. Finally, X-Forwarded-For is only trustworthy if the proxy is the only path to the app, which the loopback binding guarantees.
8. Add a second access control layer (optional but recommended)
The WireGuard key and the nginx source-address check both gate access, but a stolen laptop with an active tunnel would pass both. For sensitive services, require a client certificate as well. Create a separate CA for client identities, issue a certificate per device, and add these lines inside the server block:
ssl_client_certificate /etc/nginx/tls/clients-ca.crt;
ssl_verify_client on;
Each device then presents its own certificate, which you can revoke by removing it from the trusted CA chain and reloading. Plan how you will distribute and renew client certificates before you enforce this, or you will lock yourself out.
Verify it worked
Test from both sides of the boundary. Start with the tunnel itself:
sudo wg show wg0 latest-handshakes
ping -c 3 10.8.0.1
A recent handshake timestamp and successful pings confirm the tunnel is up. Next, confirm the service responds over the tunnel with valid TLS and headers:
curl -I --cacert ca.crt https://app.YOUR_DOMAIN/ --resolve app.YOUR_DOMAIN:443:10.8.0.1
Look for your application's normal response code and the Strict-Transport-Security and X-Frame-Options headers. Then confirm the boundary holds from outside. Disconnect the VPN on your client and run a scan from a separate network, such as a mobile hotspot:
nmap -Pn -p 22,80,443,8080 YOUR_PUBLIC_IP
Only the WireGuard port is UDP and will not appear in a default TCP scan. Ports 22, 80, 443, and 8080 should show as filtered or closed. Finally, check the logs to confirm requests are recorded with the tunnel source address:
sudo tail -n 5 /var/log/nginx/app.access.log
Each entry should show a 10.8.0.x client address. Entries from anywhere else indicate a gap in your rules.
Common mistakes
- Forwarding port 443 on the router. This defeats the entire design. Only UDP 51820 should be forwarded, and only if your host sits behind NAT.
- Leaving the application on 0.0.0.0. Many apps bypass your proxy entirely if you do not change their bind address. Check with
ssafter every upgrade, since defaults sometimes return. - Using
AllowedIPs = 0.0.0.0/0on the server's peer entries. On the server, each peer should be a single /32. Broad peer ranges weaken source filtering. - Omitting
alwaysonadd_header. Without it, error pages lose their security headers. - Running two firewall managers. ufw, firewalld, and a hand-written nftables file can silently conflict. Choose one and disable the others.
- Forgetting PersistentKeepalive behind NAT. Connections work for a while, then mysteriously stall on mobile or home networks.
- Losing the CA key or WireGuard private keys. Store backups of
/etc/wireguardand the CA material somewhere encrypted and offline. Recovery without them is slow. - Ignoring certificate expiry. Put a calendar reminder on the renewal date or script a check, because an expired certificate looks like an outage.
- Never reviewing logs. Access logs are only useful if someone reads them or ships them to a central place. Review them weekly at minimum.
Takeaway
The strongest exposure strategy is often no exposure at all. By binding the application to loopback, allowing only WireGuard on the public interface, restricting HTTPS to the tunnel address, and layering TLS, security headers, and source checks in the reverse proxy, you turn a service that scanners can find into one that only your own devices can reach. Test the boundary from outside the network after every change, keep your keys backed up, and treat the logs as part of the system rather than an afterthought.
