Skip to content

How to Configure Nginx as a Reverse Proxy

DodaTech Updated 2026-06-24 1 min read

Nginx as a reverse proxy forwards requests to backend servers. Correct header configuration ensures the backend gets accurate client information. This guide walks through the specific troubleshooting steps to diagnose and resolve proxy_pass issues.

Before You Begin

Before you begin, be sure to have the following in place:

  • A Linux server with the relevant software installed
  • Access to the command line interface
  • Appropriate permissions (root or sudo)

Quick Fix

Wrong

location / {\n    proxy_pass http://localhost:3000;\n}

Wrong: Proxy without correct headers

location / {\n    proxy_pass http://backend:3000;\n    proxy_set_header Host $host;\n    proxy_set_header X-Real-IP $remote_addr;\n    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n    proxy_set_header X-Forwarded-Proto $scheme;\n}

Right: Proxy with all required headers for backend

Output

Reverse proxy configured for http://backend:3000\nHeaders: X-Real-IP, X-Forwarded-For, X-Forwarded-Proto

Prevention

To avoid future issues, follow these best practices:

  • Always set proxy headers for correct client information
  • Use proxy_set_header Host $host to preserve original hostname
  • Add X-Forwarded-For for logging client IPs on backend
  • Use proxy_pass with upstream groups for load balancing
  • Set proxy_buffering off for streaming applications

DodaTech Tools

For further assistance with any of the above issues, consider using DodaTech consulting services or DodaTech tutorials for more in-depth guidance.

Common Mistakes with proxy pass

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world NGINX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Why does my backend see 127.0.0.1 instead of the client IP?|||Because Nginx connects to the backend from localhost. Add proxy_set_header X-Real-IP $remote_addr and configure the backend to read this header.
What is the difference between proxy_pass with URI and without? With URI (proxy_pass http://backend/): the location prefix is replaced. Without URI (proxy_pass http://backend): the full URI is passed.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro