【问题标题】:How to rewrite URI nginx reverse proxy using proxy_pass?如何使用 proxy_pass 重写 URI nginx 反向代理?
【发布时间】:2018-07-01 20:58:09
【问题描述】:

我正在尝试使用 nginx 将代理反向代理到运行各种 Web 应用程序的 kubernetes pod。我的问题是我只能在位置设置为/ 而不是/someplace 时代理

我知道内部 IP 可以正常工作,因为当我将它们与 location / 一起使用时,这两个 Web 应用程序都可以成功加载,并且我可以在内部卷曲网页。

我希望发生什么

http://ServerIP/app1 路由到http://Pod1IP:3000 http://ServerIP/app2 路由到http://Pod2IP:80

通过这种方式,我可以轻松地在同一个端口上运行我的所有应用程序。

我相信正在发生的事情 http://ServerIP/app1 --> httpL//Pod1IP:3000/app1

我尝试通过重写 URI 来解决这个问题,如下所示,当我尝试访问 /app1 时导致加载空白页面

server {
listen 80;

location = /app1/ {
  rewrite ^.*$ / break;
  proxy_pass http://10.82.5.80:80;
  }
location / {
  proxy_pass http://10.106.228.213:15672;
  }
}

有什么我搞砸的想法吗?

我尝试使用的 webapp 称为 RabbitMQ UI。为了尝试调试我的情况,我整理了三个不同的 URL,以查看 nginx 的响应。

卷曲 / 会加载正确的 html 页面,并且可以在浏览器中运行。

curl http://10.82.5.80/

<!doctype html>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<html>
  <head>
    <title>RabbitMQ Management</title>
    <script src="js/ejs-1.0.min.js" type="text/javascript"></script>
    <script src="js/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script src="js/jquery.flot-0.8.1.min.js" type="text/javascript"></script>
    <script src="js/jquery.flot-0.8.1.time.min.js" type="text/javascript"></script>
    <script src="js/sammy-0.7.6.min.js" type="text/javascript"></script>
    <script src="js/json2-2016.10.28.js" type="text/javascript"></script>
    <script src="js/base64.js" type="text/javascript"></script>
    <script src="js/global.js" type="text/javascript"></script>
    <script src="js/main.js" type="text/javascript"></script>
    <script src="js/prefs.js" type="text/javascript"></script>
    <script src="js/formatters.js" type="text/javascript"></script>
    <script src="js/charts.js" type="text/javascript"></script>

    <link href="css/main.css" rel="stylesheet" type="text/css"/>
    <link href="favicon.ico" rel="shortcut icon" type="image/x-icon"/>

<!--[if lte IE 8]>
    <script src="js/excanvas.min.js" type="text/javascript"></script>
    <link href="css/evil.css" rel="stylesheet" type="text/css"/>
<![endif]-->
  </head>
  <body>
    <div id="outer"></div>
    <div id="debug"></div>
    <div id="scratch"></div>
  </body>
</html>

Curling /rabbitmq 返回未找到或永久移动

curl http://10.82.5.80/rabbitmq

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>

卷曲/rabbitmq/ 位置会提供正确的页面,但会在浏览器中加载空白。我认为这是因为浏览器无法引用 html 文档中存在的 js 文件?

卷曲http://10.82.5.80/rabbitmq/

<!doctype html>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<html>
  <head>
    <title>RabbitMQ Management</title>
    <script src="js/ejs-1.0.min.js" type="text/javascript"></script>
    <script src="js/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script src="js/jquery.flot-0.8.1.min.js" type="text/javascript"></script>
    <script src="js/jquery.flot-0.8.1.time.min.js" type="text/javascript"></script>
    <script src="js/sammy-0.7.6.min.js" type="text/javascript"></script>
    <script src="js/json2-2016.10.28.js" type="text/javascript"></script>
    <script src="js/base64.js" type="text/javascript"></script>
    <script src="js/global.js" type="text/javascript"></script>
    <script src="js/main.js" type="text/javascript"></script>
    <script src="js/prefs.js" type="text/javascript"></script>
    <script src="js/formatters.js" type="text/javascript"></script>
    <script src="js/charts.js" type="text/javascript"></script>

    <link href="css/main.css" rel="stylesheet" type="text/css"/>
    <link href="favicon.ico" rel="shortcut icon" type="image/x-icon"/>

<!--[if lte IE 8]>
    <script src="js/excanvas.min.js" type="text/javascript"></script>
    <link href="css/evil.css" rel="stylesheet" type="text/css"/>
<![endif]-->
  </head>
  <body>
    <div id="outer"></div>
    <div id="debug"></div>
    <div id="scratch"></div>
  </body>
</html>

【问题讨论】:

    标签: javascript nginx proxy rabbitmq kubernetes


    【解决方案1】:

    尝试在您的代理位置添加 URL,如下所示:

    location = /app1/ {
      proxy_pass http://10.82.5:80/;
    }
    

    在 proxy_pass 末尾添加尾随 / 会强制 nginx 在将请求发送到后端时从您的请求中去除 /app1 前缀。

    nginx官方页面上的解释:http://nginx.org/en/docs/http/ngx_http_proxy_module.html?&_ga=1.74997266.187384914.1443061481#proxy_pass

    If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
    

    【讨论】:

    • 不是你要代理的地址吗?我从你的问题中得到它
    • 是的,我明白了,但您能澄清一下删除 url 重写将如何解决我的问题吗? location = /app1/ { rewrite ^.*$ / break; proxy_pass http://10.82.5.80:80; }
    • 对不起,忘了说我的sn-p的意思了。更新了我的答案
    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2019-05-03
    • 2020-12-18
    • 2019-12-22
    相关资源
    最近更新 更多