使用重写设置 nginx 反向代理以应用于站点中的所有链接

ada*_*aml 6 rewrite nginx reverse-proxy

我有以下 Nginx 配置:

server {
    listen 80;

    location /test {
        rewrite /test(.*) /$1 break;
        proxy_pass "http://www.example.com/";
    }
}
Run Code Online (Sandbox Code Playgroud)

这适用于主页,当输入“ http://localhost/test ”时,我得到了http://www.example.com/的内容,而 URL 仍然是“ http://localhost/test ”。

很酷,但是当我点击网站中的链接时,说/more_examples我希望将链接重写为: http://localhost/test/more_examples。在实践中,我得到以下信息:http://localhost/more_examples,这显然不起作用。这甚至可能吗?

任何帮助将不胜感激。

unN*_*med 1

我已经测试了您的代码,对我来说,这就像您所描述的那样:

location /test {
  rewrite /test(.*) $1 break;
  proxy_pass http://www.basicwebsiteexample.com;
}
Run Code Online (Sandbox Code Playgroud)

我调用http://localhost/test和 来获取这样的子页面http://localhost/test/media

我的浏览器中没有任何重定向或 URL 重写。

如果您的浏览器中仍然收到 URL 重定向,则您正在代理的网站或 nginx 配置中一定有重定向。