0
0
NginxHow-ToBeginner · 3 min read

How to Proxy to Localhost Using Nginx: Simple Guide

To proxy requests to a localhost server using nginx, use the location block with the proxy_pass directive pointing to http://localhost:PORT/. This forwards incoming requests to your local backend service running on the specified port.
📐

Syntax

The basic syntax to proxy requests in nginx uses the location block inside a server block. The proxy_pass directive specifies the target URL where requests should be forwarded.

  • location /path/: Defines the URL path to match incoming requests.
  • proxy_pass http://localhost:PORT/;: Forwards matched requests to the local server on the given port.
nginx
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000/;
    }
}
💻

Example

This example shows a complete nginx server block that proxies all requests to a local server running on port 3000. It demonstrates how to forward traffic from port 80 to your backend app on localhost.

nginx
server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://localhost: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;
    }
}
Output
When you visit http://localhost in your browser, nginx forwards the request to http://localhost:3000 and returns the response from that local server.
⚠️

Common Pitfalls

  • Forgetting to include the trailing slash in proxy_pass can cause unexpected URL rewriting.
  • Not setting necessary headers like Host or X-Real-IP can break backend logic relying on client info.
  • Proxying to the wrong port or service that is not running will cause connection errors.
  • Not restarting or reloading nginx after config changes means your proxy won't work until you do.
nginx
server {
    listen 80;
    server_name localhost;

    location /api {
        # Wrong: missing trailing slash causes path issues
        proxy_pass http://localhost:3000;
    }

    location /app/ {
        # Correct: trailing slash preserves path
        proxy_pass http://localhost:3000/;
    }
}
📊

Quick Reference

Remember these tips when proxying to localhost with nginx:

  • Use proxy_pass http://localhost:PORT/; with trailing slash for path consistency.
  • Set headers like Host and X-Real-IP for proper backend handling.
  • Reload nginx config after changes with sudo nginx -s reload.
  • Ensure your local service is running on the target port before proxying.

Key Takeaways

Use the proxy_pass directive inside a location block to forward requests to localhost.
Always include a trailing slash in proxy_pass URL to avoid path rewriting issues.
Set proxy headers to pass client information to the backend server.
Reload nginx after configuration changes to apply the proxy settings.
Verify your local backend service is running on the specified port before proxying.