How to make nginx to stop processing other rules a

2020-02-27 17:57发布

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?

标签: nginx
3条回答
ら.Afraid
2楼-- · 2020-02-27 18:46

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.

查看更多
唯我独甜
3楼-- · 2020-02-27 18:56

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.

查看更多
一纸荒年 Trace。
4楼-- · 2020-02-27 18:59
location = /aliasname/ { 
  alias /path/to/alias/
}

Trailing slash will be a problem if it is not present in URI. See https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms#matching-location-blocks

查看更多
登录 后发表回答