1、nginx負載均衡html
nginx負載均衡配置很簡單,能夠實現7層的負載,對一些輕量級訪問量的站點仍是很實用的nginx
一、架構web
系統版本: CentOS 6.6 x86_64 nginx版本: 1.10.2 #當前最新版本 服務器: 負載均衡server 10.0.18.146 端口 80 後端web server 10.0.18.144 端口 80 10.0.18.145 端口80
二、配置過程算法
在三臺服務器上配置nginx的yum源後端
#cat /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 而後使用yum安裝nginx #yum install nginx -y
在三臺服務器上配置打開文件數centos
#tail -5 /etc/security/limits.conf # End of file * soft nofile 65535 * hard nofile 65535 * soft nproc 10240 * hard nproc 10240
配置後端兩臺server:10.0.18.144,以下:數組
#cat /etc/nginx/nginx.conf #修改以下兩項,其餘不變 events { use epoll; #使用epoll worker_connections 10240; #增長鏈接數 } #cat /etc/nginx/conf.d/default.conf #修改server_name 其餘不變 server { listen 80; server_name nginx1.test.com; ……………… } 修改默認頁面以下: #cat /usr/share/nginx/html/index.html #增長18.144,其餘不變 ………… <h1>Welcome to nginx 18.144!</h1> ………… 啓動nginx #service nginx start
在物理機添加域名解析以下:瀏覽器
添加在hosts中 --->C:\Windows\System32\drivers\etc\hosts 10.0.18.144 nginx1.test.com 10.0.18.145 nginx2.test.com 10.0.18.146 balance.test.com
在瀏覽器訪問,以下:
緩存
配置後端兩臺server:10.0.18.145,方法和18.144同樣,再也不贅述,在瀏覽器訪問以下:bash
三、配置負載均衡server
先查看配置文件:
#cat nginx.conf events { use epoll; worker_connections 10240; } 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; #include /etc/nginx/conf.d/*.conf; upstream backend { #添加的後端server,權重爲1 server 10.0.18.144 weight=1; server 10.0.18.145 weight=1; } server { listen 80; server_name balance.test.com; location / { proxy_set_header Host $host; #設置主機頭和客戶端真實地址 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffering on; #開啓緩存 proxy_pass #方向代理地址 } } } 而後啓動nginx #service nginx start
在瀏覽器訪問http://balance.test.com進行測試:
不斷刷新瀏覽器,是能夠看到18.144和18.145輪流相應的,以下:
這樣就實現了nginx的負載均衡機制,很簡單!
2、nginx upstream的幾種方式:
一、輪詢
輪詢是upstream的默認分配方式,即每一個請求按照時間順序輪流分配到不一樣的後端服務器,若是某個後端服務器down掉後,能自動剔除,以下:
upstream backend { server 192.168.1.101:80; server 192.168.1.102:8080; #後端的端口能夠不同,在這裏配置上就OK }
二、weight
輪詢的增強版,便可以指定輪詢比率,weight和訪問概率成正比,主要應用於後端服務器配置不一樣的場景下,以下:
upstream backend { server 192.168.1.101 weight=1; server 192.168.1.102 weight=2; #權重越大,相應web端的概率越大 } 也能夠這樣: upstream backend { server 192.168.1.101:80 weight=1; server 192.168.1.102:8080 weight=2; }
三、ip_hash
每一個請求按照訪問ip(即Nginx的前置服務器或者客戶端IP)的hash結果分配,這樣每一個訪客會固定訪問一個後端服務器,能夠解決session一致問題,以下:
upstream backend { ip_hash; server 192.168.1.101; server 192.168.1.102; }
四、fair
fair顧名思義,公平地按照後端服務器的響應時間(rt)來分配請求,響應時間短即rt小的後端服務器優先分配請求,以下:
upstream backend { server 192.168.1.101; server 192.168.1.102; fair; }
五、url_hash
與ip_hash相似,可是按照訪問url的hash結果來分配請求,使得每一個url定向到同一個後端服務器,主要應用於後端服務器爲緩存時的場景下,以下:
upstream backend { server 192.168.1.101; server 192.168.1.102; hash $request_uri; hash_method crc32; } 其中,hash_method爲使用的hash算法,須要注意的是此時,server語句中不能加weight等參數。
注意:max_fails和fail_timeout通常會關聯使用,若是某臺server在fail_timeout時間內出現了max_fails次鏈接失敗,那麼Nginx會認爲其已經掛掉了,從而在fail_timeout時間內再也不去請求它,fail_timeout默認是10s,max_fails默認是1,即默認狀況是隻要發生錯誤就認爲服務器掛掉了,若是將max_fails設置爲0,則表示取消這項檢查,以下:
upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; }
3、配置nginx反向代理實現web緩存服務器
nginx支持相似squid的web緩存功能,就是把web頁面根據url編碼哈希後保存到硬盤上,有不少資料顯示,nginx的穩定性和速度不遜於Squid,並且在性能上nginx對多核cpu的利用也超過Squid。並且nginx也同時支持負載均衡,這對於在短時間內忽然頂不住訪問量的網站來講十分的便利,配置以下:
在負載均衡服務器配置:10.0.18.146 #cat nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { use epoll; worker_connections 10240; } 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; #include /etc/nginx/conf.d/*.conf; proxy_temp_path /usr/local/nginx/proxy_temp_dir; #注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區 proxy_cache_path /usr/local/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=30g; #設置Web緩存區名稱爲cache_one,內存緩存空間大小爲100MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小爲20GB upstream backend { server 10.0.18.144:80 weight=1; server 10.0.18.145:8080 weight=1; } server { listen 80; server_name balance.test.com; location / { #若是後端的服務器返回50二、50四、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另外一臺服務器,實現故障轉移。 proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; proxy_cache_valid 200 304 12h; #對不一樣的HTTP狀態碼設置不一樣的緩存時間 proxy_cache_key $host$uri$is_args$args; #以域名、URI、參數組合成Web緩存的Key值,Nginx根據Key值哈希,存儲緩存內容到二級緩存目錄內 #設置主機頭和客戶端真實地址 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_buffering on; #禁用緩存 proxy_pass #後端server expires 1d; #緩存,至關於cookies,1d表示1天,1m表示1分鐘(不肯定) } #location ~ /purge(/.*) { #使用purge,用於清除緩存,須要編譯ngx_cache_purge # allow 127.0.0.1; # deny all; # proxy_cache_purge cache_one $host$1$is_args$args; #} } } 建立目錄: #mkdir -pv /usr/local/nginx/{proxy_cache_dir,proxy_temp_dir} 重啓nginx,確保成功! #service nginx restart
而後到18.144和18.145的網頁存放路徑分別建立test.html,內容以下:
18.144 #cat /usr/share/nginx/html/test.html <h1>This is a test page 18.144</h1> 18.145 #cat /usr/share/nginx/html/test.html <h1>This is a test page 18.145</h1>
而後經過負載均衡域名來訪問,http://balance.test.com/test.html 顯示頁面以下:
而後不停刷新或者更換不一樣的瀏覽器,一直都是18.145這臺server響應,由於設置了緩存,第一次訪問以後,就會將數據緩存起來,直到設置的緩存過時時間生效爲止,而後這個時候到負載均衡服務器查看緩存信息:
#cd /usr/local/nginx/ #ll proxy_cache_dir/9/41/ total 4 -rw------- 1 nginx nginx 444 Nov 4 16:53 b5e4d782cfd84da92097d8ed956fb419 能夠看到緩存文件已經生成了!
查看緩存內容,以下圖:
能夠看到緩存的是18.145這個頁面!
如今將緩存手動刪除,那麼更換瀏覽器訪問http://balance.test.com/test.html 會出現18.144響應的頁面,以下:
#cd /usr/local/nginx #rm -rf proxy_cache_dir/* 訪問頁面以下:
查看緩存內容,以下圖:
到這裏就結束了!不足之處,請多多指教!