An nginx reverse proxy is a server block whose location uses proxy_pass http://127.0.0.1:3000; to forward each request to an app running on another port, so clients only ever talk to nginx. Add proxy_set_header lines for Host, X-Real-IP and X-Forwarded-For so the app still sees the original host name and visitor address.
Everything below is taken from the official nginx documentation at nginx.org (checked November 2026). Commands assume Linux; the config syntax is the same on every platform.
A minimal nginx reverse proxy config
Suppose your app listens on 127.0.0.1:3000 and you want it reachable at http://app.example.com:
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;
}
}
Line by line:
listen 80;andserver_namedecide which requests this block handles.location /matches every path. nginx picks the longest matching prefix location, then checks regular-expression locations, which override it if one matches.proxy_passis the address of the backend. It can be an IP and port, a host name, a Unix socket (http://unix:/run/app.sock:, with the path between colons) or the name of anupstreamgroup.- The
proxy_set_headerlines are explained next. Leave them out and many apps generate wrong links and log the wrong IP.
Which headers to set, and why
By default nginx does not pass the client's Host and Connection headers to the backend. It sends these instead:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
$proxy_host is the name and port from proxy_pass, so the app would see Host: 127.0.0.1:3000 instead of app.example.com. That breaks virtual hosts, redirects and absolute URLs.
| Header | Value | What it gives the app |
|---|---|---|
Host | $host | The host name the client asked for. The docs recommend $host over $http_host because $host falls back to the server name when the client sent no Host header. |
X-Real-IP | $remote_addr | The address of the client connecting to nginx |
X-Forwarded-For | $proxy_add_x_forwarded_for | Any existing X-Forwarded-For list with $remote_addr appended, so a chain of proxies is preserved |
X-Forwarded-Proto | $scheme | Whether the client used http or https, so the app builds correct links after nginx terminates TLS |
These headers are only hints that anyone can send. Configure your app framework to trust them only when the request comes from the proxy's own address, or a client can fake its IP.
The proxy_pass trailing slash rule
This is the most common nginx proxy surprise. What matters is whether proxy_pass contains a URI (anything after the host and port, even just /):
- Without a URI (
proxy_pass http://127.0.0.1:3000;), the request URI is passed unchanged. - With a URI, the part of the request that matched the
locationis replaced by that URI.
| location | proxy_pass | Request | Backend receives |
|---|---|---|---|
/api/ | http://127.0.0.1:3000 | /api/users | /api/users |
/api/ | http://127.0.0.1:3000/ | /api/users | /users |
/api/ | http://127.0.0.1:3000/v2/ | /api/users | /v2/users |
The docs' own example is location /name/ { proxy_pass http://127.0.0.1/remote/; }, where /name/ is replaced by /remote/. Keep the slashes consistent: location /api/ with proxy_pass http://127.0.0.1:3000/v2 (no trailing slash) turns /api/users into /v2users. For regex locations and when proxy_pass contains variables, the docs say to specify it without a URI.
Proxying WebSockets
WebSockets start as an HTTP request with Upgrade and Connection headers, which nginx does not forward by default. The nginx docs give this pattern:
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
...
location /chat/ {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
}
On nginx versions before 1.29.7, also add proxy_http_version 1.1;, because older versions spoke HTTP/1.0 to the backend by default. Since 1.29.7, HTTP/1.1 is the default. Check yours with nginx -v.
Idle WebSocket connections are closed if the backend sends nothing for 60 seconds. Raise proxy_read_timeout or have the server send WebSocket ping frames.
Adding HTTPS in front of the app
nginx can terminate TLS and talk plain HTTP to the app on localhost:
server {
listen 443 ssl;
server_name app.example.com;
ssl_certificate /etc/ssl/app.example.com.crt;
ssl_certificate_key /etc/ssl/app.example.com.key;
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;
}
}
server {
listen 80;
server_name app.example.com;
return 301 https://$host$request_uri;
}
The docs note that the defaults are already ssl_protocols TLSv1.2 TLSv1.3 and ssl_ciphers HIGH:!aNULL:!MD5, so you rarely need to set them. The second block redirects plain HTTP to HTTPS. To check the certificate you end up serving, see how to check an SSL certificate.
Load balancing across several backends
Name a group of servers with upstream and point proxy_pass at it:
upstream app_backend {
server 127.0.0.1:3000 weight=2;
server 127.0.0.1:3001;
server 127.0.0.1:3002 backup;
}
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://app_backend;
proxy_set_header Host $host;
}
}
By default nginx uses weighted round-robin: here, out of every 3 requests, 2 go to port 3000 and 1 to port 3001. The backup server only gets traffic when the others are unavailable. A server counts as unavailable after max_fails failed attempts (default 1) within fail_timeout (default 10 seconds), and stays out for that same fail_timeout.
Test and reload without downtime
The main config file is nginx.conf in /etc/nginx, /usr/local/nginx/conf or /usr/local/etc/nginx, depending on how nginx was installed. Distribution packages often split site configs into separate files pulled in with include. After every change:
sudo nginx -t
sudo nginx -s reload
nginx -t checks the syntax and tries to open the files the config refers to. nginx -T does the same and prints the full merged configuration, which shows you exactly what nginx loaded. nginx -s reload applies the new config; on systemd distributions sudo systemctl reload nginx does the same job (see systemctl commands).
If the proxy doesn't work
- 502 Bad Gateway: RFC 9110 defines it as the proxy receiving "an invalid response" from the server behind it; in practice, nginx couldn't reach the backend or the backend failed. Check the app is running and listening on the exact address in
proxy_pass, for example withcurl -i http://127.0.0.1:3000/on the server. Look in the nginx error log for the upstream address it tried; theerror_logline innginx -Toutput shows where that log is. - 504 Gateway Timeout: the proxy "did not receive a timely response" (RFC 9110). With nginx that usually means the backend sent nothing for longer than
proxy_read_timeout(60 seconds by default) orproxy_connect_timeout. - Redirects go to
127.0.0.1:3000: theHostheader isn't being passed. Addproxy_set_header Host $host;. nginx's defaultproxy_redirect defaultalso rewritesLocationheaders that match theproxy_passURL. - Wrong paths reach the app: check the trailing-slash table above.
- Streaming responses arrive all at once:
proxy_bufferingis on by default. Setproxy_buffering off;in that location for server-sent events or streamed output; the docs say the response is then passed to the client "immediately as it is received".
Frequently asked questions
- What is the difference between a reverse proxy and a forward proxy?
- A forward proxy acts for clients: browsers send their requests through it to reach any website, often for filtering or privacy. A reverse proxy acts for servers: clients think they are talking to your site, and the proxy passes each request to one of your backend applications. nginx with proxy_pass is a reverse proxy.
- Why does my app see 127.0.0.1 as every visitor's IP address behind nginx?
- Because the connection to your app really does come from nginx. Pass the original address in headers, for example proxy_set_header X-Real-IP $remote_addr and proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for, and configure your framework to trust those headers only from the proxy's address.
- Why do I get 502 Bad Gateway from nginx?
- nginx could not get a valid response from the backend. The usual causes are that the app is not running, it listens on a different port or address than the one in proxy_pass, or it crashed while handling the request. The nginx error log names the upstream address it tried, which tells you which of these it is.
- How do I increase the nginx proxy timeout?
- Set proxy_read_timeout, which defaults to 60 seconds and applies between two successive reads from the backend, not to the whole response. For example, proxy_read_timeout 300s; inside the location. proxy_connect_timeout controls how long nginx waits to open the connection and usually cannot exceed 75 seconds.