简体   繁体   English

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

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

I'm trying to use nginx to reverse proxy into kubernetes pods running various web apps. 我正在尝试使用Nginx将代理反向转换为运行各种Web应用程序的kubernetes pod。 My issue is that I can only proxy when location is set to / and not /someplace 我的问题是,我只能在location设置为/而不是/someplace时代理

I know the internal IPs are working because both web apps load successfully when I use them with location / , and I can curl the webpages internally. 我知道内部IP可以正常工作,因为当我将两个IP应用程序与location /一起使用时,两个IP应用程序都可以成功加载,并且可以在内部卷曲网页。

What I would like to happen 我想发生什么

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

In this manner I could easily run all my apps on the same port. 这样,我可以轻松地在同一端口上运行所有应用程序。

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

I tried solving this by doing a rewrite of the URI like below, that resulted in a blank page loading when I tried to access /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;
  }
}

Any ideas where I messed up? 有什么想法我搞砸了吗?

The webapp I am trying to use is called RabbitMQ UI. 我尝试使用的Webapp称为RabbitMQ UI。 To try and debug my situation I curled three different URLs to see what nginx was responding with. 为了调试我的情况,我卷曲了三个不同的URL,以查看nginx响应了什么。

Curling the / loads the correct html page, and it works in the browser. 卷曲/加载正确的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 returns not found or Moved Permanently 未找到冰壶/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>

Curling the /rabbitmq/ location gives the correct page, but loads a blank in the browser. 卷曲/rabbitmq/位置可提供正确的页面,但会在浏览器中加载空白。 I think this is because the browser cannot reference the js files present in the html doc? 我认为这是因为浏览器无法引用html文档中存在的js文件?

curl http://10.82.5.80/rabbitmq/ 卷曲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>

Try adding the URL in your proxy location like this: 尝试像这样在您的代理位置中添加URL:

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

Adding a trailing / at the end of the proxy_pass forces nginx to strip the /app1 prefix from your requests while sending it to the backend. 在proxy_pass的末尾添加尾随/强制nginx在将其/app1前缀发送到后端时,从您的请求中删除。

Explanation on how it works on the official nginx page: http://nginx.org/en/docs/http/ngx_http_proxy_module.html?&_ga=1.74997266.187384914.1443061481#proxy_pass 在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