標籤:cache nginx 老男孩教育
Nginx本身就有緩衝功能,能夠緩衝靜態對象,比片、CSS、JS等內容直接緩衝到本地,下次訪問相同對象時,直接從緩衝即可,無需訪問後端靜態伺服器以及儲存儲存伺服器,可以替代squid功能。
1 環境準備
我們這裡只測試nginx的proxy_cache的緩衝功能,所以結構越簡單越好,這裡我們只需要準備一台nginx的虛擬機器即可,如果沒有nginx,那麼我們可以使用epel源,yum安裝一個即可:
#添加epel源[email protected]~>> wget -O /etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-6.repo#yum安裝nginx[email protected]~>> yum install nginx -y#rpm -ql查看主要設定檔位置[email protected]~>> rpm -ql nginx這裡為了簡單,只使用簡單的nginx.conf設定檔:[email protected]>> cat nginx.confuser nginx;worker_processes 1;error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;events { worker_connections 1024;}http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local]"$request" ‘ ‘$status $body_bytes_sent"$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; sendfile on; keepalive_timeout 65; server { listen 80; location / { root /usr/share/nginx/html; index index.html index.htm; } }}
啟動查看初始介面是否正常:
[email protected]>> nginx[email protected]>> netstat -tupln|grepnginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1043/nginx[email protected]>> curl -I 192.168.16.199HTTP/1.1 200 OKServer: nginx/1.0.15Date: Mon, 14 Sep 2015 09:40:53 GMTContent-Type: text/htmlContent-Length: 3698Last-Modified: Tue, 16 Jun 2015 21:34:15GMTConnection: keep-aliveAccept-Ranges: bytes
一切正常,首頁有2張圖片,正好用於實驗:
[email protected]>> tree/usr/share/nginx/html//usr/share/nginx/html/|-- 404.html|-- 50x.html|-- index.html|-- nginx-logo.png`-- poweredby.png
至此環境準備完畢。
2 配置cache2.1 建立目錄並掛載tmpfs
nginx的proxy_cache是基於記憶體和磁碟的緩衝,需要指定緩衝目錄和臨時目錄:
[email protected]>> mkdir /tmp/{ngx_tmp,ngx_cache}-p
緩衝存放於磁碟,磁碟IO會影響緩衝的速度,所以我們在將tmpfs掛載於ngx_cache目錄上來加速緩衝的讀取和寫入:
[email protected]>> mount -t tmpfs -osize=100M tmpfs /tmp/ngx_cache[email protected]>> mount|grep tmpfstmpfs on /dev/shm type tmpfs (rw)tmpfs on /tmp/ngx_cache type tmpfs (rw,size=100M)
2.2 配置緩衝目錄大小以及key空間名
將下面配置放至http標籤中:
[email protected]>> grep proxy_cache_pathnginx.conf
proxy_cache_path /tmp/ngx_cache levels=1:2 keys_zone=cache_one:100minactive=1d max_size=5g;
#指定緩衝目錄,緩衝等級,鍵空間名,鍵空間大小,失效時間,以及磁碟最大緩衝大小
2.3 配置反向 Proxy
首先配置upstream節點池:
upstream server_pool { server 127.0.0.1:8080;}
在server標籤的location段中配置代理:
proxy_pass http://server_pool;
配置8080連接埠的標籤:
server { listen 8080; location / { root /usr/share/nginx/html; index index.html index.htm; } access_log /var/log/nginx/access.log main;}
配置proxy_cache相關參數啟用緩衝:
proxy_pass http://server_pool;proxy_next_upstream http_502 http_504error timeout invalid_header; #出錯嘗試下一個節點proxy_cache cache_one; #緩衝鍵空間名proxy_cache_valid 200 304 12h; #指定對應狀態代碼的緩衝時間proxy_cache_valid 301 302 1m;proxy_cache_valid any 1m;proxy_cache_key $host$uri$is_args$args; #指定鍵key的格式proxy_set_header Host $host; #傳遞主機名稱給後端節點proxy_set_header X-Forwarded-For$remote_addr; #傳遞用戶端IP給後端節點expires 1d; #超期時間
最終的nginx.conf設定檔如下:
[email protected]>> cat nginx.confuser nginx;worker_processes 1;error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;events { worker_connections 1024;}http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local]"$request" ‘ ‘$status $body_bytes_sent"$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘ ‘"addr:$upstream_addr-status:$upstream_status-cachestatus:$upstream_cache_status"‘; sendfile on; keepalive_timeout 65; proxy_cache_path /tmp/ngx_cache levels=1:2 keys_zone=cache_one:100m inactive=1dmax_size=5g; upstream server_pool { server 127.0.0.1:8080; } server { listen 80; location / { proxy_passhttp://server_pool; proxy_next_upstreamhttp_502 http_504 error timeout invalid_header; proxy_cache cache_one; proxy_cache_valid 200304 12h; proxy_cache_valid 301302 1m; proxy_cache_valid any 1m; proxy_cache_key$host$uri$is_args$args; proxy_set_header Host$host; proxy_set_headerX-Forwarded-For $remote_addr; expires 1d; } access_log /var/log/nginx/cache_access.log main; } server { listen 8080; location / { root/usr/share/nginx/html; index index.htmlindex.htm; } }}2.4 配置日誌
為了觀察緩衝的命中狀態,我們可以將緩衝相關的變數記錄在日誌中。
定義日誌格式:
log_format main ‘$remote_addr - $remote_user[$time_local] "$request" ‘ ‘$status $body_bytes_sent"$http_referer" ‘ ‘"$http_user_agent""$http_x_forwarded_for"‘ ‘"addr:$upstream_addr-status:$upstream_status-cachestatus:$upstream_cache_status"‘;
#其中upstream_addr記錄分發的後端節點IP;upstream_status記錄後端節點返回的狀態代碼;upstream_cache_status記錄緩衝的命中情況。
在反向 Proxy標籤中引用日誌:
access_log /var/log/nginx/cache_access.log main;
nginx重新載入配置:
[email protected]>> nginx -s reload
2.5 監測緩衝
監測快取檔案的事件
瀏覽網站:
[email protected]_cache>> inotifywait -mrq/tmp/ngx_cache//tmp/ngx_cache/ CREATE,ISDIR 6/tmp/ngx_cache/ OPEN,ISDIR 6/tmp/ngx_cache/ CLOSE_NOWRITE,CLOSE,ISDIR6/tmp/ngx_cache/ CREATE,ISDIR 1/tmp/ngx_cache/ OPEN,ISDIR 1/tmp/ngx_cache/ CLOSE_NOWRITE,CLOSE,ISDIR1/tmp/ngx_cache/ CREATE,ISDIR 3/tmp/ngx_cache/ OPEN,ISDIR 3/tmp/ngx_cache/ CLOSE_NOWRITE,CLOSE,ISDIR3/tmp/ngx_cache/3/ CREATE,ISDIR fd/tmp/ngx_cache/3/ OPEN,ISDIR fd/tmp/ngx_cache/3/CLOSE_NOWRITE,CLOSE,ISDIR fd/tmp/ngx_cache/3/fd/ CREATEdd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ OPENdd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ MODIFYdd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ CLOSE_WRITE,CLOSEdd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ MOVED_FROMdd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ MOVED_TOdd404cd351f6b9efb072e5806dc2efd3
說明:有最後幾行可知,圖片緩衝到目錄中。
提示:本內容來自老男孩教育營運23期、雲端運算與DevOps進階架構師課程13期學員筆記
更多內容請看老男孩教育的Linux課程 http://www.oldboyedu.com
本文出自 “老男孩linux培訓” 部落格,請務必保留此出處http://oldboy.blog.51cto.com/2561410/1884326
使用nginx cache緩衝網站資料實踐