0

Does it matter whether you place regular expressions before or after prefix locations?

Consider this configuration:

server {
    listen 80 default_server;

    server_name www.example.com;

    root /var/www/nginx/example.com/public_html;
    index index.php index.html index.htm;

    location ~ /\.well-known { allow all; }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; allow all; 
    }

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ ^.+\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|ppt|tar|mid|midi|wav|bmp|rtf)$ { access_log off; log_not_found off; expires max; }

    location ~ /\. { access_log off; log_not_found off; deny all; }
}

compared with this one:

server {
    listen 80 default_server;

    server_name www.example.com;

    root /var/www/nginx/example.com/public_html;
    index index.php index.html index.htm;

    location ~ /\.well-known { allow all; }

    location ~ ^.+\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|ppt|tar|mid|midi|wav|bmp|rtf)$ { access_log off; log_not_found off; expires max; }

    location ~ /\. { access_log off; log_not_found off; deny all; }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; allow all; }

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
}

Note that while I have grouped the regular expressions together and they now appear near the top, the regular expressions still follow the same sequential order. Will these two configurations behave identically?

0

This is what documentation says about matching order:

To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.


If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.


Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.

https://nginx.org/en/docs/http/ngx_http_core_module.html#location

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.