Nginx 之五: Nginx服务器的负载均衡、缓存与动静分离功能
一、負載均衡:
通過反向代理客戶端的請求到一個服務器群組,通過某種算法,將客戶端的請求按照自定義的有規律的一種調度調度給后端服務器。
Nginx的負載均衡使用upstream定義服務器組,后面跟著組名,組名后面是大括號包起來的服務器列表,每個服務器使用server開頭,后面跟定義的服務器名字、服務器IP:Port、參數;
1:upstream要寫在Server塊的外面,可以有多個,名稱不同即可,如下:
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 / { #首頁負載之后端服務器proxy_pass http://webserver; #通過upstrean定義的服務器組名調用后端服務器proxy_set_header X-Real-IP $remote_addr; #傳遞客戶端的ip地址}location ~* ^/form { #后端Web服務器要有此目錄proxy_pass http://webserver;proxy_set_header X-Real-IP $remote_addr;} }1.1:后端服務器要準備好首頁和form目錄
1.2:訪問首頁測試:
? ??
1.3:訪問form目錄測試:
??
?1.4:nginx支持的三種負載方式:
round-robin:輪訓調度,默認 ip_hash:會話綁定 least_conn:最少會話鏈接1.5:backup服務器:
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; #調用backup服務器,可以是本機或其他服務器。 } server {listen 9008;server_name localhost;root html/error;index index.html; }[root@hfnginx nginx]# cat html/error/index.html #backup服務器的內容 Error Page!測試:將服務器組內的其他服務器關閉,訪問如下:
1.6:實現動靜分離:
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:實現數據緩存:
緩存是緩存nginx服務器接收請求過的數據,數據超時時間不能太長,因為數據可能會發生變化,但是nginx服務器內部的緩存的數據還沒有更細,會導致客戶端請求的數據不是最新數據的問題,數據緩存目錄不能定義在server快內,要定義在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塊內,要定義在http塊中
#/nginx/cache/first定義緩存目錄參數 #evels=1:2 定義兩層目錄,第一層一個字符名稱,第二個兩個字符名稱 #keys_zone=first:20m 每個緩存都是一個共享內存空間。這就是用戶定義共享內存空間地址的名稱 #max_size=1g 定義目錄最大空間為1g,因為緩存空間越大搜索數據越慢,因此不宜太大。 server {server_name hfnginx.chinacloudapp.cn;location / {add_header X_Via $server_addr; #添加服務器地址到報文頭部add_header X-Cache $upstream_cache_status; #添加緩存狀態到報文頭部proxy_pass http://webserver;proxy_cache first; #調用緩存proxy_cache_valid 200 10m; #定義緩存失效時間,200是狀態碼,即緩存狀態碼是200請求成功的數據,10m是10分鐘,即緩存的數據的超時時間10分鐘,10分鐘后即過期,不定義則緩存不生效 }location ~* ^/form {proxy_cache cache_one;proxy_pass http://webserver;proxy_set_header X-Real-IP $remote_addr;} }
?測試緩存:
注:X_Via返回的響應客戶端請求報文的服務器,將有Nginx構建報文響應客戶端請求,所以顯示的是Nginx服務器的IP地址,X-Cache標記是否緩存,HIT是緩存過的數據,MISS是沒有緩存的數據。
把緩存刪除,重新訪問,將返回沒有緩存的數據:
?刷新后再次訪問:
1.8:另外常用的三種緩存:
open_log_cache:日志緩存,降低磁盤IO open_file_cache:打開文件句柄緩存,將文件緩存至 Nginx管理的內存當中加速響應 fastcgi_cache:緩存后端php服務器的內容,當時如果php內容發生更改則會導致客戶端訪問的頁面不是最新的,因此要慎用。另外Nginx的limit限制也是基于內存共享來實現的
?
轉載于:https://www.cnblogs.com/myblog1314/p/11162080.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Nginx 之五: Nginx服务器的负载均衡、缓存与动静分离功能的全部內容,希望文章能夠幫你解決所遇到的問題。

- 上一篇: LeetCode 426. 将二叉搜索树
- 下一篇: 【线段树】【FeyatCup】——2.法