搭建一個具有快取功能的反向代理服務
前提:需要搭建和部署nginx服務以及後端服務,以便測試反向代理使用,這部分工作省略。
一. 設定快取空間
在nginx的http模組中開闢一塊名為my_cache的快取空間,快取路徑為/tmp/nginxcache,大小為10m。
# 設定快取路徑及其他快取引數。 # levels定義快取記憶體的級別,每個級別接收值1或2, # keys_zone設定共享快取名稱和大小,所有活動的key和資訊被儲存再快取中。 # max_size設定最大快取大小 proxy_cache_path/tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=1g;
二. 設定快取
在nginx server模組設定反向代理快取
proxy_passhttp://127.0.0.1:3000;#代理的後端服務 proxy_cache my_cache ;#定義用於快取的共享快取區域,預設為proxy_cache off proxy_cache_key $host$uri$is_args$args;#設定快取key proxy_cache_valid 200 304 302 5m;#設定不同響應碼的快取時間 proxy_cache_valid 404 10s;
三. 重啟nginx,測試
nginx -s reload siege http://onlinetest.guoew.cc/ -c 500 -t 5 Lifting the server siege...done.Transactions:129470 hits Availability:100.00 %#成功率 Elapsed time:299.03 secs#執行時間 Data transferred:6918.02 MB#資料傳輸大小 Response time:0.65 secs#平均響應時間 Transaction rate:432.97 trans/sec#每秒處理請求數 Throughput:23.13 MB/sec#平均每秒傳輸資料量 Concurrency:282.00#實際最高併發 Successful transactions:129470#成功處理次數 Failed transactions:0#失敗處理次數 Longest transaction:17.72#傳輸所花最長時間 Shortest transaction:0.00#傳輸所花最短時間
四. 不使用快取,再進行測試
不敢直視,siege 500併發幾乎全部報錯,降低到併發100才能勉強進行測試。
ransactions:8389 hits Availability:100.00 % Elapsed time:299.32 secs Data transferred:356.36 MB Response time:3.05 secs Transaction rate:28.03 trans/sec Throughput:1.19 MB/sec Concurrency:85.60 Successful transactions:6721 Failed transactions:0 Longest transaction:18.60 Shortest transaction:0.00