Nginx as a reverse proxy for Node.js, Docker and other apps
Make an application on an internal port cleanly reachable under your domain with HTTPS, including WebSockets, the real visitor IP and several apps on one server.
Many applications bring their own small web server: a Node.js app on port 3000, a Docker container on port 8080, a dashboard on port 9000. A reverse proxy accepts the requests on ports 80 and 443 and forwards them to the right application. You get HTTPS, clean addresses without port numbers and several apps on one server.
Requirements
- Nginx is installed, see Install Nginx
- Your application runs locally, for example on
http://127.0.0.1:3000
Check that first, directly on the server:
curl -I http://127.0.0.1:3000
Create the configuration
sudo nano /etc/nginx/sites-available/app.example.com
server {
listen 80;
listen [::]:80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
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;
# WebSockets (chats, live updates, consoles)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Enable, test, reload:
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Then add HTTPS with Certbot: sudo certbot --nginx -d app.example.com, see Let's Encrypt certificate with Certbot.
What the headers do
| Header | Purpose |
|---|---|
Host |
The app sees the real domain name instead of 127.0.0.1. |
X-Real-IP, X-Forwarded-For |
The app sees the visitor's IP instead of the proxy's. |
X-Forwarded-Proto |
The app knows the visitor uses HTTPS and builds links correctly. |
Upgrade, Connection |
WebSocket connections are passed through. |
Many frameworks only honour
X-Forwarded-*if you enable it. In Express the setting isapp.set('trust proxy', 1).
Do not expose the app directly
If the app listens on all addresses (0.0.0.0:3000), it is also reachable via http://YOUR-IP:3000 without HTTPS. It is better to let it listen on 127.0.0.1 only. With Docker:
ports:
- "127.0.0.1:3000:3000"
Watch out: Docker bypasses UFW. A port you publish with -p 3000:3000 is open to the outside despite a UFW rule.
Several apps on one server
Create a separate file with its own server_name for each application, for example app.example.com on port 3000 and status.example.com on port 3001. Nginx distributes the requests by name.
Larger uploads and long requests
client_max_body_size 100M; # the default is only 1 MB
proxy_read_timeout 300s; # for slow requests
These lines go into the server or location block.
Common errors
- 502 Bad Gateway: The app is not running or uses a different port.
curl http://127.0.0.1:3000on the server shows it. - 413 Request Entity Too Large: Increase
client_max_body_size. - Endless redirect: The app redirects to HTTPS itself because it does not evaluate
X-Forwarded-Proto.
For the community
Know your stuff? Write a guide.
Share what you know: we review every submitted guide, publish it under your name and say thank you with credit for your services.