【问题标题】:nginx rewrite url problem need help [closed]nginx重写url问题需要帮助[关闭]
【发布时间】:2011-09-10 03:43:30
【问题描述】:

我是 nginx 新手,我已经安装了 php(使用端口 8888)和 nginx(使用端口 80),并且我有一个静态 html 页面,如 www.aa.com:8888/news/html/about/aboutus /index.html,现在我想用 www.aa.com/test/aboutus.html 来访问它,这样做时,浏览器中的 URL 应该不会改变。这个怎么做?

我尝试了一些方法,但浏览器中的链接只会重定向到新的 URL 字符串...

我在 nginx 的 conf 文件中的示例配置如下:

    location ^~ /test/aboutus.html {
        proxy_pass   http://127.0.0.1:8888;
        proxy_redirect   default;
        rewrite ^/test/aboutus.html$ /news/html/about/aboutus last;
        break;
    }

非常感谢!

【问题讨论】:

    标签: proxy nginx rewrite


    【解决方案1】:

    没有其他人回答,所以我想我会试一试。

    根据the documentation for proxy_pass

    一种特殊情况是在 proxy_pass 语句中使用变量:请求的 URL 未被使用,您完全负责自己构建目标 URL。

    因此,如果您可以在其中获取变量,则只需提供完整的 URL,而不必担心 rewrite。像这样的:

    location /test/aboutus.html {
        proxy_pass $scheme://127.0.0.1:8888/news/html/about/aboutus;
    }
    

    我没有测试过这个(抱歉)。

    [更新]

    还有一个想法:

    location /test/aboutus.html {
        proxy_pass $scheme://127.0.0.1:8888/news/html/about/aboutus;
        proxy_redirect $scheme://127.0.0.1:8888/news/html/about/aboutus $scheme://$host/test/aboutus.html
    }
    

    这个想法是将回复中的 Location 标头映射回您希望客户端在浏览器中显示的内容。这就是proxy_redirect 的用途(尽管我认为“默认”设置应该有效)。

    【讨论】:

    • 感谢您的回复!我测试过,在我的本地环境中,浏览器中的 URL 只会从 localhost/test/aboutus.html 更改为 127.0.0.1:8888/news/html/about/aboutus ,不确定这是否是我的 nginx 环境问题......我只是希望 URL 根本不会改变......
    • 真的吗?即使没有rewrite 规则?这很奇怪……
    • 是的,完全复制您上面写的内容。我正在 windows xp 上进行测试。
    • 我添加了一个更新。很抱歉,我无法对此进行测试...
    • 尼莫,非常感谢您的热心帮助。我也测试了你的更新,可惜浏览器中的网址还是变了,我在centOS下测试,同样的症状……:(
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多