I have this config that works as expected in an empty server { }
definition
location ^~ /foo/ { alias /var/www/foo/; }
But when I move this in a considerably bigger server definition (one used for a WordPress multi-site config), it will stop working and wordpress will respond to it (which obviously was not my intent).
I tried to put at the begining or end of server block, but this didn't change it.
How can I force Nginx to use this location?
The location directive within NGINX server block allows to route request to correct location within the file system. The directive is used to tell NGINX where to look for a resource by including files and folders while matching a location block against an URL.
~* Tilde followed by an asterisk modifier means that the location will be processed as a case-insensitive RE match. ^~ Assuming this block is the best non-RE match, a carat followed by a tilde modifier means that RE matching will not take place.
Using try_files means that you can test a sequence. If $uri doesn't exist, try $uri/ , if that doesn't exist try a fallback location.
0.0:80 (or 0.0. 0.0:8080 if Nginx is being run by a normal, non-root user). This allows these blocks to respond to requests on any interface on port 80, but this default value does not hold much weight within the server selection process. The listen directive can be set to: An IP address/port combo.
You are probably looking for break
.
location ^~ /foo/ { alias /var/www/foo/; break; }
From the HttpRewriteModule
documentation:
last
- completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.
break
- completes processing of current rewrite directives and non-rewrite processing continues within the current location block only.Note that outside location blocks, last and break are effectively the same.
Location blocks in Nginx are exclusive. If you use location ^~
then other rules probably expiry headers for static objects will not apply unless you copy those rules as nested under the same location block.
If you could share your full config then I can make it work for you. Most likely you need to use nested location blocks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With