我正试图为以下文档的index.html文件添加特殊的头:http://nginx.org/en/docs/http/ngx_http_core_module.html#location。我想在请求时提供X-foo: index
,而没有任何路径,即请求/
。
我使用了" = "修饰符,并首先尝试了这个配置:
events {}
http {
server {
listen 80;
root /tmp/web;
index index.html;
location = / {
add_header X-foo "index";
}
location / {
add_header X-foo "other";
}
}
}
问题是,我得到了/
和/foo.html
的X-foo: other
。如果我把return 200
放在第一个位置块,我得到X-foo: index
头,但没有任何内容。
现在如果我在路径中指定index.html
并指定配置如下:
location = /index.html {
add_header X-foo "index";
}
我得到/
请求的X-foo: index
。似乎在接收/
nginx是重新路由请求内部到/index.html
再次。我的理解是正确的,还是有一些东西我错过了,而使用"="修饰语?
Nginx就是这么做的。
index
模块内部将URI从/
重写为/index.html
,然后重新搜索匹配的location
来处理请求。
最后的location
处理请求的是提供add_header
内容的请求,而不是接收初始请求的location
。