I am trying to define an nginx server configuration as following, but the exact location is never triggered only the general path is triggered. So, the response header is always Access-Control-Allow-Origin. What I want is when I visit "curl -v localhost:8081" it should return me with XYZ header only, because it is exact match, but instead it returns the general one. What am I doing wrong?
server {
listen 8081;
server_name localhost;
#index index.html;
root /usr/share/nginx/wallet;
location / {
try_files $uri /index.html /example.html =404;
add_header Access-Control-Allow-Origin *;
}
location = / {
add_header XYZ "xyxxxxxxxxxxxxx";
}
}
CodePudding user response:
The exact match location is the first location to process the request, but is not the final location to process the request.
Nginx internally rewrites the URI to /index.html
and processed this new request in the general location block.
You can force Nginx to process the entire request within the exact match location by adding a try_files
statement.
For example:
location = / {
try_files /index.html =404;
add_header XYZ "xyxxxxxxxxxxxxx";
}