How to Log proxy_pass Destination URL in Nginx Access Logs


1 views

When working with Nginx reverse proxy configurations, developers often need visibility into where requests are being forwarded. While Nginx logs client IPs by default ($remote_addr), it doesn't automatically log the proxy destination URL from proxy_pass directives.

The most effective approach is to create a custom log format that captures the proxy destination:

log_format proxy_log '$remote_addr - $remote_user [$time_local] '
                     '"$request" $status $body_bytes_sent '
                     '"$http_referer" "$http_user_agent" '
                     '"$proxy_host"';

server {
    listen 80;
    server_name example.com;

    set $proxy_host $proxy_host;    
    access_log /var/log/nginx/access.log proxy_log;

    location / {
        proxy_pass http://yahoo.com;
        proxy_set_header Host $proxy_host;
    }
}

For more complex scenarios with multiple proxy destinations:

map $uri $logged_proxy {
    default "http://yahoo.com";
}

log_format proxy_log '$remote_addr - $logged_proxy [$time_local] "$request"';

server {
    access_log /var/log/nginx/access.log proxy_log;
    
    location / {
        proxy_pass http://yahoo.com;
    }
}

After implementing either solution, check your logs with:

tail -f /var/log/nginx/access.log

You should now see entries containing your proxy destination URLs alongside other request information.


When working with Nginx reverse proxy configurations, developers often need visibility into where requests are being forwarded. While Nginx logs client IPs ($remote_addr) by default, it doesn't automatically log the proxy_pass destination in access logs.

The most reliable way is to leverage Nginx's built-in variables in your log format:

log_format proxied '$remote_addr - $remote_user [$time_local] '
                   '"$request" $status $body_bytes_sent '
                   '"$http_referer" "$http_user_agent" '
                   '"$upstream_addr"';

server {
    access_log /var/log/nginx/access.log proxied;
    
    location / {
        proxy_pass http://yahoo.com;
    }
}

Depending on your specific needs, you might want to use:

  • $proxy_host: Contains the domain name from proxy_pass
  • $proxy_port: The port number from proxy_pass
  • $proxy_add_x_forwarded_for: Client IP with X-Forwarded-For header

For complex setups with multiple proxy_pass directives:

http {
    log_format proxy_log '$remote_addr - $upstream_addr - [$time_local] '
                        '$request $status $body_bytes_sent';

    server {
        location /search {
            proxy_pass http://search.backend.com;
            access_log /var/log/nginx/search_proxy.log proxy_log;
        }

        location /images {
            proxy_pass http://images.backend.com:8080;
            access_log /var/log/nginx/image_proxy.log proxy_log;
        }
    }
}

If you're not seeing the expected values:

  • Verify Nginx version (variables available may differ)
  • Check for syntax errors with nginx -t
  • Ensure the log format is properly associated with the server/location