如何在 NGINX 配置中对两个位置使用相同的规则? [英] How can I have same rule for two locations in NGINX config?

查看:18
本文介绍了如何在 NGINX 配置中对两个位置使用相同的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 NGINX 配置中对两个位置使用相同的规则?

How can I have same rule for two locations in NGINX config?

我已经尝试了以下

server {
  location /first/location/ | /second/location/ {
  ..
  ..
  }
}

但是 nginx reload 抛出了这个错误:

but nginx reload threw this error:

nginx: [emerg] invalid number of arguments in "location" directive**

推荐答案

尝试

location ~ ^/(first/location|second/location)/ {
  ...
}

~ 表示对 url 使用正则表达式.^ 表示从第一个字符开始检查.这将查找 / 后跟其中一个位置,然后是另一个 /.

The ~ means to use a regular expression for the url. The ^ means to check from the first character. This will look for a / followed by either of the locations and then another /.

这篇关于如何在 NGINX 配置中对两个位置使用相同的规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆