Answer the question
In order to leave comments, you need to log in
How to add headers in Nginx at a specific location (for API response)?
Initial data:
server {
...
location / {
...
}
location /api/v1/ {
...
}
location ~ "^(.+\.php)($|/)" {
...
}
}
add_header 'X-my-api-header' 'bla-bla-bla';
Answer the question
In order to leave comments, you need to log in
It seems to me that the approach is initially architecturally incorrect.
For the data, as I understand it, you still need to go to PHP. So why these dances with location? Send all requests for processing in php, add the corresponding http header to the script by file extension.
If 1 location, then you can use if if there are no fears that if is evil , something like this:
if ($uri ~ ^/api/v1/.*){
add_header 'X-my-api-header' ' bla-bla-bla';
}
If you can spawn as many locations as you like, then copy the existing one to /api/v1/, configure it similarly to the existing one (fastcgi_pass and all that) and add the header.
In general, it's easier to put the heading with PHP than to engage in such a perversion.
Probably in location /api/v1/ you have something like
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
map $upstream_http_content_type $type {
default "custom_header_default";
"application/json" "custom_header_json";
"text/xml" "custom_header_xml";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question