SoFunction
Updated on 2025-04-08

Solution to HTTP header loss problem after Nginx proxy

Preface

In daily development and operation and maintenance, we often use Nginx as a reverse proxy server to forward client requests to backend services. However, in some cases, after passing through Nginx proxy, the backend service may lose some HTTP header information, especially headers containing underscores (_), such as access_token. This article will explain in detail how to solve this problem.

1. Problem description

Today I encountered a typical problem: When accessing the backend service through the Nginx proxy, the backend service reported an error:

The authentication type of this interface is the authorization code authentication type. Please add the access_token parameter in the request header.

The address of the backend service can return data normally, but an error is reported after passing through the Nginx proxy. After troubleshooting, it was found that the access_token head was lost during the proxy process.

2. Causes of the problem

Nginx ignores HTTP headers containing underscores (_) by default. This is because underscores are non-standard characters in the HTTP header and may cause some servers or clients to not handle correctly. Therefore, Nginx will discard these headers by default.

3. Solution

3.1 Enable underscores_in_headers

In order for Nginx to pass headers containing underscores, the underscores_in_headers directive needs to be enabled in the Nginx configuration. It should be noted that this directive must be placed in the http block or server block, and cannot be placed in the location block.

Modify the Nginx configuration file (usually /etc/nginx/ or /etc/nginx//):

http {
    # Allows passing headers containing underscores    underscores_in_headers on;

    server {
        listen 80;
        server_name your_domain.com;

        location /proxy-data/ {
            proxy_pass http://172.17.17.17:18081;
            proxy_pass_request_headers on;

            # Optional: Forward a specific header            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;
        }
    }
}

3.2 Reloading Nginx configuration

After modifying the configuration file, test whether the configuration file is correct and reload Nginx:

./nginx -t          # Test whether the configuration file is correct./nginx -s reload   # Reload Nginx

Summarize

By enabling the underscores_in_headers directive, you can resolve the issue of missing HTTP headers after Nginx proxy.

The specific steps are as follows:

  • Add underscores_in_headers on in http block or server block
  • Reload Nginx configuration
  • Verify that the configuration is in effect

This is the end of this article about the solution to the HTTP header loss problem after Nginx proxy. For more related content about HTTP header loss after Nginx proxy, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!