環境介紹php
192.168.127.131 nginx負載均衡 兼web服務器html
192.168.127.130 nginx負載均衡 兼web服務器nginx
每臺服務器上面有兩個web站點分別是web
www.123.com 瀏覽器
www.hu.com 服務器
說明:負載均衡
準備用dns輪詢解析來實現負載均衡,而且在兩臺server上面還同時啓用了Nginx負載均衡,兩臺服務器是互爲負載。前提是兩臺web服務端的網站內容一致。ide
配置過程:測試
在130負載均衡上面的操做:網站
在 上面編輯一個配置文件 vi /usr/local/nginx/conf/vhost/ld.conf
##www.123.com
upstream 123{ server 192.168.127.130:81; server 192.168.127.131:81 ; ip_hash; } server { listen 80; server_name www.123.com; location / { proxy_pass http://123/; proxy_set_header Host $host; } } upstream hu { server 192.168.253.131:82; server 192.168.253.130:82; ip_hash; } server { listen 80; server_name www.hu.com; location / { proxy_pass http://hu/; proxy_set_header Host $host; }
————————————————————————————————————————————————
更改 nginx負載服務器上面各個web主機的默認端口
server { listen 192.168.127.130:82; server_name www.hu.com; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } }
—————————————————————————————————————————————
server { listen 192.168.127.130:81 ; server_name www.123.com; index index.html index.htm index.php; root /data/www2; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www2$fastcgi_script_name; } }
131上面的配置:
server { listen 192.168.127.131:81 ; server_name www.123.com; index index.html index.htm index.php; root /data/www2; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www2$fastcgi_script_name; } }
#####################################################################
server { listen 192.168.127.131:81 ; server_name www.123.com; index index.html index.htm index.php; root /data/www2; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www2$fastcgi_script_name; } }
#########################################################################################
一樣的在 上面編輯一個配置文件 vi /usr/local/nginx/conf/vhost/ld.conf
##www.123.com
upstream 123{ server 192.168.127.130:81; server 192.168.127.131:81 ; ip_hash; } server { listen 80; server_name www.123.com; location / { proxy_pass http://123/; proxy_set_header Host $host; } } upstream hu { server 192.168.253.131:82; server 192.168.253.130:82; ip_hash; } server { listen 80; server_name www.hu.com; location / { proxy_pass http://hu/; proxy_set_header Host $host; }
最後分別啓動nginx服務並查看端口是否正常啓動
測試:分別在兩個網站的目錄裏添加1.txt測試文件,裏面寫上不一樣的內容而後用瀏覽器訪問 www.hu.com/1.txt,刷新幾回
一樣的,www.123.com/1.txt也是這樣