簡體   English   中英

如何使用proxy_pass重寫URI nginx反向代理?

[英]How to rewrite URI nginx reverse proxy using proxy_pass?

我正在嘗試使用Nginx將代理反向轉換為運行各種Web應用程序的kubernetes pod。 我的問題是,我只能在location設置為/而不是/someplace時代理

我知道內部IP可以正常工作,因為當我將兩個IP應用程序與location /一起使用時,兩個IP應用程序都可以成功加載,並且可以在內部卷曲網頁。

我想發生什么

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>

未找到冰壺/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>

嘗試像這樣在您的代理位置中添加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:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM