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

这个 proxy_redirect 是做什么的?

  •  
  •   whx20202 · 2016-09-11 10:45:09 +08:00 · 14639 次点击
    这是一个创建于 2785 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想弄一个反向代理到 HTTPS 的网站,弄好之后我发现: 1.反向代理到我们学校 BBS ,( HTTP 的)就没事 2.反代到 github 百度盘这种 https 的就有问题

    access.log 报 301 记录, chrome 说 too many redirect

    我不会抓包,网上也没类似的解决方案(主要是 access.log 好像只记录我到代理服务器的内容,不记录代理服务器和 github 之间的内容?)

    于是我就找了别人的正确的配置文件一行一行比较,发现有个: proxy_redirect off;

    设置好了之后就可以访问 github 了,但是浏览器地址栏变成github了,相当于我直接访问,不是代理的意思了(我本身就能访问 github ),

    配置文件如下:

    http {

        server {
        listen  8080 default backlog=2048;
        listen  443 ssl;
    
        server_name  127.0.0.1;  
    
        ssl_certificate /etc/nginx/xxxxx.crt;
        ssl_certificate_key /etc/nginx/xxxx.key;
    
        location /proxy/git/ {   
            proxy_pass https://github.com/;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
            proxy_pass_header Server;
    
    
            proxy_redirect off;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
    
            }
            error_page   500 502 503 504  /50x.html;   
       }
    
        ##
        # Basic Settings
        ##
    
        sendfile on; 
        tcp_nopush on; 
        tcp_nodelay on; 
        keepalive_timeout 65; 
        types_hash_max_size 2048;
        # server_tokens off;
    
        # server_names_hash_bucket_size 64; 
        # server_name_in_redirect off;
    
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
    
        ##
        # Logging Settings
        ##
    

    问题: 1.我成功失败的这些情况,到底是跟 301 重定向有关还是跟 http https 有关?(我了解一些 HTTP HTTPS 的内容和 HSTS )

    2.为什么加了

    proxy_redirect off;

    就能好了?

    3.所以到底如何才能在浏览器地址栏不变的情况下真正代理?

    谁能简单说一下 浏览器 代理服务器 目的服务器的过程? 谢谢了!感激不尽!

    11 条回复    2018-04-25 13:20:50 +08:00
    whx20202
        1
    whx20202  
    OP
       2016-09-11 11:17:20 +08:00
    popu111
        2
    popu111  
       2016-09-11 12:17:37 +08:00 via Android   ❤️ 1
    zhoulouzi
        3
    zhoulouzi  
       2016-09-11 14:17:39 +08:00   ❤️ 1
    upsteam 和 set 用一个。
    whx20202
        4
    whx20202  
    OP
       2016-09-11 15:51:29 +08:00
    @zhoulouzi 可以麻烦您稍微说一下为什么它 301 发过来,我就开始直连了呢?好像反代失效了
    julyclyde
        5
    julyclyde  
       2016-09-11 22:46:59 +08:00
    @whx20202 这明显是由于给的 301 是完整网址
    whx20202
        6
    whx20202  
    OP
       2016-09-12 00:19:28 +08:00 via iPhone
    @julyclyde 奇怪的很,我用抓包发现 http 头的 host 没有被替换,还是 127.0.0.1 ,估计这里出错了,可是我配置文件写了替换头的那句啊
    Siril
        7
    Siril  
       2016-09-12 09:47:29 +08:00
    呃, 反代 https 不是 只能降级成 http 么, 不明觉厉

    -------

    写 hosts (或者 dns 服务器的 hosts ) 把 github.com 劫持到反代上。
    julyclyde
        8
    julyclyde  
       2016-09-12 11:47:45 +08:00
    @whx20202 响应里面的 Location 啊,不是 host
    julyclyde
        9
    julyclyde  
       2016-09-12 11:48:30 +08:00   ❤️ 1
    proxy_set_header Host $http_host;
    这个可能有问题,对方会不认的
    whx20202
        10
    whx20202  
    OP
       2016-09-12 13:40:01 +08:00
    结贴

    proxy_set_header Host $http_host;

    这句话把我和 nginx 通信的 HTTP 头原封不动的发给了 目的服务器

    我以为这句话的意思是: Nginx 向目的服务器请求的时候替换成目的服务器的 Host ,实际没有

    真正的做法是 proxy_set_header Host $proxy_host;

    再次感谢帮助我的各位,也在这里存个档
    liuyinltemp
        11
    liuyinltemp  
       2018-04-25 13:20:50 +08:00
    学习一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2930 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 09:04 · PVG 17:04 · LAX 02:04 · JFK 05:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.