A
A
asakasinsky2013-12-09 13:27:12
Nginx
asakasinsky, 2013-12-09 13:27:12

How to add headers in Nginx at a specific location (for API response)?

Initial data:

server {
    ... 
    location / {
        ...
    }
    location /api/v1/ {
        ...
    }
    location ~ "^(.+\.php)($|/)" {
        ...
    }
}

Let's declare a header, for example: It is
add_header 'X-my-api-header' 'bla-bla-bla';
required to add this header for the generated api json or xml in location /api/v1/ .
If a title is added before the description of the location rules, the title appears, but for the entire resource. If you add it for location api or location /, the header is missing from the server response, but appears if you put the header in location php. Then the header appears in all responses to requests to php scripts.
Since the API returns data in the requested formats, I tried to declare a header for these formats, but in vain. Perhaps this is happening because the output of data is generated by a php script. Before this task, I naively thought that I knew how the location settings work.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Eugene, 2013-12-09
@asakasinsky

Do you have one entry point per application?

A
Alexey Sundukov, 2013-12-09
@alekciy

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.

J
justabaka, 2013-12-09
@justabaka

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.

D
Dan Ivanov, 2013-12-09
@ptchol

Probably in location /api/v1/ you have something like

if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}

Therefore, the request goes to location ~ "^(.+\.php)($|/)"
If I understand the task correctly, then you need to put down the corresponding header for backend responses with content-type json / xml.
This is where checking the Content-type header in the server response can help.
You can get it through the upstream module variables: $upstream_http_$HEADER. So in your case it will look like $upstream_http_content_type.
And then if you need a custom title value for each content type, apply a map like this:
map $upstream_http_content_type $type {
default "custom_header_default";
"application/json" "custom_header_json";
"text/xml" "custom_header_xml";
}

I have never done this myself, so I only answer "theoretically" how it can work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question