nginx 配置文件中如何写两个条件“且”的关系?

nginx 配置文件中不能随意嵌套 if,而且 if 中也没有 and 和 or 逻辑,不过可以借用变量来实现:

set $a "";
if ($request_uri ~* "action=postreview") 
{
   set $a "${a}1";
}
if ($http_referer !~* "^https?://([^/]+\.)?domains\.com([:/?#]|$)") 
{
   set $a "${a}1"; 
}
if ($a = '11')
{
    return 403;
}

fastcgi...

上例中实现了 url 中包含 action=postreview 且来源不是本站的情况拒绝访问。

一般放在 fastcgi 所在 location,譬如宝塔面板的 enable-php-xx.conf 文件中。

xoyozo 2 小时前
转载请注明出处
可能相关的内容