Nginx 之五: Nginx服务器的负载均衡、缓存与动静分离功能
一、負(fù)載均衡:
通過反向代理客戶端的請(qǐng)求到一個(gè)服務(wù)器群組,通過某種算法,將客戶端的請(qǐng)求按照自定義的有規(guī)律的一種調(diào)度調(diào)度給后端服務(wù)器。
Nginx的負(fù)載均衡使用upstream定義服務(wù)器組,后面跟著組名,組名后面是大括號(hào)包起來的服務(wù)器列表,每個(gè)服務(wù)器使用server開頭,后面跟定義的服務(wù)器名字、服務(wù)器IP:Port、參數(shù);
1:upstream要寫在Server塊的外面,可以有多個(gè),名稱不同即可,如下:
upstream webserver {server 192.168.0.201;server 192.168.0.202; }server {server_name hfnginx.chinacloudapp.cn;#access_log logs/host.access.log main;location / { #首頁負(fù)載之后端服務(wù)器proxy_pass http://webserver; #通過upstrean定義的服務(wù)器組名調(diào)用后端服務(wù)器proxy_set_header X-Real-IP $remote_addr; #傳遞客戶端的ip地址}location ~* ^/form { #后端Web服務(wù)器要有此目錄proxy_pass http://webserver;proxy_set_header X-Real-IP $remote_addr;} }1.1:后端服務(wù)器要準(zhǔn)備好首頁和form目錄
1.2:訪問首頁測(cè)試:
? ??
1.3:訪問form目錄測(cè)試:
??
?1.4:nginx支持的三種負(fù)載方式:
round-robin:輪訓(xùn)調(diào)度,默認(rèn) ip_hash:會(huì)話綁定 least_conn:最少會(huì)話鏈接1.5:backup服務(wù)器:
upstream webserver {server 192.168.0.201 weight=1 max_fails=2 fail_timeout=2;server 192.168.0.202 weight=1 max_fails=2 fail_timeout=2;server 127.0.0.1:9008 backup; #調(diào)用backup服務(wù)器,可以是本機(jī)或其他服務(wù)器。 } server {listen 9008;server_name localhost;root html/error;index index.html; }[root@hfnginx nginx]# cat html/error/index.html #backup服務(wù)器的內(nèi)容 Error Page!測(cè)試:將服務(wù)器組內(nèi)的其他服務(wù)器關(guān)閉,訪問如下:
1.6:實(shí)現(xiàn)動(dòng)靜分離:
upstream web {server 192.168.0.1 weight=1 max_fails=2 fail_timeout=2;server 192.168.0.2 weight=1 max_fails=2 fail_timeout=2; } upstream image {server 192.168.0.3 weight=1 max_fails=2 fail_timeout=2;server 192.168.0.4 weight=1 max_fails=2 fail_timeout=2; } upstream php {server 192.168.0.5 weight=1 max_fails=2 fail_timeout=2;server 192.168.0.6 weight=1 max_fails=2 fail_timeout=2; } location /{root html/web;index index.php index.html; }location ~* \.php$ {fastcgi_proxy http://php; }location ~* "\.(.jpg|png|jpeg|gif)" {proxy_pass http://image; }1.7:實(shí)現(xiàn)數(shù)據(jù)緩存:
緩存是緩存nginx服務(wù)器接收請(qǐng)求過的數(shù)據(jù),數(shù)據(jù)超時(shí)時(shí)間不能太長(zhǎng),因?yàn)閿?shù)據(jù)可能會(huì)發(fā)生變化,但是nginx服務(wù)器內(nèi)部的緩存的數(shù)據(jù)還沒有更細(xì),會(huì)導(dǎo)致客戶端請(qǐng)求的數(shù)據(jù)不是最新數(shù)據(jù)的問題,數(shù)據(jù)緩存目錄不能定義在server快內(nèi),要定義在http塊中。
[root@hfnginx nginx]# grep -v "#" conf/conf.d/hfnginx.conf | grep -v "^$" upstream webserver {server 192.168.0.201 weight=1 max_fails=2 fail_timeout=2;server 192.168.0.202 weight=1 max_fails=2 fail_timeout=2; } server {listen 9008;server_name localhost;root html/error;index index.html; }proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g; #緩存目錄不能定義在server塊內(nèi),要定義在http塊中
#/nginx/cache/first定義緩存目錄參數(shù) #evels=1:2 定義兩層目錄,第一層一個(gè)字符名稱,第二個(gè)兩個(gè)字符名稱 #keys_zone=first:20m 每個(gè)緩存都是一個(gè)共享內(nèi)存空間。這就是用戶定義共享內(nèi)存空間地址的名稱 #max_size=1g 定義目錄最大空間為1g,因?yàn)榫彺婵臻g越大搜索數(shù)據(jù)越慢,因此不宜太大。 server {server_name hfnginx.chinacloudapp.cn;location / {add_header X_Via $server_addr; #添加服務(wù)器地址到報(bào)文頭部add_header X-Cache $upstream_cache_status; #添加緩存狀態(tài)到報(bào)文頭部proxy_pass http://webserver;proxy_cache first; #調(diào)用緩存proxy_cache_valid 200 10m; #定義緩存失效時(shí)間,200是狀態(tài)碼,即緩存狀態(tài)碼是200請(qǐng)求成功的數(shù)據(jù),10m是10分鐘,即緩存的數(shù)據(jù)的超時(shí)時(shí)間10分鐘,10分鐘后即過期,不定義則緩存不生效 }location ~* ^/form {proxy_cache cache_one;proxy_pass http://webserver;proxy_set_header X-Real-IP $remote_addr;} }
?測(cè)試緩存:
注:X_Via返回的響應(yīng)客戶端請(qǐng)求報(bào)文的服務(wù)器,將有Nginx構(gòu)建報(bào)文響應(yīng)客戶端請(qǐng)求,所以顯示的是Nginx服務(wù)器的IP地址,X-Cache標(biāo)記是否緩存,HIT是緩存過的數(shù)據(jù),MISS是沒有緩存的數(shù)據(jù)。
把緩存刪除,重新訪問,將返回沒有緩存的數(shù)據(jù):
?刷新后再次訪問:
1.8:另外常用的三種緩存:
open_log_cache:日志緩存,降低磁盤IO open_file_cache:打開文件句柄緩存,將文件緩存至 Nginx管理的內(nèi)存當(dāng)中加速響應(yīng) fastcgi_cache:緩存后端php服務(wù)器的內(nèi)容,當(dāng)時(shí)如果php內(nèi)容發(fā)生更改則會(huì)導(dǎo)致客戶端訪問的頁面不是最新的,因此要慎用。另外Nginx的limit限制也是基于內(nèi)存共享來實(shí)現(xiàn)的
?
轉(zhuǎn)載于:https://www.cnblogs.com/myblog1314/p/11162080.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Nginx 之五: Nginx服务器的负载均衡、缓存与动静分离功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 426. 将二叉搜索树
- 下一篇: 【线段树】【FeyatCup】——2.法