如何使用proxy_pass重写URI nginx反向代理? [英] How to rewrite URI nginx reverse proxy using proxy_pass?
问题描述
我正在尝试使用nginx将代理反向转换为运行各种Web应用程序的kubernetes pod.我的问题是,仅当位置设置为/
而不是/someplace
I'm trying to use nginx to reverse proxy into kubernetes pods running various web apps. My issue is that I can only proxy when location is set to /
and not /someplace
我知道内部IP正在工作,因为当我将它们与location /
一起使用时,两个Web应用程序均成功加载,并且可以在内部卷曲网页.
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.
我想发生的事情
http://ServerIP/app1 路由到 http://Pod1IP:3000 http://ServerIP/app2 路由到通过这种方式,我可以轻松地在同一端口上运行所有应用程序.
In this manner I could easily run all my apps on the same port.
我相信正在发生的事情 http://ServerIP/app1 -> httpL//Pod1IP:3000/app1
What I believe is happening http://ServerIP/app1 --> httpL//Pod1IP:3000/app1
我尝试通过重写如下所示的URI来解决此问题,当我尝试访问/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
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?
我要使用的Web应用程序称为RabbitMQ UI.为了尝试调试我的情况,我卷曲了三个不同的URL,以查看nginx响应了什么.
The webapp I am trying to use is called RabbitMQ UI. To try and debug my situation I curled three different URLs to see what nginx was responding with.
卷曲/
会加载正确的html页面,并且可以在浏览器中使用.
Curling the /
loads the correct html page, and it works in the browser.
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
找不到返回或已永久移动
Curling /rabbitmq
returns not found or Moved Permanently
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文件?
Curling the /rabbitmq/
location gives the correct page, but loads a blank in the browser. I think this is because the browser cannot reference the js files present in the html doc?
curl 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,如下所示:
Try adding the URL in your proxy location like this:
location = /app1/ {
proxy_pass http://10.82.5:80/;
}
在proxy_pass的末尾添加尾随/
会强制nginx从请求中剥离/app1
前缀,同时将其发送到后端.
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.
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
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:
这篇关于如何使用proxy_pass重写URI nginx反向代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!