I am trying to add special header for the index.html file following the documentation here: http://nginx.org/en/docs/http/ngx_http_core_module.html#location. I want to provide X-foo: index
when requested without any path i.e. requested with /
.
I used “=” modifier and tried with this config first:
events {}
http {
server {
listen 80;
root /tmp/web;
index index.html;
location = / {
add_header X-foo "index";
}
location / {
add_header X-foo "other";
}
}
}
The problem is, I got X-foo: other
for both of /
and /foo.html
.
If I put a return 200
inside the first location block, I get X-foo: index
header but without any content.
Now if I specify index.html
in the path and specify the config like:
location = /index.html {
add_header X-foo "index";
}
I get X-foo: index
for the /
request. Seems like on receiving of /
nginx is rerouting the request internally to /index.html
again. Is my understanding correct or there is something I am missing while using the “=” modifier?
CodePudding user response:
That is exactly what Nginx does.
The index
module internally rewrites the URI from /
to /index.html
then restarts the search for a matching location
to process the request.
The location
that finally processes the request is the one that provides the add_header
content, not the location
that receives the initial request.