nginx 怎么反向代理到百度? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
whx20202
V2EX    NGINX

nginx 怎么反向代理到百度?

  •  
  •   whx20202 2016-09-12 08:51:41 +08:00 20267 次点击
    这是一个创建于 3390 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我试着用了下面的配置文件,结果返货 405 bfe not allowed ,然后我用 tcpdump 和 wireshark 在自己 nginx 上抓包,发现好像, nginx 反向到百度的 http 头,还是我一开始的那个:

    我在浏览器输入我自己本地的 nginx 地址: http://127.0.0.1:8080/ 这时候 http 头的 host 肯定是 127.0.0.1

    nginx 反向代理的时候,host应该是被他修改,变成: www.baidu.com 才对,实际上没有...。 可是最关键的问题是,我已经设置:

    proxy_set_header Host $http_host; 按理来说应该给我转换了

    是不是我还有哪里没设置好?

    具体配置如下:

    http {

     server { listen 8080 default backlog=2048; listen 443 ssl; server_name 127.0.0.1; ssl_certificate /etc/nginx/1.crt; ssl_certificate_key /etc/nginx/1.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 / /; 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; 
    5 条回复    2016-09-12 11:19:20 +08:00
    oott123
        1
    oott123  
       2016-09-12 10:17:05 +08:00   2
    proxy_set_header Host $http_host;

    proxy_set_header Host www.baidu.com;
    whx20202
        2
    whx20202  
    OP
       2016-09-12 10:59:26 +08:00
    @oott123 已感谢! 可是您能否告诉我为啥

    proxy_set_header Host $http_host; proxy_set_header Host $host; 都不行呢。。
    感觉应该是可以的~
    oott123
        3
    oott123  
       2016-09-12 11:06:05 +08:00   1
    $host 和 $http_host 都是你访问 nginx 的时候的 host ,并不是你反代的那个 host 。$proxy_host 才是你反代的那个 host ,其实你写死也一样,不一定要配置成变量。


    $host
    in this order of precedence: host name from the request line, or host name from the “ Host ” request header field, or the server name matching a request

    ---

    The ngx_http_proxy_module module supports embedded variables that can be used to compose headers using the proxy_set_header directive:

    $proxy_host
    name and port of a proxied server as specified in the proxy_pass directive;
    jerray
        4
    jerray  
       2016-09-12 11:13:35 +08:00   2
    @whx20202 http://serverfault.com/questions/706438/what-is-the-difference-between-nginx-variables-host-http-host-and-server-na

    $host 和 $http_host 是指向你自己的机器的。要代理到其他网站, HTTP Header 里的 Host 就要设置成目标站点的域名,这样请求发到目标站点,目标站点的 Web 服务器才能正确处理请求。
    whx20202
        5
    whx20202  
    OP
       2016-09-12 11:19:20 +08:00
    @jerray 非常感谢您和上面那位同学。。了解了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5063 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 09:09 PVG 17:09 LAX 01:09 JFK 04:09
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86