Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make nginx to stop processing other rules and serve a specific location?

Tags:

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?

like image 213
sorin Avatar asked May 22 '12 09:05

sorin


People also ask

What is location block in nginx?

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.

What does ~* mean in nginx?

~* 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.

What does try_files do in nginx?

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.

What does 80 mean in nginx?

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.


2 Answers

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.

like image 68
rivers Avatar answered Sep 20 '22 16:09

rivers


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.

like image 30
Sohail Ahmed Avatar answered Sep 23 '22 16:09

Sohail Ahmed