Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Configure NGINX as a Reverse Proxy

Forward a hostname to an Apache or application backend, verify the configuration, and troubleshoot gateway and redirect errors.

Table of Contents

Point an NGINX location block at your application using proxy_pass. Clients connect to NGINX, which fetches the response from the backend and returns it. This example assumes NGINX listens on port 80 and the backend listens on 127.0.0.1:8080 on the same host.

Instructions for configuring Reverse Proxy with the latest NGINX Picture 1

Instructions for configuring Reverse Proxy with the latest NGINX Picture 2
NGINX receives the request and forwards it to the backend web server.

1. Verify the backend before adding the proxy

From the NGINX host, run curl -i http://127.0.0.1:8080/. Confirm it returns the expected page or application response. If Apache and NGINX run on the same address, configure them to listen on different ports. If the backend is on another host, use its private address and let only the proxy and authorized operators reach it.

2. Add the NGINX server block

Use an NGINX site configuration included by your distribution. Replace app.example.com and the backend address for your setup. This example assumes NGINX is the first proxy clients contact.

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

This configuration replaces the incoming X-Forwarded-For value with the connecting client's address. If another trusted proxy or load balancer precedes NGINX, configure trusted proxy addresses and verify how the application interprets forwarded headers. The NGINX proxy module reference explains path and header behavior.

3. Test and reload

sudo nginx -t
sudo systemctl reload nginx
curl -i -H 'Host: app.example.com' http://127.0.0.1/

Only reload after the configuration test succeeds. The last command sends a request through NGINX. Compare the status and content with the direct backend test. Configure DNS and a valid certificate before serving public traffic over HTTPS. TLS termination at NGINX covers the client-to-proxy leg. Decide separately how to protect traffic to a backend on another host.

Instructions for configuring Reverse Proxy with the latest NGINX Picture 3
Verify that the proxy request returns the expected backend page.

Fix common reverse proxy errors

  • 502 Bad Gateway: Check that the backend is running and reachable at the configured address. Review NGINX and backend logs.
  • Default NGINX page: Check whether the server block is included and whether the request's host matches server_name.
  • Redirect loop: Check whether the application is receiving and trusting the original HTTPS scheme.

Use the Linux service logs guide for troubleshooting and the TLS troubleshooting guide for certificate or redirect problems.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.