Nginx位置“不等于"正则表达式 [英] Nginx location "not equal to" regex

查看:1339
本文介绍了Nginx位置“不等于"正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在Nginx中设置一个location条件,以响应不等于所列位置的任何内容?

How do I set a location condition in Nginx that responds to anything that isn't equal to the listed locations?

我尝试过:

location !~/(dir1|file2\.php) {
   rewrite ^/(.*) http://example.com/$1 permanent;
}

但是它不会触发重定向.它仅使用其余服务器配置中的规则来处理请求的URI.

But it doesn't trigger the redirect. It simply handles the requested URI using the rules in the rest of the server configuration.

推荐答案

根据 nginx 文档

没有不匹配正则表达式的语法.相反,匹配目标正则表达式并分配一个空块,然后使用location/匹配其他任何内容

there is no syntax for NOT matching a regular expression. Instead, match the target regular expression and assign an empty block, then use location / to match anything else

因此您可以定义类似

location ~ (dir1|file2\.php) { 
    # empty
}

location / {
    rewrite ^/(.*) http://example.com/$1 permanent; 
}

这篇关于Nginx位置“不等于"正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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