SoFunction
Updated on 2025-03-03

Detailed explanation of nginx log printing request header information example

question:

When I found out that when nginx forwarding, I seemed to lose the fields customized in the request header~~, so I wanted to try to print out the request header and find out the specific reason.

1. Print the nigx request header

Just simply modify the configuration below

http {
    include       ;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_user_id" "$http_accept_language" ';

    access_log  logs/  main;

    sendfile        on;
    #tcp_nopush     on;
    underscores_in_headers on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    server_names_hash_bucket_size 512;
    client_max_body_size 500m;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
}

The changed location is

log_format , the custom parameter in the print request header is user_id, so you need to add "$http_user_id" to the configuration. Yes, with the $http_ prefix

access_log logs/ main; make it print in the log and view the log

2. But mine doesn't work

After continuous attempts, I discovered a problem.My custom key is user_id, but by default, nginx does not support underscore fields!

That is to say, if it is user-id, then there is no problem

The field I need now is user_id

Add the following configuration in:

underscores_in_headers on;

It's still an http module, there is already

Summarize

  • Print variables in the request header, you need to prefix $http_
  • When you want nginx forward or print variables with underscores, you need to enable underscores_in_headers on;

This is the article about nginx log printing request header information. For more information about nginx log printing request header information, please search for my previous article or continue browsing the following related articles. I hope everyone will support me in the future!