You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Docker容器时Nginx请求头无法正常获取的问题排查

问题原因与解决方法

核心问题:map指令未配置匹配规则

你的map块仅声明了将$http_abc映射到$abc变量,但未定义任何匹配规则,导致$abc默认是空值,无法传递请求头的内容。

修正后的nginx.conf配置

events{}
http {
    map "$http_abc" $abc {
        default $http_abc; # 将请求头abc的值直接赋值给$abc变量
    }
    server {
        listen 80;
        server_name localhost;
        root /usr/share/nginx/html;
        index index.html;
        location / {
            add_header Set-Cookie "abc=$abc; Path=/;";
        }
    }
}

补充说明:Nginx请求头变量规则

Nginx会将请求头名称转换为小写,再以http_为前缀生成对应变量。比如自定义请求头X-Test,对应的变量是$http_x_test;你的场景中请求头abc对应$http_abc的写法是正确的。

验证操作步骤

  1. 重新构建镜像:
docker build -f ./Dockerfile . -t abc
  1. 替换旧容器:
docker stop competent_swirles && docker rm competent_swirles
docker run -d -p 8888:80 --name competent_swirles abc
  1. 再次测试:
curl localhost:8888 -H "abc:123" --verbose

此时响应头的Set-Cookie会正确返回abc=123; Path=/;。

内容的提问来源于stack exchange,提问作者Yuriy Faktorovich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 10:52:59