一台nginx的負載均衡服務器(172.25.254.131)
兩台安裝httpd作為web端
一、准備工作
1.1 安裝nginx
yum -y install gcc openssl-devel pcre-devel useradd -s /sbin/nologin nginx cd ~/nginx-1.10.1/ ./configure \ >--prefix=/usr/local/nginx \ >--user=nginx \ >--group=nginx \ >--with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module #在安裝pcre的時候,如果使用的是源碼安裝,則在這里需要只用pcre的位置 make && make install
ln -s /usr/local/nginx/sbin/nginx /sbin/
nginx -t
nginx
nginx -s reload
1.2 安裝appache
[root@web1 ~]# yum -y install httpd [root@web1 ~]# systemctl restart httpd [root@web1 ~]# netstat -ntlp |grep 80 tcp6 0 0 :::80 :::* LISTEN 80049/httpd [root@web1 ~]# echo 172.25.254.134>>/var/www/html/index.html [root@web1 ~]# curl 172.25.254.134 172.25.254.134 [root@web1 ~]# firewall-cmd --add-port=80/tcp --permanent [root@web1 ~]# firewall-cmd --reload [root@web1 ~]# firewall-cmd --list-all ports: 80/tcp [root@web2 ~]# yum -y install httpd [root@web2 ~]# systemctl restart httpd [root@web2 ~]# netstat -ntlp |grep 80 tcp6 0 0 :::80 :::* LISTEN 80049/httpd [root@web1 ~]# echo 172.25.254.135>>/var/www/html/index.html [root@web1 ~]# curl 172.25.254.135 172.25.254.135 [root@web1 ~]# firewall-cmd --add-port=80/tcp --permanent [root@web1 ~]# firewall-cmd --reload [root@web1 ~]# firewall-cmd --list-all ports: 80/tcp
二、配置
2.1 開始配置nginx的負載均衡
一般2000萬pv以下的負載均衡都可以使用nginx實現,依賴於Nginx_http_upstream_module模塊。所支持的代理方式有proxy_pass,fastcgi_pass,memcached_pass.
upstream模塊應放於nginx.conf配置的http{}內。默認的算法是wrr權重輪詢
參數:weight max_fails(根據應用場景,小的話,用戶體驗度高,但是要求節點足夠多,同時在負載大於80%后,不會向該節點扔請求,大的話可以提高服務器的利用率 ),backup,fail_timeout,down
[root@lb01 ~]# cd /usr/local/nginx/conf/
[root@lb01 conf]# egrep -v "#|^$" nginx.conf.default
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
[root@lb01 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
[root@lb01 conf]# vim nginx.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web_pools{
#可以跟ip_hash;這樣,backup不能使用,權重也沒有效果,一個客戶端永遠訪問一台 server 172.25.254.134:80 weight=5; server 172.25.254.135:80 weight=5; server 172.25.254.158:80 weight=5 backup; #nginx自帶的一個高可用 }
#nginx自帶判斷功能,單節點故障,會自己剔除故障節點,節點故障修復,又會自己添加進來 server { listen 80; server_name www.lb01test.com; location / { root html; index index.html index.htm;
proxy_set_header Host $host; #只帶請求的主機名 proxy_pass http://web_pools; } } }
[root@lb01 conf]# 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@lb01 conf]# nginx -s reload
2.2 訪問
[root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.134 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.134 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.134 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.134 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135
三 、upstream核心模塊和相關參數
3.1 算法
輪詢:默認算法,每一個請求按順序逐一分配到不同的后端服務器,如果后端服務器down掉了,則能自動剔除。
weight:權重,設置權重,用於后端服務器性能不均的情況,訪問比率約等於權重之比
ip_hash:解決了session問題:每個請求按訪問IP的hash結果分配,這樣每個訪客可以固定一個后端服務器。
fair(第三方)動態算法:
按照后端服務器的響應時間來分配請求,相應的時間短的有限分配,是比輪詢和權重的更加智能的負載均衡算法,必須下載upstream_fair的模塊
url_hash算法
按照訪問的url的hash結果來分配請求,讓每個url定向到同一個后端服務器,后端服務器Wie緩存服務器時的效果顯著。在uupstream中加入hash語句,servevr語句中不能寫入weight等其他參數,hash_method是使用的hash算法
url_hash安訪問的url的hash結果來分配請求,是每個url定向到同一個后端服務器。可以進一步的提高后端緩存服務器的命中率,需要安裝Nginx的hsah軟件包
url_hash(web緩存節點)和Ip_hsah(回話保持類似)
least_conn:最少連接數,那個機器連接數少就分發
一致性Hash
max_conns=number:限制最大的並發連接,默認為0,沒有限制
3.2 http_proxy_module
Proxy_pass指令屬於ngx_http_proxy_module模塊,此模塊可以將請求轉發到另一台服務器
proxy_set_header:設置后端的服務器獲取用戶的主機名或者真實的IP地址,以及代理着的真實IP
client_body_buffer_size:用於指定客戶端請求主題緩沖區大小,可以理解為先保存到本地在傳給用戶
proxy_connect_timeout:表示與后端服務器連接的超時時間,及發起我吼等候的超時時間
proxy_send_timeout:表示后端服務器的數據回傳時間,即在規定的時間之內后端服務器必須傳完所有的數據,否則,Nginx將斷開連接
proxy_read_timeout:是指Nginx從反向代理的后端服務器獲取信息的時間,表示連接建立成功后,Nginx等后端服務器的響應時間,
Proxy_pass http://blog_server_pool:用於指定反向代理的服務器池
proxy_set_header Host $Host:當后端服務器上配置多個虛擬主機時,需要用到Header來區分反向代理哪一個主機
proxy_set_header X-Forwareded-For $remote_addr:如果侯丹Web服務器上的程序需要獲取用戶IP,從該Header頭獲取。