V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Ansen
V2EX  ›  NGINX

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

  •  2
     
  •   Ansen · 2015-08-29 10:41:09 +08:00 · 4362 次点击
    这是一个创建于 3165 天前的主题,其中的信息可能已经有所发展或是发生改变。
    内网网站地址为: 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 入手,求大家指点
    第 1 条附言  ·  2015-08-29 21:34:06 +08:00

    目前的配置文件

    http://ww4.sinaimg.cn/large/867ecea7gw1evjt493adzj20jk0bgtbs.jpg

    目前的效果是
    访问 10.10.1.10 会自动跳转到 10.10.1.10/pmc_frontend/ 下面
    我想要的效果是 直接 10.10.1.10 就行了,不用跳转

    13 条回复    2015-08-29 23:59:00 +08:00
    msg7086
        1
    msg7086  
       2015-08-29 12:07:01 +08:00   ❤️ 1
    看上去像是 Host $host 的问题。
    yaxin
        2
    yaxin  
       2015-08-29 12:32:07 +08:00   ❤️ 1
    楼主缩进能对其不,看的难受啊。

    我给你个思路,既然你的 backend 是 http 的,直接抓包, tcpdump -i ethX port 8080 ,看一下你的 backend 返回的是什么状态
    usernametoolong
        3
    usernametoolong  
       2015-08-29 12:50:36 +08:00   ❤️ 1
    proxy pass 没配正确,按这样设置需要 proxy_redirect /pmc_frontend/ /
    lhbc
        4
    lhbc  
       2015-08-29 12:56:14 +08:00   ❤️ 1
    不是 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
        5
    wql  
       2015-08-29 14:48:55 +08:00   ❤️ 1
    你多了个斜杠。
    Ansen
        6
    Ansen  
    OP
       2015-08-29 19:55:49 +08:00
    @lhbc 后端代码的访问路径我配置的是 nginx 的 ip 并没有 /pmc_frontend/ 这个目录
    另外,后端之所以有这个路径是因为 jboss 的 war 包是这名
    Ansen
        7
    Ansen  
    OP
       2015-08-29 20:08:39 +08:00
    @msg7086
    @yaxin
    @usernametoolong
    @wql
    统一回复:感谢大家,今天太忙了,没有时间上网,到现在才有空上来回复,目前问题依旧
    lhbc
        8
    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
        9
    Ansen  
    OP
       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
        10
    lhbc  
       2015-08-29 23:17:05 +08:00 via Android
    @Ansen 我在 4 楼回复的方法二和方法三。
    都是很不优雅的解决方法,而且可能存在 bug 。
    arnofeng
        11
    arnofeng  
       2015-08-29 23:22:27 +08:00   ❤️ 1
    你应该少配置了两个关键的参数 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
        12
    arnofeng  
       2015-08-29 23:24:06 +08:00
    location 中少加了一个 proxy_pass https://lushannyc.wordpress.com/;
    geekzu
        13
    geekzu  
       2015-08-29 23:59:00 +08:00   ❤️ 1
    是 host 的问题吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3806 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:08 · PVG 13:08 · LAX 22:08 · JFK 01:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.