Nginx代理緩存功能


Nginx代理緩存功能
      Nginx緩存主要是用於減輕后端服務器的負載,提高網站並發量,提升用戶體驗度。

注意:Nginx反向代理的緩存功能是由ngx_http_proxy_module提供,在使用緩存功能時務必要nginx支持該模塊。可能有些選項的不支持Nginx的版本,具體看官方文檔: http://nginx.org/en/docs/http/ngx_http_proxy_module.html

后端服務器可能無法承受負載
為了更好的提升用戶體驗
環境介紹

                 服務器IP            服務器角色

               192.168.123.36           代理服務器

               192.168.123.34           上游服務器

                                                                                部署環境配置表 1-0                                                                 

Proxy cache配置

    1.server配置

proxy_cache_path /cache/nginx/ levels=1:2 keys_zone=mycache:64m;
server {
listen 80;
location / {
proxy_cache mycache;
proxy_pass http://192.168.123.34:80/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_methods GET HEAD;
proxy_cache_revalidate on;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1m;
proxy_cache_min_uses 1;
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;

}
}
    2.參數詳解

proxy_cache_path /cache/nginx/ levels=1:2 keys_zone=mycache:64m;
#proxy_cache_path 為緩存存放路徑;
#levels的第一位表示使用1級子目錄冒號隔開第二位表示使用2級子目錄,其最多使用三級,1表示每個一級子目錄的名字只能使用1個字符;
#key_zone中的mycache為緩存名字,可以在location或者server中通過proxy_cache引用;64m表示用多少內存空間存儲nginx key;

proxy_cache mycache;
#引用mycache緩存空間;

proxy_pass http://192.168.123.34:80/;
#將來自 / 的請求代理至192.168.123.34:80 該服務器,后面的 '/' 是必須的;

proxy_set_header Host $host;
#用於后端的real server區分不同的虛擬主機;

proxy_set_header X-Real-IP $remote_addr;
#記錄客戶端真實ip地址,而不是代理服務器地址,需要后端web服務器開啟日志相應功能接收;

proxy_cache_methods GET HEAD;
#表示對客戶端請求的GET 和 HEAD方法進行緩存;

proxy_cache_revalidate on;
#本地緩存過期會檢查后端服務器該緩存是否存在,避免后端重傳占據帶寬;

proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1m;
#針對於不同的響應碼進行緩存不同的時間設定;

proxy_cache_min_uses 1;
#某一個請求被響應多少次才會被緩存,默認是1,可以將該值設置為大一些;

proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
#指明哪種場景可以使用過期緩存,提升用戶體驗;

補充:
proxy_hide_header;
#隱藏由proxy響應客戶端時指定的首部;

proxy_buffer 4|8k
#為了響應客戶端更快,服務器端響應客戶端可能分成多個ip報文響應,也可以整合在一起再一次響應;


Nginx緩存是鍵值存儲,URL是鍵,文件路徑是值。鍵值存儲的速度就是加快在文件系統中查找的速度。所以,存儲的key是哈希過的值。

3.平滑重啟nginx

[root@localhost nginx]# nginx -s reload
[root@localhost nginx]# ps -elf|grep nginx
1 S root 10175 1 0 80 0 - 27830 sigsus 09:52 ? 00:00:00 nginx: master process nginx
5 S www 11165 10175 0 80 0 - 28893 ep_pol 18:10 ? 00:00:00 nginx: worker process
5 S www 11166 10175 0 80 0 - 28893 ep_pol 18:10 ? 00:00:00 nginx: worker process
5 S www 11167 10175 0 80 0 - 27830 ep_pol 18:10 ? 00:00:00 nginx: cache manager process
重啟完成這里會多一個cache manager,其主要作用和memcached的LRU算法相似,刪除過期緩存。而如果緩存沒過期其上有服務器數據發生變化則依舊訪問是錯誤的數據。可以通過程序實現。

4. 后端服務器配置靜態頁面

    4.1 虛擬主機配置

server {
listen 80;
server_name www.test.com;
location / {
root html/bbs;
index index.html index.htm;
}
}
    4.2 頁面內容

<h1>i am node1</h1>
    4.3 物理機嘗試訪問

 

 

 

 

    #可以在代理服務器里查看到緩存的key

  #此時修改index內容客戶端請求並不會發生改變

<h1>i am node2 node2!!!</h1>
   #客戶端清理緩存再次請求

 

 

 

5.在不使用緩存與使用緩存的對比測速

    1.分別處理10000個請求1000個並發對比,先是不使用緩存的情況

[root@localhost nginx]# ab -n 10000 -c 1000 http://www.test.com/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.test.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software: nginx/1.6.3
Server Hostname: www.test.com
Server Port: 80

Document Path: /index.html
Document Length: 20 bytes

Concurrency Level: 1000
Time taken for tests: 4.393 seconds
Complete requests: 10000
Failed requests: 836
(Connect: 0, Receive: 0, Length: 836, Exceptions: 0)
Write errors: 0
Non-2xx responses: 836
Total transferred: 2586108 bytes
HTML transferred: 343792 bytes
Requests per second: 2276.42 [#/sec] (mean)
Time per request: 439.286 [ms] (mean)
Time per request: 0.439 [ms] (mean, across all concurrent requests)
Transfer rate: 574.91 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 114 312.3 6 3018
Processing: 11 116 283.4 51 3038
Waiting: 1 112 283.4 48 3038
Total: 18 230 482.0 64 4019

Percentage of the requests served within a certain time (ms)
50% 64
66% 78
75% 93
80% 105
90% 1054
95% 1261
98% 2066
99% 2315
100% 4019 (longest request)

#在不使用緩存的情況下耗費了4.393秒,值可以多測試幾次對比
    2.使用緩存

[root@localhost nginx]# ab -n 10000 -c 1000 http://www.test.com/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.test.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software: nginx/1.6.3
Server Hostname: www.test.com
Server Port: 80

Document Path: /index.html
Document Length: 20 bytes

Concurrency Level: 1000
Time taken for tests: 1.179 seconds
Complete requests: 10000
Failed requests: 651
(Connect: 0, Receive: 0, Length: 651, Exceptions: 0)
Write errors: 0
Total transferred: 2337250 bytes
HTML transferred: 186980 bytes
Requests per second: 8483.63 [#/sec] (mean)
Time per request: 117.874 [ms] (mean)
Time per request: 0.118 [ms] (mean, across all concurrent requests)
Transfer rate: 1936.36 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 66 195.8 24 1044
Processing: 11 31 11.0 30 249
Waiting: 0 22 11.3 22 239
Total: 35 97 194.3 55 1070

Percentage of the requests served within a certain time (ms)
50% 55
66% 59
75% 62
80% 64
90% 84
95% 107
98% 1062
99% 1066
100% 1070 (longest request)
#可以看到在使用緩存的情況下處理10000個請求1000的並發耗時為1.179秒!!!

注意:在使用到緩存的時候務必考慮緩存過期!根據生產場景使用

有需要交流的小伙伴可以點擊這里加本人QQ:luke


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM