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:
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
測試如下:
註意:正式環境當中,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了
再刷新:
2、多個域名的負載均衡
在1中我們只對www.doubles.cn做了負載均衡,實際環境中我們可能需要對多個域名進行負載均衡。這裏我們添加一個abc.dd.cn。
2.1、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 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上進行跟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
...
#gzip on;
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;
}
.....
重新加載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
測試:
再刷新:
測試之前的域名:
仍然可以進行負載均衡,所以生效。
Nginx多域名負載均衡配置