NGINX as a Reverse Proxy for Node.js, Python and PHP Applications
Configure NGINX in front of Node.js, Gunicorn/Uvicorn and PHP-FPM. Covers proxy_pass, forwarded headers, WebSockets, timeouts, buffering and upload limits.
Published November 27, 2026
Application servers like Node.js, Gunicorn and PHP-FPM are good at running your code but not at facing the internet. A reverse proxy in front handles HTTPS, static files, slow clients, compression and request limits, and hides internal ports. This guide shows production-ready NGINX configs for the three most common backends.
The concept
In a reverse proxy setup the client never connects to your application directly. NGINX accepts the connection, reads the request, opens (or reuses) a connection to the backend, forwards the request, and streams the response back. Because NGINX buffers the response by default, a slow mobile client downloading a large page ties up a cheap NGINX connection rather than an expensive application worker.
The backend loses information in the process: it sees every request coming from 127.0.0.1, over plain HTTP, with whatever Host header NGINX sends. That is why you forward the original details in headers (Host, X-Real-IP, X-Forwarded-For, X-Forwarded-Proto) and configure your framework to trust them from the proxy.
The reverse proxy round trip
Pass directives by backend
| Backend | Protocol | NGINX directive | Typical address |
|---|---|---|---|
| Node.js / Express / Next.js | HTTP | proxy_pass | http://127.0.0.1:3000 |
| Python Gunicorn / Uvicorn | HTTP (or unix socket) | proxy_pass | http://unix:/run/gunicorn.sock |
| PHP-FPM | FastCGI | fastcgi_pass | unix:/run/php/php8.3-fpm.sock |
| Python uWSGI | uwsgi | uwsgi_pass | unix:/run/uwsgi/app.sock |
| gRPC services | HTTP/2 gRPC | grpc_pass | grpc://127.0.0.1:50051 |
Headers every proxied app needs
Put these in a snippet and include it in each proxied location. Then configure the app to trust the proxy: app.set("trust proxy", 1) in Express, ProxyFix in Flask, --forwarded-allow-ips in Gunicorn/Uvicorn, or TrustProxies in Laravel.
# /etc/nginx/snippets/proxy-headers.conf
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;
proxy_set_header X-Forwarded-Host $host;Verifying the proxy
Practical examples
Node.js with WebSockets
WebSockets need the Upgrade and Connection headers passed through.
map $http_upgrade $connection_upgrade { default upgrade; '' close; }
server {
server_name app.example.com;
location / {
include snippets/proxy-headers.conf;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 3600s;
proxy_pass http://127.0.0.1:3000;
}
}Python (Gunicorn/Uvicorn) over a unix socket
Static files are served by NGINX directly.
upstream django { server unix:/run/gunicorn.sock; }
server {
server_name shop.example.com;
client_max_body_size 20m;
location /static/ { alias /srv/shop/staticfiles/; expires 30d; }
location / {
include snippets/proxy-headers.conf;
proxy_pass http://django;
}
}Laravel / PHP-FPM
FastCGI rather than HTTP proxying.
server {
server_name laravel.example.com;
root /var/www/laravel/public;
index index.php;
location / { try_files $uri $uri/ /index.php?$query_string; }
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_read_timeout 60s;
}
}Common mistakes
- Trailing slash confusion in proxy_pass: proxy_pass http://app/ (with a URI) strips the matched location prefix, proxy_pass http://app does not.
- Not forwarding X-Forwarded-Proto, so the app generates http:// links and redirect loops behind HTTPS.
- Default client_max_body_size of 1m causing 413 errors on uploads.
- Forgetting WebSocket upgrade headers, so real-time features fall back or fail.
- Long-running requests hitting the default 60 s proxy_read_timeout.
Key takeaways
- NGINX terminates client connections and forwards requests to local app servers.
- Forward Host and X-Forwarded-* headers and make the app trust the proxy.
- Use proxy_pass for HTTP apps and fastcgi_pass for PHP-FPM.
- Tune body size, timeouts and WebSocket headers per application.
FAQ
Should the app listen on 0.0.0.0?
No. Bind it to 127.0.0.1 or a unix socket so it is reachable only through NGINX.
What is proxy_buffering off for?
Server-sent events and streaming responses. With buffering off, NGINX passes bytes to the client as soon as they arrive from the backend.
Unix socket or TCP port?
Unix sockets are slightly faster and avoid port conflicts on one host. Use TCP when the backend is on another machine or in another container.