Log inSign up

Proxy an application through Nginx

Bind your app to 127.0.0.1 on a port such as 3000, then add a server block whose location / uses proxy_pass http://127.0.0.1:3000; plus Host and X-Forwarded headers. Add the Upgrade headers for WebSockets, run sudo certbot --nginx for HTTPS, and Nginx handles TLS, compression and static files while the app only handles requests.

Topic: Web servers and proxies · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

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 nginx

Make 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 produces 413 Request Entity Too Large.
  • proxy_read_timeout 300s; gives slow reports time to finish before Nginx returns 504.
  • proxy_buffering off; in a dedicated location passes server-sent events and streamed responses straight through.
  • proxy_buffer_size 16k; and proxy_buffers 8 16k; fix upstream sent too big header when 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-run

After 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_timeout or 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.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2one small app behind Nginx1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4several apps or a heavier runtime2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

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.