til - nginx and headers with underscores

· bergpb's blog

How to allow Nginx to receive headers with underscores

It was requested to me to troubleshoot an issue with a Nginx setup.

Short history: the header passed by the application wasn't available on the upstream host.

To troubleshoot this, the first step here was to activate debug on Nginx, with that we are able to see the headers received by the server and also more details about the request received:

These lines bellow was added to the Nginx site config file:

error_log /var/log/nginx/error.log debug;

log_format detailed '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent" '
                    '"$http_x_forwarded_for"';
...

Then check for sintax errors and restart Nginx server:

sudo nginx -t &&\
  sudo systemctl restart nginx

Once the server is up and running again, the curl command to perform the request was executed and them:

client sent invalid header line: "<header_name>: <header_value>" while reading client request headers

Now was the time to figure out what is happening here, the official docs and this helps with the issue. By default Nginx ignore headers with underscores, only accepting values with -. It's possible to change this adding underscores_in_headers on; on the server block.

Once this was performed and server restarted, Nginx was able to read the header and send it to the proxy.

last updated: