Nginx Proxy緩存的具體實現(xiàn)
Proxy緩存
緩存類型
- 網(wǎng)頁緩存 (公網(wǎng))CDN
- 數(shù)據(jù)庫緩存 memcache redis
- 網(wǎng)頁緩存 nginx-proxy
- 客戶端緩存 瀏覽器緩存
模塊
- ngx_http_proxy_module
語法
緩存開關(guān) Syntax: proxy_cache zone | off; Default: proxy_cache off; Context: http, server, location 代理緩存 Syntax: proxy_cache_path path [levels=levels] keys_zone=name:size[inactive=time] [max_size=size] [manager_files=number] Default: — Context: http example:proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m; 緩存維度 Syntax: proxy_cache_key string; 定義緩存唯一key,通過唯一key來進行hash存取,緩存文件名 Default: proxy_cache_key $scheme$proxy_host$request_uri; Context: http, server, location 緩存過期 Syntax: proxy_cache_valid [code ...] time; Default: — Context: http, server, location proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m;
啟動緩存
1 延續(xù)代理實驗
2 設置nginx-2為緩存服務器
vim /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log notice;
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"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
#開啟反向代理緩存
proxy_cache_path /app/limou/cache levels=1:2 keys_zone=proxy_cache:10m max_size=10g inactive=2h use_temp_path=off;
}
proxy_cache_path命令中的參數(shù)及對應配置說明如下:
1、proxy_cache_path /app/laochen/cache:
指定了緩存文件存儲的路徑為 /app/laochen/cache。
2、levels=1:2:
設置了緩存目錄的層級結(jié)構(gòu)。這里 levels=1:2 表示在緩存目錄下,使用兩個級別的子目錄來存儲緩存文件。第一級目錄有 1 個字符(例如 A),第二級目錄有 2 個字符(例如 00),這種結(jié)構(gòu)有助于管理大量緩存文件,避免單個目錄中文件過多。
3、keys_zone=proxy_cache:10m:
定義了一個名為 proxy_cache 的共享內(nèi)存區(qū)域,用于存儲緩存鍵的元數(shù)據(jù)(例如緩存的路徑、過期時間等)。10m 表示這個內(nèi)存區(qū)域的大小為 10 兆字節(jié)。
4、max_size=10g:
設置了緩存的最大總大小為 10 GB。超過這個大小的緩存會被清理,以保持總緩存大小在限制之內(nèi)。
5、inactive=60m:
定義了緩存的過期時間。如果緩存項在 60 分鐘內(nèi)沒有被訪問,它將被標記為過期并最終被清理。
6、use_temp_path=off:
表示緩存的臨時文件不使用臨時路徑。默認情況下,Nginx 會在寫入緩存文件時先使用臨時文件,如果設置為 off,則直接寫入最終緩存路徑。
vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
#開啟反向代理緩存
# Proxy_cache 使用名為 的對應緩存配置
proxy_cache proxy_cache;
# proxy_cache_valid 200 206 304 301 302 12h;
# 對http狀態(tài)碼為200、304…的內(nèi)容緩存12小時
proxy_cache_valid 200 304 12h;
# 設置不同相應碼的緩存時間,除了上面配置12小時的,其他的的存10分鐘
proxy_cache_valid any 10m;
# proxy_cache_key $uri 定義緩存唯一key,通過唯一key來進行hash存取
proxy_cache_key $host$uri$is_args$args;
# add_header:判斷數(shù)據(jù)包是否緩存了該信息
#緩存命中情況如何在http頭中體現(xiàn),以及在nginx日志中查看
add_header Nginx-Cache "$upstream_cache_status";
# proxy_next_upstream 出現(xiàn)502-504或錯誤,會跳過此臺服務器訪問下一臺服務器
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
mkdir -p /app/limou/cache
- 準備緩存文件的存放目錄
systemctl restart nginx
- 重啟服務器
3 使用PC客戶機,再次訪問nginx-2服務器
4 通過PC客戶機瀏覽器開發(fā)者功能。觀察是否命中緩存。
未命中miss

命中hit

提示:新創(chuàng)建的網(wǎng)頁文件,初次訪問均為miss。
nginx緩存工作原理
未啟動緩存
啟動緩存第一次查詢
- 第一次訪問,proxy_cache并沒有找到對應的緩存文件(未命中緩存MISS),所以當?shù)谝淮握埱笸瓿傻耐瑫r,proxy_cache會保持緩存:
啟動緩存第二次查詢
- 同一個url第二次訪問,當同一個文件再次到達源站,proxy_cache就會找到其對應的緩存文件(命中緩存HIT)直接返回給請求端,無需再執(zhí)行php程序
到此這篇關(guān)于Nginx Proxy緩存的具體實現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx Proxy緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx調(diào)用php-fpm出錯解決方法和nginx配置詳解
這篇文章介紹了nginx調(diào)用php-fpm出錯的解決方法,最后給出了nginx配置方法,需要的朋友可以參考下2014-03-03

