V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wico97
V2EX  ›  问与答

反代域名 302 怎么办

  •  
  •   wico97 · 2017-08-04 02:11:00 +08:00 · 2216 次点击
    这是一个创建于 2456 天前的主题,其中的信息可能已经有所发展或是发生改变。

    被反代域名 c.emnoffers.com 会 302 跳转到其他域名,我想用 nginx 反代这个域名,总是出现 502 错误,不知道如何解决 server { listen 80; server_name jc.jattdjmaza.com; location / {

        resolver 8.8.8.8;
        proxy_pass https://c.emnoffers.com;  
        proxy_set_header referer https://c.emnoffers.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host c.emnoffers.com;
        proxy_set_header Accept-Encoding "";
    
        }
    

    } 我理想的结果是用我的域名反代 c.emnoffers.com 以及跳转的域名。请 v 友帮忙解决

    16 条回复    2017-08-04 05:05:43 +08:00
    ryd994
        1
    ryd994  
       2017-08-04 02:33:32 +08:00 via Android
    proxy_redirect
    wico97
        2
    wico97  
    OP
       2017-08-04 02:36:50 +08:00
    @ryd994 proxy_redirect 我测试了几个写法都不对。proxy_redirect off; proxy_redirect ~^http://(.+) http://$1.jattdjmaza.com;
    类似的还是 502 出错。我测试如果被反代的域名 301 用 proxy_redirect 是没问题的。但 302 跳转的就是不行。
    cxbig
        3
    cxbig  
       2017-08-04 02:50:37 +08:00
    你这个域名直接访问就是 302,没毛病。要加 upstream_http_location 配置才能跟 302 的跳转。
    给你搜到一个参考:
    https://gist.github.com/sirsquidness/710bc76d7bbc734c7a3ff69c6b8ff591
    ryd994
        4
    ryd994  
       2017-08-04 03:12:13 +08:00
    @cxbig 这代理的就是根路径,没毛病

    @wico97 RTFM …又不是 rewrite
    无脑试试
    proxy_redirect default;
    不行的话
    proxy_redirect http://c.emnoffers.com/ https://jc.jattdjmaza.com/;
    ryd994
        5
    ryd994  
       2017-08-04 03:16:06 +08:00
    @cxbig 你给的链接里,把 302 分出去是为了缓存跳转结果
    但是他这样做明显是有坑的,也许在他的使用场景下没事
    但是他根本没有考虑 Cookies
    wico97
        6
    wico97  
    OP
       2017-08-04 03:19:39 +08:00
    @ryd994 你这两个都试了。不行

    @cxbig http://pureage.info/2014/08/25/hiding-302-using-proxy-pass.html 这个文章好像跟我的需求很像,但看不懂 rewrite_by_lua 那部分
    ryd994
        7
    ryd994  
       2017-08-04 03:23:15 +08:00
    @wico97 你不需要隐藏 302 也能保证正常访问……
    curl -v 一下,看 log
    wico97
        8
    wico97  
    OP
       2017-08-04 03:25:53 +08:00
    @ryd994 root@tmt:~# curl -v c.emnoffers.com
    * Rebuilt URL to: c.emnoffers.com/
    * Hostname was NOT found in DNS cache
    * Trying 54.76.181.151...
    * Connected to c.emnoffers.com (54.76.181.151) port 80 (#0)
    > GET / HTTP/1.1
    > User-Agent: curl/7.35.0
    > Host: c.emnoffers.com
    > Accept: */*
    >
    < HTTP/1.1 302 Found
    < Content-Type: text/html; charset=utf-8
    < Date: Thu, 03 Aug 2017 19:24:49 GMT
    < Location: http://karonty.com/36G85/KHSr/JnC7/fjzsKPhsPW4Cg201ay95yb9jHU2ypw7d0j-y5MGawkd8BhD4xKfw?LH0=WW_MS2&clickid=&af=-1
    < Set-Cookie: sid=HuGhrIb0/BONywj1dnu38ryLQmZ7gUjLcuzxA3zlb3MWYo87k359ObmZW2t1TKnrF/mtyCBQP545HGTY+33TOylA+liQcbvo; Domain=.emnoffers.com; HttpOnly
    < Set-Cookie: trk=BanjztBZBeHUb6XkwdpVuEiR0n2sm4EMrVakcwnhXpaBo5aHJ+NVjbmZW2t1TKnrF/mtyCBQP545HGTY+33TOylA+liQcbvo; Domain=.emnoffers.com; Expires=Tue, 02 Aug 2022 19:24:49 GMT; HttpOnly
    < Content-Length: 144
    <
    <a href="http://karonty.com/36G85/KHSr/JnC7/fjzsKPhsPW4Cg201ay95yb9jHU2ypw7d0j-y5MGawkd8BhD4xKfw?LH0=WW_MS2&amp;clickid=&amp;af=-1">Found</a>.

    * Connection #0 to host c.emnoffers.com left intact
    可是我就想反代呀
    cxbig
        9
    cxbig  
       2017-08-04 03:27:16 +08:00 via iPad
    @wico97 nginx 有支持 lua 的模块,用 lua 语言来写复杂的逻辑
    那一句等同于 / => /proxy-to$request_uri
    ryd994
        10
    ryd994  
       2017-08-04 03:29:16 +08:00
    你这是要加
    proxy_redirect http://karonty.com/ 你的 karonty 域名

    proxy_redirect 就是个字符替换而已,别多想了
    proxy_redirect ~^http://(.+) http://$1.jattdjmaza.com;
    表面上似乎可以,但是你考虑 DNS 了么?还有你 nginx 是想被人当作公开代理么?
    wico97
        11
    wico97  
    OP
       2017-08-04 03:34:42 +08:00
    @ryd994 proxy_redirect http://karonty.com/ 你的 karonty 域名 这个我也早就测试过了。一直都是 502 错误。
    ryd994
        12
    ryd994  
       2017-08-04 03:44:44 +08:00
    @wico97 502 的 curl log 呢?
    502 是谁给的?你的服务还是对方服务?在哪个域名上?
    curl log 一看就知道
    wico97
        13
    wico97  
    OP
       2017-08-04 03:49:56 +08:00
    @ryd994 root@tmt:~# curl -I jc.jattdjmaza.com
    HTTP/1.1 502 Bad Gateway
    Server: nginx
    Date: Thu, 03 Aug 2017 19:43:24 GMT
    Content-Type: text/html
    Content-Length: 166
    Connection: keep-alive

    是我的反代域名,一直 502.测试了很多方法
    ryd994
        14
    ryd994  
       2017-08-04 03:58:49 +08:00
    @wico97 你这没跳转啊……
    那 nginx error.log 说什么?看看谁给的 502
    wico97
        15
    wico97  
    OP
       2017-08-04 05:04:42 +08:00
    @ryd994 我把 https 变成 http 就可以访问了。谢谢
    wico97
        16
    wico97  
    OP
       2017-08-04 05:05:43 +08:00
    @cxbig local _, _, upstream_http_location = string.find(ngx.var.upstream_http_location, "^http:/(.*)$") 请问这句 lua 写的用 nginx 怎么写
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4146 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:20 · PVG 13:20 · LAX 22:20 · JFK 01:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.