我怎样才能在NGINXconfiguration中的两个位置有相同的规则?
我怎样才能在NGINXconfiguration中的两个位置有相同的规则?
我已经尝试了以下
server { location /first/location/ | /second/location/ { .. .. } } 但nginx重新加载抛出这个错误:
 nginx: [emerg] invalid number of arguments in "location" directive** 
	
尝试
 location ~ ^/(first/location|second/location)/ { ... } 
〜意味着使用正则expression式的url。 ^手段检查从第一个字符。 这将寻找一个/其次是任一个地点,然后是另一个/。
另一种select是使用包含文件在两个前缀位置重复规则。 由于前缀位置在configuration中与位置无关,因此在稍后添加其他正则expression式位置时,使用它们可以节省一些混淆。
 server { location /first/location/ { include shared.conf; } location /second/location/ { include shared.conf; } } 
正则expression式和包含文件都是很好的方法,我经常使用这些。 但另一种select是使用命名位置,这在许多情况下是一种有用的方法,尤其是更复杂的方法。 标准的“如果是邪恶的”页面基本上显示了以下作为做事的好方法:
 error_page 418 = @common_location; location /first/location/ { return 418; } location /second/location/ { return 418; } location @common_location { # The common configuration... } 
这是一个简短而有效的方法:
 location ~ (patternOne|patternTwo){ #rules etc. } 
所以人们可以很容易地使用简单的pipe道语法指向相同的位置块/规则的多个模式。
对于inheritance情况:
 location /first { # do some stuff here, and than go to /base try_files /base; } location /second { # do some other stuff here, and than go to /base try_files /base; } location /base { # base stuff here }