如何匹配问号“?"作为 nginx.conf 位置上的正则表达式 [英] How to match question mark "?" as regexp on nginx.conf location

查看:99
本文介绍了如何匹配问号“?"作为 nginx.conf 位置上的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配问号?"作为 nginx.conf 位置上的正则表达式.

I'd like to match question mark "?" as regexp on nginx.conf location.

例如,我想要匹配的 URL 模式是/something?foo=5 或/something?bar=8(仅参数可更改).

For example, a URL pattern which I'd like to match is /something?foo=5 or /something?bar=8 (parameter only changeable).

因为nginx采用了RCPE,所以我可以在nginx.conf上写位置如下:

Because nginx adopts RCPE, I can write the location on nginx.conf as follows:

location ~ ^/something\?.* {
}

以上内容与网址格式不匹配.我该怎么做?

The above doesn't match the URL pattern. How can I do that?

另外,以下不是我的期望.

Also, the following is not my expectation.

location ~ ^/something?.* {
}

它会匹配我没想到的/something_foo_bar_buzz.

It'll match /something_foo_bar_buzz that I don't expect.

推荐答案

nginx 位置块根本不匹配查询字符串.所以这是不可能的.

nginx location block doesn't match query string at all. So it's impossible.

位置

该指令允许根据 URI 进行不同的配置.

This directive allows different configurations depending on the URI.

在 nginx 中,有一个内置变量 $uri,用于匹配位置块.例如,给出一个请求

In nginx, there is a built-in variable $uri, which the location block is matched against. For example, give a request

http://www.example.com/app/login.php?username=xyz&password=secret

$uri 值是这个字符串:

the $uri value is this string:

 /app/login.php

并且 query_string 存储在 nginx 变量 $args 中:

and the query_string is stored in nginx variable $args:

username=xyz&password=secret

做点什么.查询字符串,您可以执行类似

To do something wrt. query string, you can do something like

if ($args ~ username=xyz) {
   # do something for requests with this query string
}

但是要小心,如果是邪恶的

这篇关于如何匹配问号“?"作为 nginx.conf 位置上的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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