1、負載均衡:php
經過反向代理客戶端的請求到一個服務器羣組,經過某種算法,將客戶端的請求按照自定義的有規律的一種調度調度給後端服務器。html
Nginx的負載均衡使用upstream定義服務器組,後面跟着組名,組名後面是大括號包起來的服務器列表,每一個服務器使用server開頭,後面跟定義的服務器名字、服務器IP:Port、參數;nginx
1:upstream要寫在Server塊的外面,能夠有多個,名稱不一樣便可,以下:web
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服務器:app
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限制也是基於內存共享來實現的