6

I cannot debug this piece of nginx configuration:
I want to add some headers to all requests for a .pdf file
Then I want to remove a token which I add to my veiw directory to avoid unwanted browser's cache:

location /static {
    location ~* \.pdf$ {
        add_header Access-Control-Allow-Origin *;
        add_header Content-Disposition 'inline';
    }
    #Remove Anti cache token
    rewrite "^/static[0-9]{10}/(.*)$" /static/$1 last;
    ...
}

There is no nginx syntax error but requesting a .pdf for shows a 404 not found error means that rewrite doesn't apply on the request.

Any help is very appreciated
Thank you

mahyard
  • 237
  • 3
  • 11

2 Answers2

10

When nginx chooses a location to process a request, it may choose the inner or outer location block. It does not combine the statements from both.

The rewrite is not inherited by the nested location. If you want the rewrite to apply to all locations, you should place it in server block scope.

The regular expression of your rewrite statement is sufficiently specific, that it can be moved as is.

For example:

rewrite "^/static[0-9]{10}/(.*)$" /static/$1 last;

location /static {
    location ~* \.pdf$ {
        add_header Access-Control-Allow-Origin *;
        add_header Content-Disposition 'inline';
    }
    ...
}

Of course, it may be more efficient to just repeat the rewrite statement in both location blocks.


An alternative approach is to avoid the rewrite altogether and use a regular expression location to remove the anti-cache token by using an alias directive. See this document for more.

For example:

location ~ "^(?<prefix>/static)[0-9]{10}(?<suffix>/.*)$" {
    alias /path/to/root$prefix$suffix;

    location ~* \.pdf$ {
        add_header Access-Control-Allow-Origin *;
        add_header Content-Disposition 'inline';
    }
    ...
}

Note that regular expression location block have a different evaluation order to prefix location blocks. See this document for details.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29
  • Thank you for your reply... The `rewrite` must be applied to all requests ask a file inside static directory and specially for this reason I want to use nested locations. also I have more locations like what I said in the question. what's your idea? should I repeat the line in ALL location blocks I want it? – mahyard Jan 20 '18 at 10:27
  • I don't know which would be the best solution for you. But I have added an alternative solution for you to consider. – Richard Smith Jan 20 '18 at 10:48
1

While the best answer for this question is what I checked as the best I want to post the solution I used to solve my problem. It's not necessary to define a nested location block to add a header to some of requests. Instead I used an if to check a regex condition and do what I want.
Look at the following block I wrote to handle this:

location /static {
    if ( $request_uri ~* \.pdf$ ) {
        add_header Access-Control-Allow-Origin *;
        add_header Content-Disposition 'inline';
    }

    #Remove Anti cache token
    rewrite "^/static[0-9]{10}/(.*)$" /static/$1 last;
    ...
}
mahyard
  • 237
  • 3
  • 11