日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用nginx cache缓存网站数据实践

發(fā)布時間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用nginx cache缓存网站数据实践 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


Nginx本身就有緩存功能,能夠緩存靜態(tài)對象,比如圖片、CSSJS等內(nèi)容直接緩存到本地,下次訪問相同對象時,直接從緩存即可,無需訪問后端靜態(tài)服務(wù)器以及存儲存儲服務(wù)器,可以替代squid功能。

1??環(huán)境準(zhǔn)備

我們這里只測試nginxproxy_cache的緩存功能,所以結(jié)構(gòu)越簡單越好,這里我們只需要準(zhǔn)備一臺nginx的虛擬機(jī)即可,如果沒有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張圖片,正好用于實(shí)驗(yàn):

root@html>>?tree/usr/share/nginx/html/ /usr/share/nginx/html/ |--?404.html |--?50x.html |--?index.html |--?nginx-logo.png `--?poweredby.png

至此環(huán)境準(zhǔn)備完畢。

2??配置cache

2.1??創(chuàng)建目錄并掛載tmpfs

nginxproxy_cache是基于內(nèi)存和磁盤的緩存,需要指定緩存目錄和臨時目錄:

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標(biāo)簽中:

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é)點(diǎn)池:

upstream?server_pool?{server?127.0.0.1:8080; }

server標(biāo)簽的location段中配置代理:

proxy_pass http://server_pool;

配置8080端口的標(biāo)簽:

server?{listen?8080;location?/?{root?/usr/share/nginx/html;index?index.html?index.htm;}access_log?/var/log/nginx/access.log??main; }

配置proxy_cache相關(guān)參數(shù)啟用緩存:

proxy_pass?http://server_pool; proxy_next_upstream?http_502?http_504error?timeout?invalid_header;?#出錯嘗試下一個節(jié)點(diǎn) proxy_cache?cache_one;??????#緩存鍵空間名 proxy_cache_valid?200?304?12h;?#指定對應(yīng)狀態(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;????????#傳遞主機(jī)名給后端節(jié)點(diǎn) proxy_set_header?X-Forwarded-For$remote_addr;?#傳遞客戶端IP給后端節(jié)點(diǎn) 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),我們可以將緩存相關(guān)的變量記錄在日志中。

定義日志格式:

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é)點(diǎn)IPupstream_status記錄后端節(jié)點(diǎn)返回的狀態(tài)碼;upstream_cache_status記錄緩存的命中情況。

在反向代理標(biāo)簽中引用日志:

access_log?/var/log/nginx/cache_access.log? main;

nginx重新加載配置:

root@nginx>> nginx -s reload

2.5??監(jiān)測緩存

監(jiān)測緩存文件的事件

瀏覽網(wǎng)站:

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

說明:有最后幾行可知,圖片緩存到目錄中。

提示:本內(nèi)容來自老男孩教育運(yùn)維23期、云計(jì)算與DevOps高級架構(gòu)師課程13期學(xué)員筆記

更多內(nèi)容請看老男孩教育的Linux課程 http://www.oldboyedu.com

總結(jié)

以上是生活随笔為你收集整理的使用nginx cache缓存网站数据实践的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。