网站制作学习网经验与学习→正文:proxy_set_header Host $http_host / $host 两者的区别
字体:

proxy_set_header Host $http_host / $host 两者的区别

经验与学习 2015/7/31 12:18:22  点击:不统计

原载于:文章来源:www.forasp.cn网站制作学习
 转自:http://rhomobi.com/topics/49

扫盲proxy_set_header

Syntax: proxy_set_header field value
Default: Host $proxy_host / Connection close
Context: http / server / location
Reference: proxy_set_header

This directive allows to redefine and to add some request header lines which will be transferred to the proxied server.

As the value it is possible to use a text, variables and their combination.

proxy_set_header directives issued at higher levels are only inherited when no proxy_set_header directives have been issued at a given level.

By default only two lines will be redefined:

proxy_set_header Host $proxy_host;
proxy_set_header Connection Close;

The unchanged request-header "Host" can be transmitted like this:
>注意如果这么设置了,后面的端口号可能会“泄露”,程序拼接要采用$host$URI,剔除$port;

proxy_set_header Host $http_host;

However, if this line is absent from the client request, then nothing will be transferred.

In this case it is better to use variable $host, it's value is equal to the name of server in the request-header "Host" or to the basic name of server, if there is no line:

proxy_set_header Host $host;

Furthermore, it is possible to transmit the name of server together with the port of the proxied server:

proxy_set_header Host $host:$proxy_port;

If value is empty string, than header will not be sent to upstream. For example this setting can be used to disable gzip compression on upstream:

proxy_set_header  Accept-Encoding  "";

proxy_set_header Host $http_host

应用于解决nginx同IP、同端口、不同域名时的转发。在使用nginx时对于upstream的server的测试中发现,nginx默认对其使用的是基于IP的转发,这就意味着对于同IP、同端口、不同域名时无法进行转发。

转发网友timo的文章http://zauc.wordpress.com/

    对于后端是同一端口多域名转发的nginx proxy。在nginx中的默认proxy是只能对后面real server做端口转发的,而不能做域名转发。
    这个是因为默认情况下:
    proxy_set_header Host $proxy_host;

    这样就等于前端输入域名后到nginx这里直接转换成IP进行转发了。
    于是我们需要修改proxy_set_header的参数。

    proxy_set_header Host $http_host;

    下面这个例子中backend1权重为5,其他默认为1,最大失效次数3次,如果30秒内没有响应就认为是失效了。

    upstream lb  { 
    server yaosansi.com weight=5; 
    server www.yaosansi.com max_fails=3  fail_timeout=30s; 
    server unix:/tmp/test3; 
    } 

    server { 

             listen 80; 
             server_name  www.yaosansi.com; 
             location  /     { 
             proxy_store off; 
             proxy_redirect  off; 
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
             proxy_set_header X-Real-IP $remote_addr; 
             proxy_set_header Host $http_host; 
             proxy_pass http://lb;  
            } 
    }

proxy_set_header Host $host

用于把当前server name赋予 HOST

转发网友Ayou遇到的问题《nginx的proxy_pass到$host的问题》[2012-08-02 15:22:00]

今天在配置一个location的时候,希望使用一个变量如$host来指示nginx代理:

location /test/ {
proxy_pass http://$host;
}

如你想不到,这个配置是不能使用的,查看error.log,打出来的信息也无法帮助解决问题。

但相同情况下,root标签就工作得很好:

locatin /test/ {
root /dev/shm/$host;
}

令人匪夷所思,估计这是nginx的一个bug,或者是一个搅不清的逻辑?

把上面的错误配置改成

location /test/ {
proxy_pass http://$host/;
}

或者

set $vhost "test.sudone.com";
location /test/ {
proxy_pass http://$vhost;
}

等等,都不能解决问题,最后是用了个奇怪的配置方式,居然能工作……

location /test/ {
proxy_pass http://127.0.0.1:80;
proxy_set_header Host $host;
}

这个配置就能工作正常,但上面这个配置只支持单ip的后端,能不能支持upstream呢?

再改一下测测

upstream backend.sudone.com {
server 127.0.0.1:80;
}

location /test/ {
proxy_pass http://backend.sudone.com;#固定写的后端upstream
proxy_set_header Host $host;#$host=test.sudone.com;
}

嗯?能用!

有了这个办法,我便不用在数十个主机配置内把/test/这个location贴上数十次,另外,它也可以支持这样的写法:

location /test/ {
proxy_pass http://127.0.0.1:80;
proxy_set_header Host fetch.$host;
}

还可以用正则表达式对host进行匹配:

set $vhost $host;
if $host ~* "([^\.]+)\.sudone\.com" {
set $vhost $1.xxx.com;
}

location /test/ {
proxy_pass http://127.0.0.1:80;
proxy_set_header Host fetch.$host;
}

可用的方面就广了。

---------------------------------------------------

补充:

在这个配置里,我的nginx代理的后端(backend)也是nginx,目前尚不清楚其它的web服务器如apache等是否能在这样的配置下工作正常。

学习www.网for站asp制.cn作

·上一篇:TIMESTAMP with implicit DEFAULT value is deprecated >>    ·下一篇:免费ssl证书 >>
推荐文章
最新文章