Nginx:正确设置 proxy_path。网址问题

发布于 2024-11-15 03:49:45 字数 903 浏览 4 评论 0原文

我有几台服务器在虚拟机中运行。我有一个运行 nginx 来代理对这些服务器的请求。例如, http://mydomain.com/wiki 应该将请求代理到 http://192.168.122.3。 192.168.122.3 运行 apache,DocumentRoot = /var/www/wiki。一切正常,但是当页面加载时,所有路径都指向 / 而不是 /wiki (例如 ,而不是 /wiki/bla-bla- bla")。这对于 apache 来说似乎没问题,但对于浏览器则不然——它无法加载图像等。 将 apache 中的 DocumentRoot 更改为 /var/www 并从 nginx 传递 /wiki 没有帮助 - mediawiki 开始重定向到 /wiki,nginx 将其重定向回 apache 等等,然后再说明循环重定向。 在 VM 中运行的其他项目(例如 Redmine(Ruby on Rails))的行为相同。

有什么办法可以解决这个问题吗?是否有正确的方法将 /subdir 等 URL 代理到其他服务器?

这是 nginx 服务器部分:

server {
    listen   192.168.122.7:80;
    server_name  mydomain.com;

    access_log  /var/log/nginx/localhost.access.log;

    location / {

    }
    location /wiki/ {
        proxy_pass http://192.168.122.3/;
    }
}

I have several servers running in virtual machines. And I have one running nginx for proxying requests to those servers. For example, http://mydomain.com/wiki should proxy request to http://192.168.122.3. 192.168.122.3 runs apache with DocumentRoot = /var/www/wiki. Everything works fine, but when page loads, all the paths there point to / instead of /wiki (like <img src="/bla-bla-bla"/>, not /wiki/bla-bla-bla"). It seems to be ok for apache, but not for browser - it can't load images etc.
Changing DocumentRoot in apache to /var/www and passing /wiki from nginx doesn't help - mediawiki starts redirection to /wiki, nginx redirects it back to apache and so on before it says about looped redirections.
The other projects runing in VMs like Redmine (ruby on rails) act the same.

Is there any way to solve this problem? Is there proper way to proxy URLs like /subdir to other servers?

This is nginx server section:

server {
    listen   192.168.122.7:80;
    server_name  mydomain.com;

    access_log  /var/log/nginx/localhost.access.log;

    location / {

    }
    location /wiki/ {
        proxy_pass http://192.168.122.3/;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2024-11-22 03:49:45

您应该使用 proxy_redirect 指令。

location /wiki/ {
    proxy_pass http://192.168.122.3/;
    proxy_redirect default;
}

Your should use proxy_redirect directive.

location /wiki/ {
    proxy_pass http://192.168.122.3/;
    proxy_redirect default;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文