話就不多說了,nginx安裝與配置,還有負載均衡呢,可以看我寫的另一篇文章《nginx負載均衡實戰》,還有關于負載均衡呢,大家可以看一下我寫的另外兩篇文章,一個是《lvs+keepalived負載均衡》,另一個是《haproxy+keepalived負載均衡》,三種負載均衡的差別呢,可以看一下我轉載的一篇文章《軟體級負載均衡器(LVS/HAProxy/Nginx)的特點簡介和對比》,下面直接進入配置步驟:
1.系統環境
[plain] view plain copy
- 系統版本: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上操作
[plain] view plain copy
- useradd nginx
- vi /usr/local/nginx/conf/nginx.conf
内容如下:
[plain] view plain copy
- #運作使用者
- 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配置檔案
[plain] view plain copy
- vi /etc/keepalived/keepalived.conf
内容如下:
[plain] view plain copy
- 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.寫自定義腳本
[plain] view plain copy
- vi /etc/keepalived/check_nginx.sh
内容如下: [plain] view plain copy
- !/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伺服器的負載均衡。
這裡還要給腳本加上執行權限,如下
[plain] view plain copy
- 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看到這樣的日志
[plain] view plain copy
- 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伺服器的日志顯示 [plain] view plain copy
- 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,到這裡全部結束了,實踐親測,祝君成功
From: http://blog.csdn.net/zmj_88888888/article/details/8825471