【问题标题】:How to rewrite URL in NGINX with location parameter?如何使用位置参数在 NGINX 中重写 URL?
【发布时间】:2021-12-08 09:31:07
【问题描述】:

我在网络服务器上有一个应用程序(Openproject)。 这是在http://10.0.0.1:8000/下可达的

在我的用户和网络服务器后面是一个 NGinx,我需要在一个特定的 URL 下发布它:https://ngrp.com/openproject

所以我在我的 Nginx 配置中进行了以下更改(在这个 NGINX 实例中,多个网站使用“位置”设置发布):

  location /openproject/ {
   proxy_pass             http://10.0.0.1:8000/;
   include                /etc/nginx/conf.d/proxy.conf;
 }

但是当我通过 Reverseproxy 打开页面时,Webbrowser 只显示一个白页。


在 Webbrowser 调试器中,我看到某些路径是错误的,因此浏览器无法加载它。示例:

https://ngrp.com/assets/frontend/styles.c3a5e7705d6c5db9cfc1.css (URL 中缺少 /openproject/)

正确的是:

https://ngrp.com/openproject/assets/frontend/styles.c3a5e7705d6c5db9cfc1.css

那么有人可以告诉我,需要什么配置,这样我就可以在 URL https://ngrp.com/openproject/ 下成功打开项目了吗?

非常感谢。

【问题讨论】:

    标签: nginx nginx-reverse-proxy nginx-location openproject


    【解决方案1】:

    当您 proxy_pass 时,您代理了整个 HTTP 请求,这意味着您实际上是在 http://10.0.0.1:8000/ 上请求 GET /openproject 而不仅仅是 GET /

    您可以在 proxy_pass 之前添加此行来解决此问题并删除 /openproject 前缀:

    rewrite /openproject/(.*) /$1 break;

    这会将请求的 URL 从 /openproject/... 更改为 /...

    【讨论】:

    • 您好 Misfits09,首先非常感谢您的回答。不幸的是它没有用:我在 NGINX 中放置了以下 Configpart: location /openproject/ { rewrite /openproject/(.*) /$1 break; proxy_pass 10.0.0.1:8000;包括/etc/nginx/conf.d/proxy.conf;但同样的问题存在。 ngrp.com/assets/frontend/styles.c3a5e7705d6c5db9cfc1.css(/openproject/ 在 URL 中丢失)您还有其他想法吗?
    猜你喜欢
    • 2014-12-06
    • 2020-05-26
    • 2018-10-29
    • 1970-01-01
    • 2013-12-16
    • 2012-10-12
    • 2014-02-18
    • 1970-01-01
    • 2015-12-20
    相关资源
    最近更新 更多