SoFunction
Updated on 2025-03-04

The problem of nginx not getting underscore header value is solved

Problem description: Because the project uses xxl, the header needs to be the value of xxl_sso_sessionid. There is no problem locally. When placed on the server, nginx cannot be obtained.

NGINX will convert all request headers containing hyphens (-) to underscore (_) format by default. This is to comply with some system naming rules, because environment variables usually do not allow hyphens.

To use request headers containing hyphens directly in NGINX, some special configuration is required. Can be usedunderscores_in_headersDirectives to disable this behavior, thus retaining the original hyphen format.

Solution 1. Enable underscores_in_headers

First, make sure to enable it in the HTTP blockunderscores_in_headersInstructions:

http {
    underscores_in_headers on;

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

    access_log /var/log/nginx/ main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/;
    default_type application/octet-stream;

    include /etc/nginx//*.conf;
}
(function (config) {
    // What to do before sending a request    let token = ("token");
    if (token != null && token != 'null') {
        ["Xxl_sso_sessionid"] = token;
    }
    return config;
}, function (error) {
    // What to do about the request error    return (error);
});

In this way, if the front-end is configured like this, the back-end can directly obtain the header value.

Solved 2

The front-end configuration is as follows, nginx will automatically convert to underscore, that is, xxl_sso_sessionid, so it does not need to be enabled.underscores_in_headers on

(function (config) {
    // What to do before sending a request    let token = ("token");
    if (token != null && token != 'null') {
        ["Xxl-sso-sessionid"] = token;
    }
    return config;
}, function (error) {
    // What to do about the request error    return (error);
});

nginx configuration

http {

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

    access_log /var/log/nginx/ main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/;
    default_type application/octet-stream;

    include /etc/nginx//*.conf;

    upstream takeout-back-api {
        server localhost:10086 weight=1;
    }

    upstream order-back-api {
        server 8.141.87.136:10000 weight=1;
    }

    server {
        listen 443 ssl;
        server_name ;
        ssl_certificate xxx.cn_bundle.crt;
        ssl_certificate_key ;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;

        location / {
            root /usr/share/nginx/order-ui/dist;
            index  ;
            try_files $uri $uri/ /;
        }

        location /member {
            proxy_pass http://takeout-back-api;
        }

        location /order {
            proxy_set_header xxl_sso_sessionid $http_xxl_sso_sessionid;
            proxy_pass http://order-back-api;
        }
    }
}

This is the article about nginx's problem of not being able to obtain underscore header values. This is the end of this article. For more relevant nginx's content to obtain underscore header values, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!