【问题标题】:nginx rewrite WITHOUT change urlnginx重写而不更改url
【发布时间】:2013-02-25 16:47:00
【问题描述】:

我想在我的 nginx 服务器中使用重写功能。

当我尝试“http://www.example.com/1234”时,我想重写“http://www.example.com/v.php?id=1234”并想在浏览器中获取“http://www.example.com/1234”。

这里是 nginx.conf 文件

...
  location ~ /[0-9]+ {
      rewrite "/([0-9]+)" http://www.example.com/v.php?id=$1 break;
  }
...

当我尝试“http://www.example.com/1234”时

我想……

url bar in browser : http://www.example.com/1234
real url : http://www.example.com/v.php?id=1234

但我有麻烦了……

url bar in browser : http://www.example.com/v.php?id=1234
real url : http://www.example.com/v.php?id=1234

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    参考:http://wiki.nginx.org/HttpRewriteModule#rewrite

    如果替换字符串以 http:// 开头,则客户端将被重定向,并且任何进一步的 >rewrite 指令都将终止。

    所以删除 http:// 部分,它应该可以工作:

    location ~ /[0-9]+ {
            rewrite "/([0-9]+)" /v.php?id=$1 break;
    }
    

    【讨论】:

    • 我需要使用“last”标志而不是“break”,因为重写的 url 由不同的位置处理。
    • 如果我想使用 rewrite 在另一个端口代理_pass 服务怎么办?
    【解决方案2】:

    就我而言,我需要使用“最后一个”来使其工作,因为我还有其他想要应用的规则:

    location ~ /[0-9]+ {
        rewrite "/([0-9]+)" /v.php?id=$1 last;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-03
      • 1970-01-01
      • 2023-03-07
      • 2015-02-18
      • 1970-01-01
      • 2017-03-13
      • 2018-11-11
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多