Nginx多域名負載均衡配置


Nginx負載均衡設置

環境:

負載均衡:192.168.188.128:80

Web1:192.168.188.128:81

Web2:192.168.188.129:80

正式環境中,需要解析域名www.doubles.cn、abc.dd.cn到負載均衡機器192.168.188.128,我們現在測試,就直接在本地windows下的hosts里面綁定域名:

192.168.188.128 www.doubles.cn

192.168.188.128 abc.dd.cn

1、單個域名的負載均衡

1.1、在web1(192.168.188.128)上搭好web環境:

[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
...
include vhost/*.conf;
...
}

在http{}最下面添加include vhost/*.conf;每個域名對應一個conf文件。

新建vhost目錄。

[root@localhost conf]# mkdir /usr/local/nginx/conf/vhost/

新建www.doubles.cn.conf文件:

[root@localhost conf]# vim /usr/local/nginx/conf/vhost/www.doubles.cn.conf
server {
        listen       81;
        server_name  www.doubles.cn localhost 192.168.188.128;
        location / {
            root    /usr/local/nginx/html/;
            index  index.html index.php index.htm TempLoginPanel.html;
        }
        location ~ \.php$ {
            root           /usr/local/nginx/html/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}

在/usr/local/nginx/html/里面寫好html文件:

[root@localhost conf]# vim /usr/local/nginx/html/index.html
#測試內容自定義
……

重新加載nginx配置文件

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload

測試web1:

wpsC166.tmp

1.2、在web2(192.168.188.129)上搭好web環境:

按照1.1的方法同樣搭建web2的環境,

新建虛擬主機:

[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
...
include vhost/*.conf;
...
}
[root@localhost conf]# mkdir /usr/local/nginx/conf/vhost/
[root@localhost conf]# vim /usr/local/nginx/conf/vhost/www.doubles.cn.conf
server {
        listen       80;
        server_name  www.doubles.cn localhost 192.168.188.129;
        location / {
            root    /usr/local/nginx/html/;
            index  index.html index.php index.htm TempLoginPanel.html;
        }

        location ~ \.php$ {
            root           /usr/local/nginx/html/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}

在/usr/local/nginx/html/里面寫好html文件:

[root@localhost conf]# vim /usr/local/nginx/html/index.html
#測試內容自定義
……

重新加載nginx配置文件

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload

測試如下:

wpsC176.tmp

注意:正式環境當中,web1和web2機器上面的網頁內容應該是一致的,才能做負載均衡。這里為了區分兩台機,所以在網頁內容中區分了下。

1.3、在負載均衡機器(192.168.188.128)上:

[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
...上面的省略...
upstream  doublesweb {
        #ip_hash;
        server   192.168.188.128:81;
        server   192.168.188.129:80;
    }

server {
        listen       80;
        server_name  localhost;
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://doublesweb;
            proxy_connect_timeout 2s;
        }
....略...

(注意):這里upstream與proxy_pass的名字必須一致,這里都是doublesweb。

重新加載nginx配置文件

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload

測試:

在windows上打開瀏覽器:輸入網址www.doubles.cn,發現已經可以輪詢web1和web2了

wpsC177.tmp

再刷新:

wpsC178.tmp

2、多個域名的負載均衡

在1中我們只對www.doubles.cn做了負載均衡,實際環境中我們可能需要對多個域名進行負載均衡。這里我們添加一個abc.dd.cn。

2.1、web1上面新建虛擬主機abc.dd.cn

增加網頁目錄/usr/local/nginx/html/dd/:

[root@localhost html]# mkdir /usr/local/nginx/html/dd/
[root@localhost html]# vim /usr/local/nginx/html/dd/index.html

添加一個abc.dd.cn.conf的虛擬主機:

[root@localhost html]# vim /usr/local/nginx/conf/vhost/abc.dd.cn.conf
server {
        listen       81;
        server_name  abc.dd.cn;
        location / {
            root    /usr/local/nginx/html/dd/;
            index  index.html index.php index.htm TempLoginPanel.html;
        }

        location ~ \.php$ {
            root           /usr/local/nginx/html/dd/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}

重新加載nginx配置文件

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload

2.2、在web2上新建虛擬主機abc.dd.cn

跟web1同樣的操作:

增加網頁目錄/usr/local/nginx/html/dd/:

[root@localhost html]# mkdir /usr/local/nginx/html/dd/
[root@localhost html]# vim /usr/local/nginx/html/dd/index.html

添加一個abc.dd.cn.conf的虛擬主機:

[root@localhost html]# vim /usr/local/nginx/conf/vhost/abc.dd.cn.conf
server {
        listen       80;
        server_name  abc.dd.cn;
        location / {
            root    /usr/local/nginx/html/dd/;
            index  index.html index.php index.htm TempLoginPanel.html;
        }

        location ~ \.php$ {
            root           /usr/local/nginx/html/dd/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}

重新加載nginx配置文件

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload

2.3、配置負載均衡

在負載均衡機器(192.168.188.128)上:

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
...
    upstream  www.doubles.cn {
        #ip_hash;
        server   192.168.188.128:81;
        server   192.168.188.129:80;
    }

   upstream  abc.dd.cn {   
       #ip_hash;
       server   192.168.188.128:81; 
       server   192.168.188.129:80;  
   }

    server {
        listen       80;
        server_name  localhost;
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://$host;
            proxy_connect_timeout 2s;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
.....

(注意):這里upstream后面的名字必須跟你訪問的域名保持完全一致,否則將無法代理。因為proxy_pass是根據$host來匹配的。

重新加載nginx配置文件

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload

測試:

wpsC189.tmp

再刷新:

wpsC18A.tmp

測試之前的域名:

wpsC18B.tmp

wpsC18C.tmp

仍然可以進行負載均衡,所以生效。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM