廢話就不多說了,nginx安裝與配置,還有負載均衡呢,可以看我寫的另一篇文章《nginx負載均衡實戰》,還有關於負載均衡呢,大家可以看一下我寫的另外兩篇文章,一個是《lvs+keepalived負載均衡》,另一個是《haproxy+keepalived負載均衡》,三種負載均衡的區別呢,可以看一下我轉載的一篇文章《軟件級負載均衡器(LVS/HAProxy/Nginx)的特點簡介和對比》,下面直接進入配置步驟:
1.系統環境
系統版本:CentOS release 5.9 (Final) x86 32位
nginx版本: 1.2.8
keepalived版本: 1.2.4
主keepalived:192.168.207.130
從keepalived:192.168.207.131
VIP:192.168.207.140
WEB_1:192.168.207.129 80端口
WEB_2:192.168.207.130 8080端口
WEB_3:192.168.207.131 8080端口
2.自定義nginx配置文件
在192.168.207.130和192.168.207.131上操作
useradd nginx
vi /usr/local/nginx/conf/nginx.conf
內容如下:
#運行用戶
user nginx nginx;
#啓動進程
worker_processes 2;
#全局錯誤日誌及PID文件
error_log logs/error.log notice;
pid logs/nginx.pid;
#工作模式及每個進程連接數上限
events {
use epoll;
worker_connections 1024; #所以nginx支持的總連接數就等於worker_processes * worker_connections
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型
include mime.types; #這個是說nginx支持哪些多媒體類型,可以到conf/mime.types查看支持哪些多媒體
default_type application/octet-stream; #默認的數據類型
#設定日誌格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#設定請求緩衝
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#開啓gzip模塊
#gzip on;
#gzip_min_length 1100;
#gzip_buffers 4 8k;
#gzip_types text/plain;
#output_buffers 1 32k;
#postpone_output 1460;
#設定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#設定負載均衡的服務器列表
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的機率越大
server 192.168.207.129:80 weight=5;
server 192.168.207.130:8080 weight=5;
server 192.168.207.131:8080 weight=5;
}
server { #這個是設置web服務的,監聽8080端口
listen 8080;
server_name 192.168.207.131; #這個根據系統ip變化
index index.html index.htm;
root /var/www/html;
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}
}
#設定虛擬主機
server {
listen 80;
server_name 192.168.207.140; #這裏是VIP
#charset gb2312;
#設定本虛擬主機的訪問日誌
access_log logs/three.web.access.log main;
#如果訪問 /img/*, /js/*, /css/* 資源,則直接取本地文件,不通過squid
#如果這些文件較多,不推薦這種方式,因爲通過squid的緩存效果更好
#location ~ ^/(img|js|css)/{
# root /data3/Html;
# expires 24h;
#}
#對 "/" 啓用負載均衡
location / {
proxy_pass http://mysvr; #以這種格式來使用後端的web服務器
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#設定查看Nginx狀態的地址 ,在安裝時要加上--with-http_stub_status_module參數
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd; #設置訪問密碼,htpasswd -bc filename username password
}
}
}
3.自定義keepalived配置文件
vi /etc/keepalived/keepalived.conf
內容如下:
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_nginx.sh" ###監控腳本
interval 2 ###監控時間
weight 2 ###目前搞不清楚
}
vrrp_instance VI_1 {
state MASTER ### 設置爲 主
interface eth0 ### 監控網卡
virtual_router_id 51 ### 這個兩臺服務器必須一樣
priority 101 ### 權重值 MASTRE 一定要高於 BAUCKUP
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 執行監控的服務
}
virtual_ipaddress {
192.168.207.140 ### VIP 地址
}
}
4.寫自定義腳本
vi /etc/keepalived/check_nginx.sh
內容如下:
!/bin/bash
A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx進程 把值賦給變量A
if [ $A -eq 0 ];then ## 如果沒有進程值得爲 零
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
/etc/init.d/keepalived stop ## 則結束 keepalived 進程
fi
fi
這裏是檢查nginx是否啓動好,如果沒有啓動,先啓動 nginx,隔了3秒後還沒有啓動好,則將keepalived進程也關閉,這樣從keepalived就能接手過去了,提供高可用性,在這裏呢,keepalived服務是提供高可用性,而nginx是提供後端web服務器的負載均衡。
這裏還要給腳本加上執行權限,如下
chmod +x /etc/keepalived/check_nginx.sh
5.啓動服務,並測試
在這裏先說一下啊,在WEB_1上,我是使用系統自帶的apache昨晚web服務器的,比較省事,這樣呢,我只要啓動好主從keepalived就ok了,因爲它會利用check_nginx.sh腳本來自動啓動nginx。
都啓動好了。
訪問http://192.168.207.140就可以輪訓訪問後端的三臺web服務器內容啦
這裏我們把主keepalived服務給關掉,來測試高可用性
然後會在從keepalived服務器上的/var/log/messages看到這樣的日誌
Apr 19 17:42:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140
Apr 19 17:42:45 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 added
Apr 19 17:42:45 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 added
Apr 19 17:42:45 localhost avahi-daemon[4204]: Registering new address record for 192.168.207.140 on eth0.
繼續訪問http://192.168.207.140,依舊可以訪問後端的三臺web服務器
然後再把原主keepalived打開,可以觀察到原從keepalived服務器的日誌顯示
Apr 19 17:42:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140
Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 19 17:44:06 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 removed
Apr 19 17:44:06 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 removed
Apr 19 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.
說明有恢復了原來的主從結果。
生產環境中,後端的機器也可能會掛掉,但是呢,這就不用你操心啦,nginx會自動把session分配到好的後端web服務器上的啦
ok,到這裏全部結束了,實踐親測,祝君成功