1. 程式人生 > 實用技巧 >11,nginx proxy_set_header Host $host 模組講解

11,nginx proxy_set_header Host $host 模組講解

當後端web伺服器存在多個虛擬主機時,該模組是用來區分前端請求,是給反向代理的後端的哪個虛擬主機

1 在伺服器192.168.132.131 前端伺服器如下配置:
upstream backend {
       server 192.168.132.129 max_fails=3 fail_timeout=30s;
       server 192.168.132.130 max_fails=3 fail_timeout=30s;
}

server {
        listen       80;
        server_name  www.etiantian.org;
       index    index.php  index.html index.htm;
       location / {
              proxy_pass http://backend;
       }
    }
    server {
        listen       80;
        server_name  bbs.etiantian.org;
           root     html/bbs;
           index    index.html index.htm;

       location / {
              proxy_pass http://backend;
       }
    }
}

[root@moban conf]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.132.131 www.etiantian.org bbs.etiantian.org blog.etiantian.org status.etiantian.org

[root@moban conf]#

[root@moban conf]# for n in `seq 100`; do curl bbs.etiantian.org;sleep 2; done
bbs.etiantian.org
www.etiantian.org
bbs.etiantian.org
www.etiantian.org
bbs.etiantian.org
www.etiantian.org
bbs.etiantian.org

從這可以看出你訪問到後端時,伺服器並不知到你想訪問後端的哪個虛擬主機,所以他就將第一個虛擬主機返回給你,顯然這是不正確的!!!!

那麼解決這個方法的途徑就是在 location 標籤中加上  proxy_set_header Host $host; 後端伺服器就知道你想訪問的是哪個虛擬主機了。

    server {
        listen       80;
        server_name  bbs.etiantian.org;
     root     html/bbs;
     index    index.html index.htm;
 
 location / {
  proxy_pass http://backend;
  proxy_set_header Host $host;
 }
    }

}
[root@moban conf]# for n in `seq 100`; do curl bbs.etiantian.org;sleep 2; done
http://bbs.etinatian.org
bbs.etiantian.org
http://bbs.etinatian.org
bbs.etiantian.org
http://bbs.etinatian.org
bbs.etiantian.org
http://bbs.etinatian.org
bbs.etiantian.org
 

2 在後端伺服器192.168.132.129 這樣配置:
[root@moban conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
server {
        listen       80;
        server_name  www.etiantian.org;
 root html/www;
 index    index.php  index.html index.htm;
    }
    server {
        listen       80;
        server_name  bbs.etiantian.org;
     root     html/bbs;
     index    index.html index.htm;
 
    }

}

[root@moban conf]# curl www.etiantian.org
www.etiantian.org
[root@moban conf]# curl bbs.etiantian.org
http://bbs.etinatian.org
[root@moban conf]#

 

3 在另一後端伺服器192.168.132.130如下配置:
[root@moban conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  bbs.etiantian.org;
     root     html/bbs;
     index    index.html index.htm;
 
    }
server {
        listen       80;
        server_name  www.etiantian.org;
 root html/www;
 index    index.php  index.html index.htm;
    }

}
[root@moban conf]# curl bbs.etiantian.org
bbs.etiantian.org
[root@moban conf]# curl www.etiantian.org
http://www.etiantian.org
[root@moban conf]#