Location block priorities

A problem that occurs frequently when using multiple location blocks in the same server block is that the configuration does not apply as you thought it would.

As an example, say you want to define a behavior to be applied to all the image files that are requested by the clients:

location ~* \.(gif|jpg|jpeg|png)$ {
    # matches any request for GIF/JPG/JPEG/PNG files
    proxy_pass http://imageserver; # proxy pass to backend
}

Later on, you decide to enable the automatic indexing of the /images/ directory. Therefore, you decide to create a new location block matching all the requests starting with /images/:

location ^~ /images/ {
    # matches any request that starts with /images/
    autoindex on;
}

With this configuration, when a client ...

Get Nginx HTTP Server - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.