在url中创建nginx检查参数

use*_*277 17 parameters arguments nginx

我想检查nginx中的url中是否存在参数,然后重写.我可以这样做吗?

例如,如果url http://website.com/?mobile然后将用户重定向到http://m.website.com

Ale*_*Ten 33

你最好使用http://example.com/?mobile=1(带有值的参数).在这种情况下,检查很简单:

if ($arg_mobile) {
    return 302 http://m.example.com/;
}
Run Code Online (Sandbox Code Playgroud)

检查参数存在通常使用regexp来完成,if ($args ~ mobile)但它容易出错,因为它会匹配mobile任何地方,例如http://example.com/?tag=automobile.

  • @raksja 尝试在“if”之后添加空格 (2认同)