If your application runs on a backend service listening on localhost:3000, it is not reachable from the internet, it has no TLS, and every visitor’s IP address looks the same to your app. An nginx reverse proxy solves all three problems at once: it accepts public traffic, forwards it to your backend, terminates SSL, and preserves the client’s real IP address in the process. In this guide you will set up a complete nginx reverse proxy on Ubuntu 24.04, from installation to SSL termination, caching, WebSocket support, and troubleshooting.

By the end you will have a production-style configuration that proxies traffic to a Node.js or Python backend, adds the correct proxy headers (X-Real-IP, X-Forwarded-For, Host), upgrades WebSocket connections, and serves everything over HTTPS with a free Let’s Encrypt certificate. If you are still deciding which web server to standardize on, see our comparison of LiteSpeed vs Nginx vs Apache.
Prerequisites – Set Up Nginx as a Reverse
- A server running Ubuntu 24.04 LTS with root or sudo access.
- A domain name (for example
app.example.com) with an A record pointing to your server’s public IP address. Certbot needs this for Let’s Encrypt. - A backend application listening on
localhost:3000— a Node.js Express app, a Python FastAPI or Flask app, anything that speaks HTTP. You can test with a tiny Python server:python3 -m http.server 3000. - Ports 80 and 443 open in your firewall.
Step 1: Install Nginx on Ubuntu 24.04 – Set Up Nginx as a Reverse
Ubuntu 24.04 ships a recent, stable Nginx build in its default repositories, so installation is a single apt command. Update the package index first, then install:
sudo apt update
sudo apt install nginx -y
Verify the service is running and enabled to start at boot:
sudo systemctl status nginx
sudo systemctl enable nginx
You should see active (running). Now open the firewall. If you use UFW, allow the full Nginx profile so both HTTP and HTTPS are reachable:
sudo ufw allow 'Nginx Full'
sudo ufw status
Open your server’s IP in a browser — the default “Welcome to nginx!” page confirms the install works. The rest of this guide turns that plain web server into an nginx reverse proxy in front of your application.
Step 2: Create the Nginx Reverse Proxy Server Block
On Ubuntu, Nginx uses sites-available / sites-enabled for per-site configuration. Create a new file for your application:
sudo nano /etc/nginx/sites-available/my-app
Add the following server block. It listens on port 80 and forwards every request to the backend on port 3000:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
Two details in this nginx reverse proxy configuration deserve attention:
- The trailing slash on
proxy_passmatters.proxy_pass http://127.0.0.1:3000;(no trailing slash) forwards the full original URI, whileproxy_pass http://127.0.0.1:3000/api/;(with a trailing slash) strips the matched location prefix. Mixing these up is one of the most common nginx reverse proxy bugs. - The four
proxy_set_headerlines are not optional. Without them, your backend sees every request as coming from127.0.0.1with hostlocalhost, which breaks IP-based rate limiting, logging, and any code that builds absolute URLs.X-Real-IPcarries the client address,X-Forwarded-Forappends it to any existing chain, andX-Forwarded-Prototells the app whether the original request was HTTP or HTTPS.
Enable the site and test the configuration syntax before reloading:
sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
nginx -t should report syntax is ok and test is successful. Visit http://app.example.com — you should now see your backend application served through the nginx reverse proxy. For the official directive reference, consult the ngx_http_proxy_module documentation.
Step 3: Add WebSocket Support to the Nginx Reverse Proxy
Applications with real-time features — chat, live dashboards, Socket.io — use WebSockets, which start as an HTTP request and then upgrade the connection. The default proxy configuration breaks this handshake, so the location needs extra headers:
location /ws/ {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket connections stay open a long time; the default
# 60s timeout would drop idle clients.
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
The key directives are proxy_http_version 1.1 (HTTP/1.0 cannot do connection upgrades) and the Upgrade / Connection headers, which pass the client’s upgrade request through to the backend untouched. If your whole app needs WebSockets rather than just one path, you can merge these headers into the main location / block instead.
Step 4: Enable HTTPS with Let’s Encrypt (SSL Termination)
An nginx reverse proxy is the ideal place for SSL termination: Nginx handles the TLS handshake with visitors, and traffic between Nginx and your backend on localhost stays plain HTTP. Install Certbot and its Nginx plugin:
sudo apt install certbot python3-certbot-nginx -y
Run Certbot against your domain. It will obtain a certificate, rewrite your server block, and set up automatic renewal:
sudo certbot --nginx -d app.example.com
Answer the prompts (email, terms, and whether to redirect HTTP to HTTPS — choose redirect). Certbot adds the certificate paths and a managed 443 server block to your configuration. Verify renewal works with a dry run:
sudo certbot renew --dry-run
After Certbot finishes, your nginx reverse proxy listens on port 443 with ssl_certificate pointing at /etc/letsencrypt/live/app.example.com/fullchain.pem. Because of the redirect, all plain-HTTP traffic now lands on HTTPS, and X-Forwarded-Proto correctly reports https to your backend. Mixed-content warnings and insecure-cookie issues disappear because the app knows the real scheme.
Step 5: Add Basic Proxy Caching
If your backend serves content that does not change on every request — API responses, rendered pages, static assets — caching at the nginx reverse proxy level removes load from the application. Define a cache zone in the http context of /etc/nginx/nginx.conf:
http {
...
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m
max_size=1g inactive=60m use_temp_path=off;
}
Then enable caching in your server block:
location / {
proxy_pass http://127.0.0.1:3000;
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;
proxy_cache my_cache;
proxy_cache_valid 200 10m;
proxy_cache_valid 404 1m;
proxy_cache_bypass $http_upgrade;
add_header X-Cache-Status $upstream_cache_status;
}
Successful responses are cached for ten minutes; proxy_cache_bypass $http_upgrade keeps WebSocket upgrades out of the cache. The X-Cache-Status header shows HIT or MISS so you can confirm caching works with curl -I https://app.example.com. Be careful not to cache authenticated or user-specific pages — cache only what is safe to share between visitors.
Troubleshooting: Fixing 502 Bad Gateway Errors
A 502 Bad Gateway from your nginx reverse proxy means Nginx is running but cannot reach the backend. Work through this checklist in order:
- Is the backend actually running? Check with
curl http://127.0.0.1:3000on the server itself. If curl fails, the problem is your app, not Nginx. - Is it listening on the right interface? An app bound to
127.0.0.1is fine forproxy_pass http://127.0.0.1:3000, but if the app binds to a Unix socket or a different port, the addresses must match exactly. Check withss -tlnp | grep 3000. - Read the Nginx error log.
sudo tail -n 50 /var/log/nginx/error.logusually names the cause directly:connect() failed (111: Connection refused)means nothing listens on that port;upstream timed outmeans the app is too slow (raiseproxy_read_timeout). - Check SELinux/AppArmor or firewall rules if the backend runs on another host — outbound connections from Nginx can be blocked by host policies.
- Verify syntax after every edit with
sudo nginx -tand reload withsudo systemctl reload nginx. A typo in one server block can prevent the whole configuration from loading.
If you hit permission-style failures instead — for example Nginx cannot read a file it should serve — our guide to fixing the 403 Forbidden error in Nginx and Apache covers the usual ownership and permission causes.
Conclusion
You now have a fully working nginx reverse proxy on Ubuntu 24.04: traffic enters on ports 80 and 443, gets decrypted at the proxy, and is forwarded to your backend with the correct client headers. WebSocket connections upgrade cleanly, cacheable responses are served from Nginx’s cache, and you know how to diagnose the classic 502 when the backend goes quiet.
From here, the natural next steps are adding rate limiting with limit_req_zone to protect the backend from abuse, setting up an upstream block to load-balance across several backend instances, and monitoring upstream health so Nginx can fail over automatically. An nginx reverse proxy is one of those pieces of infrastructure that quietly does its job for years — set it up carefully once, and it will serve every application you deploy behind it.