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

nginx 怎么反向代理到百度?

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

    我试着用了下面的配置文件,结果返货 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   ·   我们的愿景   ·   实用小工具   ·   948 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:48 · PVG 04:48 · LAX 13:48 · JFK 16:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.