使用nginx cache缓存网站数据实践
Nginx本身就有緩存功能,能夠緩存靜態(tài)對象,比如圖片、CSS、JS等內容直接緩存到本地,下次訪問相同對象時,直接從緩存即可,無需訪問后端靜態(tài)服務器以及存儲存儲服務器,可以替代squid功能。
1??環(huán)境準備
我們這里只測試nginx的proxy_cache的緩存功能,所以結構越簡單越好,這里我們只需要準備一臺nginx的虛擬機即可,如果沒有nginx,那么我們可以使用epel源,yum安裝一個即可:
#添加epel源 root@~>>?wget?-O?/etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-6.repo #yum安裝nginx root@~>>?yum?install?nginx?-y #rpm?-ql查看主要配置文件位置 root@~>>?rpm?-ql?nginx 這里為了簡單,只使用簡單的nginx.conf配置文件: root@nginx>>?cat?nginx.conf user??????????????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;}} }啟動查看初始界面是否正常:
root@nginx>>?nginx root@nginx>>?netstat?-tupln|grepnginx tcp???????0??????0?0.0.0.0:80???????????0.0.0.0:*???????????????????LISTEN??????1043/nginx root@nginx>>?curl?-I?192.168.16.199 HTTP/1.1?200?OK Server:?nginx/1.0.15 Date:?Mon,?14?Sep?2015?09:40:53?GMT Content-Type:?text/html Content-Length:?3698 Last-Modified:?Tue,?16?Jun?2015?21:34:15GMT Connection:?keep-alive Accept-Ranges:?bytes一切正常,首頁有2張圖片,正好用于實驗:
root@html>>?tree/usr/share/nginx/html/ /usr/share/nginx/html/ |--?404.html |--?50x.html |--?index.html |--?nginx-logo.png `--?poweredby.png至此環(huán)境準備完畢。
2??配置cache
2.1??創(chuàng)建目錄并掛載tmpfs
nginx的proxy_cache是基于內存和磁盤的緩存,需要指定緩存目錄和臨時目錄:
root@nginx>> mkdir /tmp/{ngx_tmp,ngx_cache}-p
緩存存放于磁盤,磁盤IO會影響緩存的速度,所以我們在將tmpfs掛載于ngx_cache目錄上來加速緩存的讀取和寫入:
root@nginx>>?mount?-t?tmpfs?-osize=100M?tmpfs?/tmp/ngx_cache root@nginx>>?mount|grep?tmpfs tmpfs?on?/dev/shm?type?tmpfs?(rw) tmpfs?on?/tmp/ngx_cache?type?tmpfs?(rw,size=100M)2.2??配置緩存目錄大小以及key空間名
將下面配置放至http標簽中:
root@nginx>> 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??配置反向代理
首先配置upstream節(jié)點池:
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;?#出錯嘗試下一個節(jié)點 proxy_cache?cache_one;??????#緩存鍵空間名 proxy_cache_valid?200?304?12h;?#指定對應狀態(tài)碼的緩存時間 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;????????#傳遞主機名給后端節(jié)點 proxy_set_header?X-Forwarded-For$remote_addr;?#傳遞客戶端IP給后端節(jié)點 expires?1d;?#超期時間最終的nginx.conf配置文件如下:
root@nginx>>?cat?nginx.conf user??????????????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??配置日志
為了觀察緩存的命中狀態(tài),我們可以將緩存相關的變量記錄在日志中。
定義日志格式:
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記錄分發(fā)的后端節(jié)點IP;upstream_status記錄后端節(jié)點返回的狀態(tài)碼;upstream_cache_status記錄緩存的命中情況。
在反向代理標簽中引用日志:
access_log?/var/log/nginx/cache_access.log? main;
nginx重新加載配置:
root@nginx>> nginx -s reload
2.5??監(jiān)測緩存
監(jiān)測緩存文件的事件
瀏覽網站:
root@ngx_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
總結
以上是生活随笔為你收集整理的使用nginx cache缓存网站数据实践的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iPhone 15 CAD渲染图出炉:屏
- 下一篇: 微软分享新版必应开发故事:ChatGPT