準備三臺虛擬機來做這個實驗:
192.168.232.132 web服務器
192.168.232.133 web服務器
192.168.232.134 負載均衡服務器
首先三臺電腦預裝nginx軟件:
1、導入外部軟件庫
- rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm
- rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm
- rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm
rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm
rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
以下添加註釋
- mirrorlist=http://dmirr.iuscommunity.org/mirrorlist?repo=ius-el6&arch=$basearch
mirrorlist=http://dmirr.iuscommunity.org/mirrorlist?repo=ius-el6&arch=$basearch
以下刪除註釋
- #baseurl=http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/$basearch
#baseurl=http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/$basearch
2、yum安裝nginx
yum install nginx3、啓動nginx
chkconfig nginx onservice nginx start
向web服務器中放入測試文件:
- <html>
- <head>
- <title>Welcome to nginx!</title>
- </head>
- <body bgcolor="white" text="black">
- <center><h1>Welcome to nginx! 192.168.232.132</h1></center>
- </body>
- </html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx! 192.168.232.132</h1></center>
</body>
</html>
配置負載均衡服務器:
vi /etc/nginx/nginx.conf內容如下:
- user nginx;
- worker_processes 1;
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
- sendfile on;
- #tcp_nopush on;
- keepalive_timeout 65;
- #gzip on;
- upstream test.miaohr.com {
- server 192.168.232.132:80;
- server 192.168.232.133:80;
- }
- server {
- listen 80;
- server_name test.miaohr.com;
- charset utf-8;
- location / {
- root html;
- index index.html index.htm;
- proxy_pass http://test.miaohr.com;
- proxy_set_header X-Real-IP $remote_addr;
- client_max_body_size 100m;
- }
- location ~ ^/(WEB-INF)/ {
- deny all;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root /var/www/html/;
- }
- }
- }
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream test.miaohr.com {
server 192.168.232.132:80;
server 192.168.232.133:80;
}
server {
listen 80;
server_name test.miaohr.com;
charset utf-8;
location / {
root html;
index index.html index.htm;
proxy_pass http://test.miaohr.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/;
}
}
}
下面瀏覽器打開:192.168.232.134,如果132、133交替顯示則表明試驗成功。
拓展:
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。
2、weight
指定輪詢機率,weight和訪問比率成正比,用於後端服務器性能不均的情況。
例如:
upstream bakend {
server 192.168.159.10 weight=10;
server 192.168.159.11 weight=10;
}
3、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
例如:
upstream resinserver{
ip_hash;
server 192.168.159.10:8080;
server 192.168.159.11:8080;
}
4、fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream resinserver{
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。
例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法
upstream resinserver{
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
tips:
upstream resinserver{#定義負載均衡設備的Ip及設備狀態
ip_hash;
server 127.0.0.1:8000 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6801;
server 127.0.0.1:6802 backup;
}
在需要使用負載均衡的server中增加
proxy_pass http://resinserver/;
每個設備的狀態設置爲:
1.down 表示單前的server暫時不參與負載
2.weight 默認爲1.weight越大,負載的權重就越大。
3.max_fails :允許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
4.fail_timeout:max_fails次失敗後,暫停的時間。
5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
client_body_in_file_only 設置爲On 可以講client post過來的數據記錄到文件中用來做debug
client_body_temp_path 設置記錄文件的目錄 可以設置最多3層目錄
location 對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡