0
0
Nginxdevops~5 mins

Proxy headers forwarding in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you use Nginx as a proxy server, it forwards requests to another server. Proxy headers forwarding means sending important information like the original client's IP address and protocol to the backend server. This helps the backend know who made the request and how it was made.
When you want your backend server to know the real IP address of the user visiting your website.
When your backend needs to know if the original request was made over HTTP or HTTPS.
When you want to keep track of the original host name requested by the client.
When you use Nginx as a reverse proxy in front of an application server.
When you want to pass custom headers from Nginx to your backend for logging or security.
Config File - nginx.conf
nginx.conf
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:8080;
        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;
    }
}

This configuration sets up Nginx to listen on port 80 for requests to example.com.

The proxy_pass directive forwards requests to a backend server running on localhost port 8080.

The proxy_set_header lines add headers to the forwarded request:

  • Host sends the original host requested by the client.
  • X-Real-IP sends the client's real IP address.
  • X-Forwarded-For adds the client's IP to the chain of forwarded IPs.
  • X-Forwarded-Proto tells the backend if the original request was HTTP or HTTPS.
Commands
This command tests the Nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads Nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command sends a simple HTTP request to check if Nginx is forwarding requests properly.
Terminal
curl -I http://example.com
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive
-I - Fetch only HTTP headers without the body
Key Concept

If you remember nothing else from this pattern, remember: forwarding the original client information in headers lets your backend know who made the request and how.

Common Mistakes
Not setting the X-Forwarded-For header in the proxy configuration.
The backend server will not know the real client IP, only the proxy's IP.
Always include proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; to pass the client IP chain.
Forgetting to reload Nginx after changing the configuration.
Changes won't take effect until Nginx reloads or restarts.
Run sudo systemctl reload nginx to apply changes safely.
Setting Host header incorrectly or not at all.
Backend may reject requests or behave unexpectedly if it doesn't receive the correct Host header.
Set proxy_set_header Host $host; to forward the original host requested.
Summary
Use proxy_set_header directives to forward client IP, host, and protocol to the backend.
Test your Nginx configuration with nginx -t before reloading.
Reload Nginx to apply changes without downtime using systemctl reload nginx.