请教一个 nginx 反向代理的问题

2015-08-29 10:41:09 +08:00
 Ansen
内网网站地址为: 192.168.1.242:8080/pmc_frontend/
nginx 服务器地址: 10.10.1.10
希望达到 用户浏览器地址访问 10.10.1.10:80 而实际是访问后面的 192.168.1.242:8080/pmc_frontend/
我的配置如下



但是实际访问中:键入 10.10.1.10 浏览器会 302 到 10.10.1.10/pmc_frontend/ 从而提示 404

爬了一阵子文章 感觉 应该是从 proxy_redirect 入手,求大家指点
4367 次点击
所在节点    NGINX
13 条回复
msg7086
2015-08-29 12:07:01 +08:00
看上去像是 Host $host 的问题。
yaxin
2015-08-29 12:32:07 +08:00
楼主缩进能对其不,看的难受啊。

我给你个思路,既然你的 backend 是 http 的,直接抓包, tcpdump -i ethX port 8080 ,看一下你的 backend 返回的是什么状态
usernametoolong
2015-08-29 12:50:36 +08:00
proxy pass 没配正确,按这样设置需要 proxy_redirect /pmc_frontend/ /
lhbc
2015-08-29 12:56:14 +08:00
不是 nginx 的问题

问题在于:
后端的 uri 是 /pmc_frontend/
而你要反代到 /

后端生成的代码,含有 跳转到绝对路径 的代码
比如 302 /pmc_frontend/login

浏览器向 nginx 请求 /pmc_frontend/login 的时候, nginx 就会向后端 请求 /pmc_frontend/pmc_frontend/login
当然产生 404

另外,后端所有绝对路径的资源,比如
/pmc_frontend/img/logo.gif
/pmc_frontend/style.css
都会出错,因为所有经过 nginx 进行反代的路径,都被你重写了

解决办法:
方法一: nginx 反代 /pmc_frontend 目录,不要反代 /
方法二: nginx 对后端产生的代码进行过滤,过滤掉所有 pmc_frontend/ ,但是否存在误杀,这个你要找开发了
方法三:修改后端代码。这个估计开发会把你砍了
wql
2015-08-29 14:48:55 +08:00
你多了个斜杠。
Ansen
2015-08-29 19:55:49 +08:00
@lhbc 后端代码的访问路径我配置的是 nginx 的 ip 并没有 /pmc_frontend/ 这个目录
另外,后端之所以有这个路径是因为 jboss 的 war 包是这名
Ansen
2015-08-29 20:08:39 +08:00
@msg7086
@yaxin
@usernametoolong
@wql
统一回复:感谢大家,今天太忙了,没有时间上网,到现在才有空上来回复,目前问题依旧
lhbc
2015-08-29 20:59:03 +08:00
@Ansen
反代和后端的路径不一致,就必然产生 html 里的绝对路径错误 的问题

location / {
proxy_pass http://jboss_server/pmc_frontend/;
.
.
.
}

你这样配置,访问 http://10.10.1.10/ 的时候,实际访问的是 http://jboss_server/pmc_frontend/



类似这种前后端目录不对应的反代,只有后端完全没有绝对路径的情况下才会正常
后端只要含有绝对路径,比如 /img /js /css /static 等,全都会出错


你看下日志和返回的 html 源码就能确定是不是这个问题了


正确的配置应该是

location /pmc_frontend/ {
proxy_pass http://jboss_server/pmc_frontend/;
.
.
.
}
Ansen
2015-08-29 21:02:53 +08:00
@lhbc 嗯 对的 如果这样
location /pmc_frontend/ {
proxy_pass http://jboss_server/pmc_frontend/;
.
.
.
}
假如我首页是 index
那样浏览器就是访问 http://10.10.1.10/pmc_frontend/index

问题是我想做到 http://10.10.1.10/

要怎么做呢? 也就是把 http://10.10.1.10/pmc_frontend/index 变成 http://10.10.1.10/index
lhbc
2015-08-29 23:17:05 +08:00
@Ansen 我在 4 楼回复的方法二和方法三。
都是很不优雅的解决方法,而且可能存在 bug 。
arnofeng
2015-08-29 23:22:27 +08:00
你应该少配置了两个关键的参数 proxy_redirect 、 proxy_set_header
分享一个实际使用中的反向代理
server {
listen 80;
server_name www.lushannyc.com;
location / {
proxy_redirect https://lushannyc.wordpress.com/ /;
proxy_set_header Host "lushannyc.wordpress.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header Accept-Encoding "";
proxy_set_header User-Agent $http_user_agent;
proxy_set_header Accept-Language "zh-CN";
}
}
arnofeng
2015-08-29 23:24:06 +08:00
location 中少加了一个 proxy_pass https://lushannyc.wordpress.com/;
geekzu
2015-08-29 23:59:00 +08:00
是 host 的问题吧

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/216876

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX