【问题标题】:Nginx: location regex not workingNginx:位置正则表达式不起作用
【发布时间】:2015-04-08 21:52:29
【问题描述】:

我的正则表达式遇到问题,不知道它有什么问题。它返回一个带有orls/f 而不是orls/f?p=4550 的URi。当我通过https://secure.toto02.com/orls/myservice/f?p=4550

我的 conf 文件在下面

 location ~ "^/([a-zA-Z]+)/myservice/(.+)$" {

    error_log  /var/log/nginx/error-server.log notice;
    rewrite_log on;  

    #proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;


    proxy_pass http://192.168.3.45:8080/orls/$2;    
    proxy_redirect http://192.168.3.45:8080/orls/ https://secure.toto02.com/$1/myservice/ ;          
}   

谁能帮忙?

【问题讨论】:

  • 我在您当前的正则表达式中使用了转义字符 `\`,我可以看到它正确地捕获了您想要的组。 regex101.com/r/vI6rG3/1。有什么问题?
  • 我的问题是从第二次捕获它似乎只是捕获f 而不是f?p=4550。我使用了你提出的,但它仍然无法正常工作
  • 你确定这是一个正则表达式问题吗?难道不是nginx的位置不包括查询吗? (我无法从文档中找到。)您可以尝试“硬编码”第二个捕获组(类似于 f\?p=4550),看看会发生什么......
  • 试试if ($args ~ p=4550) { rewrite ^ http://example.com/ permanent; }

标签: regex nginx proxypass


【解决方案1】:

捕获位置不会包含查询字符串。您必须通过 $is_args$args 变量手动包含它,如下所示:

proxy_pass http://192.168.3.45:8080/orls/$2$is_args$args;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多