Nginx位置正则表达式 [英] nginx location regex

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

问题描述

我正在设置nginx,我需要使用location指令来匹配所有以/site1开头并以.php结尾的请求.我用什么正则表达式来做到这一点?

I'm setting up nginx and I need to use the location directive to match all requests that begin with /site1 AND end with .php. What regex do I use to do this?

我知道我可以使用^来匹配字符串的开头,并使用$来匹配字符串的结尾.因此,我可以将字符串的开头或字符串的结尾匹配为^(/site)|(\.php)$.有任何'and'运算符,所以我可以写类似^(/site)&(\.php)$的东西吗?

I know I can use ^ to match the beginning of the string and $ to match the end of string. So I'm able to match the beginning of the string OR the end of the string as ^(/site)|(\.php)$. Is there any 'and' operator so I can write something like ^(/site)&(\.php)$ ?

推荐答案

您要这样做:^(/site).*(\.php)$.这意味着,您首先匹配开头,后跟"/site",然后匹配任意次数,最后匹配".php",然后匹配字符串的末尾.如果您不需要捕获,则它会更简单:^/site.*\.php$.

You want this: ^(/site).*(\.php)$. This means that first you match the beginning followed by "/site", then anything any number of times, and finally ".php" followed by the end of the string. If you don't need the captures, it would be simpler as: ^/site.*\.php$.

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

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