location regex

Igor Sysoev igor at sysoev.ru
Wed Nov 2 11:28:05 UTC 2016


On 02 Nov 2016, at 01:49, olat <nginx-forum at forum.nginx.org> wrote:

> Looks like there is a bug in the forum. 2 the same topics and the response
> ended up in the wrong thread, mixed up ;-)

Yes, the forum has some issues when answer come from the mailing list.

> Anyway, Thanks Igor for a quick response. Could you explain more why regex
> is not a good idea? I am asking about regex in the context of caching some
> of the requests on front-end proxy to speed up django app loading dynamic
> content where session is involved. Does it mean each of the filtered out
> requests we would like to cache, should duplicate the same location block? 
> 
> 
> server {
>   ...
> 
>    proxy_set_header Host myhost;
>    proxy_set_header X-Real-IP $remote_addr;
>    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
>    proxy_set_header X-Forwarded-Proto- $scheme;
> 
>    # catch only /appfoo* and /appbar*
>    location ~ /app(foo|bar)* {
>        proxy_pass http://ip;
>        proxy_redirect http://ip $scheme://$server_name;
> 
>        proxy_cache            $cache_zone_name;
>        proxy_cache_key        "$request_uri";
>        ...
>        proxy_cache_methods    GET HEAD;
>        proxy_cache_bypass     $cache_refresh;
>        proxy_no_cache         $skip_cache;
> 
>        proxy_ignore_headers   "Set-Cookie" "Vary" "Expires";
>        proxy_hide_header Set-Cookie;
>    }
>    # everything else goes here
>    location / {
>        proxy_pass http://ip;
>        proxy_redirect http://ip $scheme://$server_name;
>    }
> 
> Thanks in advance for help.

The regex locations are tested in order of their appearance.
The prefix locations match the longest match regardless of their order.
If you will have a lot of locations then prefix locations are much easier
to maintain: you can add, change and delete prefix locations without
worrying how this will affect other locations.


-- 
Igor Sysoev
http://nginx.com



More information about the nginx mailing list