Nginx not respecting locations execution ordering

c0nw0nk nginx-forum at forum.nginx.org
Wed Apr 18 21:00:21 UTC 2018


Igor Sysoev Wrote:
-------------------------------------------------------
> > On 18 Apr 2018, at 01:35, c0nw0nk <nginx-forum at forum.nginx.org>
> wrote:
> > 
> > Thank you for the help :)
> > 
> > A new dilemma has occurred from this.
> > 
> > I add a location like so.
> > 
> > location ^~/media/files/ {
> > add_header X-Location-Order First;
> > }
> > location ~ \.mp4$ {
> > add_header X-Location-MP4 Served-from-MP4-location;
> > }
> > location ~*
> >
> \.(ico|png|jpg|jpeg|gif|flv|mp4|avi|m4v|mov|divx|webm|ogg|mp3|mpeg|mpg
> |swf|css|js)$
> > {
> > add_header X-Location-Order Second;
> > }
> > 
> > 
> > How can i make it so my MP4 location is not overridden by the
> > ^~/media/files/ location.
> > 
> > I would like the responses to be like this.
> > 
> > URL : domain_name_dot_com/media/files/image.jpg
> > Header response is X-Location-Order: First
> > 
> > URL : domain_name_dot_com/media/files/video.mp4
> > Header response is X-Location-MP4: Served-from-MP4-location
> > 
> > URL : domain_name_dot_com/media/files/other.css
> > Header response is X-Location-Order: Second
> > 
> > 
> > How can I achieve that is it possible to have a location inside a
> location ?
> 
> If you prefer execution of the listed order, the you should use regex
> locations only:
> 
> location ~ ^/media/files/.+\.mp4$ {
>     add_header X-Location-MP4 Served-from-MP4-location;
> }
> location ~ ^/media/files/ {
>     add_header X-Location-Order First;
> }
> location ~*
> \.(ico|png|jpg|jpeg|gif|flv|mp4|avi|m4v|mov|divx|webm|ogg|mp3|mpeg|mpg
> |swf|css|js)$ {
>     add_header X-Location-Order Second;
> }
> 
> However this approach is not scaleable. Suppose you have hundreds of
> locations, then you have
> to find appropriate place where to add a new location and have to
> investigate the whole large
> configuration to understand how this tiny change will affect the
> configuration.
> 
> The other approach is to use prefix locations and to isolate regex
> location inside prefix ones:
> 
> location / {
>     location ~*
> \.(ico|png|jpg|jpeg|gif|flv|mp4|avi|m4v|mov|divx|webm|ogg|mp3|mpeg|mpg
> |swf|css|js)$ {
>         add_header X-Location-Order Second;
>     }
> }
> 
> location /media/files/ {
>     add_header X-Location-Order First;
> 
>     location ~ \.mp4$ {
>         add_header X-Location-MP4 Served-from-MP4-location;
>     }
> }
> 
> With this configuration you can sort the first level prefix locations
> in any order.
> 
> 
> -- 
> Igor Sysoev
> http://nginx.com
> 
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx


Thank you Igor for the information :)

My soloution has become this.

location ^~/media/files/ {
add_header X-Location-Order First;

location ~ \.mp4$ {
add_header X-Location-MP4 Served-from-MP4-location;
}

location ~*
\.(ico|png|jpg|jpeg|gif|flv|mp4|avi|m4v|mov|divx|webm|ogg|mp3|mpeg|mpg|swf|css|js)$
{
add_header X-Location-Order Second;
}

}

Super appreciate the help from everyone and info now I know what to do and
reflect upon in future :)

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,279504,279528#msg-279528



More information about the nginx mailing list