【发布时间】:2017-05-09 15:04:56
【问题描述】:
这是我的 nginx 配置文件:
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server_names_hash_max_size 10000;
server_names_hash_bucket_size 128;
client_max_body_size 2m;
server {
listen 80;
server_name testheader.com;
ignore_invalid_headers off;
proxy_http_version 1.1;
location ~* ^/testheader {
proxy_request_buffering off;
proxy_next_upstream off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Connection "";
proxy_read_timeout 180s;
set $upstream 127.0.0.1:8888;
proxy_pass http://$upstream ;
}
}
}
我发送了如下示例请求
POST /testhead/magic?null HTTP/1.1:
content-type: text/plain; charset=UTF-8
test_trace_id: 1234567890123456
test_span_id: 1234567890123456
x-forwarded-for: 127.0.0.1:8080
x-test-client-host: 127.0.0.1:8888
x-test-request.toplevel.uuid: xxxxxxx-xxxxxx-xxxxxx-xxxxxxx
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-encoding: gzip
connection: keep-alive
accept-language: null
host: top.ctrip.uat.qa.nt.ctripcorp.com
user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Content-Length: 264
{"ActCode":2}
我希望从我的上游服务器 127.0.0.1:8888 中获取所有无效标头,例如 test_trace_id、test_span_id、all x-{$value},但 没有显示任何标头。同时,nginx 日志在发送无效 headers 阶段没有报告任何错误。
我还尝试使用 tcpdump 在目标上游服务器上捕获请求,我确信 nginx 甚至没有尝试将它们发送出去。
但是,在另一个示例中:
POST /testhead/magic?null HTTP/1.1:
content-type: text/plain; charset=UTF-8
test_trace_id: 1234567890123456
test_span_id: 1234567890123456
host: top.ctrip.uat.qa.nt.ctripcorp.com
x-forwarded-for: 127.0.0.1:8080
x-test-request.toplevel.uuid: xxxxxxx-xxxxxx-xxxxxx-xxxxxxx
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-encoding: gzip
connection: keep-alive
accept-language: null
user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Content-Length: 264
{"ActCode":3}
无效的标头 x-test-request.toplevel.uuid 被发送到上游服务器。
我不认为第二个示例中缺少两个标题是问题的原因。我尝试了很多请求,似乎这个问题是随机发生的,有时指令完美,有时却没有。
这是一个错误吗? 配置文件清楚地告诉 nginx 忽略无效的标头并将它从客户端获得的任何内容传递到上游。 Nginx 没有理由丢弃这些标头。
有人知道怎么解决吗? 提前致谢。
【问题讨论】:
-
您阅读过文档吗?
If the directive is specified on the server level, its value is only used if a server is a default one. The value specified also applies to all virtual servers listening on the same address and port.你的服务器不是默认的,所以这个指令被忽略了。 -
顺便说一句,也许你需要
underscores_in_headers。 -
@AlexeyTen 感谢您的快速回复,并对我不完整的问题描述感到抱歉。将指令放入默认服务器可能是解决它的一种方法,让我们看看。但是,在某些情况下,testheader.com 服务器下的指令确实有效。我更新了描述,以便可以更清楚地描述问题。我仍在寻求解释为什么这个问题是随机提出的。如果你知道为什么会这样,你会很高兴解释原因。再次感谢您的建议。 :)
-
x-forwarded-for是完全有效的标头。 -
@AlexeyTen 感谢您的回答。 :) 它有帮助。
标签: nginx