On this page
- Why put Nginx in front of an app?
- How do you install Nginx and prepare the app?
- What does a basic proxy server block look like?
- How do you support WebSockets?
- How do you tune timeouts, uploads and buffering?
- How do you serve static files and proxy a sub-path?
- How do you add HTTPS?
- How do you fix 502 and 504 errors?
- Questions
Why put Nginx in front of an app?
Application servers such as Node.js, Gunicorn, Uvicorn, Puma or a Go binary are good at running code and weak at everything around it. Nginx takes over the outer jobs: it terminates TLS, serves static files from disk, buffers slow clients so app workers are not tied up, compresses responses, and lets several apps share ports 80 and 443 by host name. The app stays on a loopback port that nobody outside can reach.
This guide assumes the app already runs as a service. If it does not yet, start with systemd services, or PM2 for Node.js, and come back once curl http://127.0.0.1:3000/ answers on the server.
How do you install Nginx and prepare the app?
Install the packaged Nginx and open web ports:
# Ubuntu and Debian
sudo apt update && sudo apt install -y nginx
sudo ufw allow 'Nginx Full'
# Rocky and Alma
sudo dnf install -y nginx
sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload
sudo setsebool -P httpd_can_network_connect 1
sudo systemctl enable --now nginxMake the app listen on 127.0.0.1, not 0.0.0.0. Check what it binds with sudo ss -ltnp | grep 3000. If the output shows 0.0.0.0:3000 or *:3000, change the host setting in the app; the port should never be opened in the firewall, because Nginx is the only client it needs.
What does a basic proxy server block look like?
Keep the forwarding headers in a snippet so every site can include them. Create /etc/nginx/snippets/proxy-headers.conf (run sudo mkdir -p /etc/nginx/snippets first on Rocky and Alma):
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_set_header X-Forwarded-Host $host;Then the site file, in sites-available on Debian family or conf.d/app.example.net.conf on Rocky and Alma:
server {
listen 80;
listen [::]:80;
server_name app.example.net;
location / {
proxy_pass http://127.0.0.1:3000;
include /etc/nginx/snippets/proxy-headers.conf;
}
}Enable, test and reload:
sudo ln -s /etc/nginx/sites-available/app.example.net /etc/nginx/sites-enabled/ # Debian family only
sudo nginx -t && sudo systemctl reload nginx
curl -I http://app.example.net/Without the Host header the app would see 127.0.0.1 as its host name, and without the forwarded headers it would log the proxy address for every visitor.
How do you support WebSockets?
WebSocket clients ask to upgrade the connection, and Nginx has to pass that request on with HTTP/1.1. Add a map once in the http context, for example in /etc/nginx/conf.d/upgrade-map.conf:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}Then add three lines inside the location:
location / {
proxy_pass http://127.0.0.1:3000;
include /etc/nginx/snippets/proxy-headers.conf;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 1h;
}The long read timeout stops Nginx from closing idle sockets after the default 60 seconds. Socket.IO, live dashboards, chat apps and many admin panels need this.
How do you tune timeouts, uploads and buffering?
Match the proxy limits to what the app really does. The defaults suit quick page loads; exports, uploads and streams need more room.
client_max_body_size 50m;raises the upload limit from 1 MB, which otherwise produces413 Request Entity Too Large.proxy_read_timeout 300s;gives slow reports time to finish before Nginx returns504.proxy_buffering off;in a dedicated location passes server-sent events and streamed responses straight through.proxy_buffer_size 16k;andproxy_buffers 8 16k;fixupstream sent too big headerwhen apps send large cookies.
Set these per location rather than globally, so one heavy endpoint does not loosen limits everywhere.
How do you serve static files and proxy a sub-path?
Let Nginx deliver built assets straight from disk and send everything else to the app:
location /static/ {
alias /var/www/app/static/;
expires 7d;
access_log off;
}To mount an app under a path such as example.net/app/, mind the trailing slash. proxy_pass http://127.0.0.1:3000/; with a slash strips /app/ before forwarding; without the slash the full path reaches the app. Most frameworks also need a base path setting so their links include the prefix. Apps that listen on a Unix socket work too: proxy_pass http://unix:/run/app/app.sock;, as long as the Nginx user can read the socket.
How do you add HTTPS?
Point an A and AAAA record for app.example.net at 198.51.100.24 and 2001:db8:4f2a::24 (see DNS records), then let Certbot rewrite the block:
sudo apt install -y certbot python3-certbot-nginx # Ubuntu and Debian
sudo dnf install -y epel-release && sudo dnf install -y certbot python3-certbot-nginx # Rocky and Alma
sudo certbot --nginx -d app.example.net
sudo certbot renew --dry-runAfter this, X-Forwarded-Proto carries https. Tell the app to trust the proxy (Express app.set('trust proxy', 1), Django SECURE_PROXY_SSL_HEADER, and similar settings elsewhere) or it may build plain HTTP links and loop on redirects.
How do you fix 502 and 504 errors?
A 502 Bad Gateway means Nginx could not get a valid answer; a 504 Gateway Timeout means the app took too long. Work from the inside out:
curl -I http://127.0.0.1:3000/
systemctl status myapp --no-pager
sudo tail -n 30 /var/log/nginx/error.log
sudo journalctl -u myapp -n 50- connect() failed (111: Connection refused): the app is down or on another port.
- (13: Permission denied) on Rocky or Alma: set
httpd_can_network_connect. - upstream timed out: raise
proxy_read_timeoutor speed up the endpoint. - Redirect loop after HTTPS: the app does not trust
X-Forwarded-Proto.
For more than one app instance, the load balancing guide shows how to turn the single address into an upstream group.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Nginx as a reverse proxy for an app
Should the app listen on 0.0.0.0?
No. Bind it to 127.0.0.1 so only Nginx on the same server can reach it. That keeps unencrypted app traffic off the internet.
Can one Nginx proxy several apps?
Yes. Give each app its own port and its own server block with a different server_name, such as app.example.net and api.example.net.
Why does my app log 127.0.0.1 for every visitor?
It reads the socket address instead of the forwarded header. Use X-Real-IP or X-Forwarded-For, and enable the framework option that trusts one proxy hop.
Do I need Nginx if my app can do TLS itself?
Not strictly, but Nginx makes certificate renewal, multiple domains, static files and buffering much easier to manage.
How is this different from Caddy?
Caddy fetches certificates on its own with a shorter config. Nginx needs Certbot but gives finer control. The Caddy tutorial shows the other route.
More in Web servers and proxies
- Install and configure Nginx
- Install and configure Apache
- OpenLiteSpeed web server
- HAProxy load balancer and TLS proxy
- Load balancing with Nginx upstreams
- Apache as a reverse proxy
- Compression with gzip and Brotli
- HTTPS and www redirects
- Turn off Apache directory listings
- Nginx Proxy Manager with Docker
- Nginx UI web interface
- Traefik for Docker containers
- Apache Tomcat for Java apps
- Encrypted FTP with vsftpd (FTPS)
- Varnish cache in front of a site