Nginx Proxy緩存的具體實(shí)現(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來進(jìn)行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ù)代理實(shí)驗(yàn)
2 設(shè)置nginx-2為緩存服務(wù)器
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ù)及對應(yīng)配置說明如下:
1、proxy_cache_path /app/laochen/cache:
指定了緩存文件存儲的路徑為 /app/laochen/cache。
2、levels=1:2:
設(shè)置了緩存目錄的層級結(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:
設(shè)置了緩存的最大總大小為 10 GB。超過這個大小的緩存會被清理,以保持總緩存大小在限制之內(nèi)。
5、inactive=60m:
定義了緩存的過期時間。如果緩存項(xiàng)在 60 分鐘內(nèi)沒有被訪問,它將被標(biāo)記為過期并最終被清理。
6、use_temp_path=off:
表示緩存的臨時文件不使用臨時路徑。默認(rèn)情況下,Nginx 會在寫入緩存文件時先使用臨時文件,如果設(shè)置為 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 使用名為 的對應(yīng)緩存配置
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;
# 設(shè)置不同相應(yīng)碼的緩存時間,除了上面配置12小時的,其他的的存10分鐘
proxy_cache_valid any 10m;
# proxy_cache_key $uri 定義緩存唯一key,通過唯一key來進(jìn)行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或錯誤,會跳過此臺服務(wù)器訪問下一臺服務(wù)器
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
- 準(zhǔn)備緩存文件的存放目錄
systemctl restart nginx
- 重啟服務(wù)器
3 使用PC客戶機(jī),再次訪問nginx-2服務(wù)器
4 通過PC客戶機(jī)瀏覽器開發(fā)者功能。觀察是否命中緩存。
未命中miss

命中hit

提示:新創(chuàng)建的網(wǎng)頁文件,初次訪問均為miss。
nginx緩存工作原理
未啟動緩存
啟動緩存第一次查詢
- 第一次訪問,proxy_cache并沒有找到對應(yīng)的緩存文件(未命中緩存MISS),所以當(dāng)?shù)谝淮握埱笸瓿傻耐瑫r,proxy_cache會保持緩存:
啟動緩存第二次查詢
- 同一個url第二次訪問,當(dāng)同一個文件再次到達(dá)源站,proxy_cache就會找到其對應(yīng)的緩存文件(命中緩存HIT)直接返回給請求端,無需再執(zhí)行php程序
到此這篇關(guān)于Nginx Proxy緩存的具體實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx Proxy緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析阿里云centos7服務(wù)器nginx配置及常見問題解答
這篇文章主要介紹了阿里云centos7服務(wù)器nginx配置及常見問題解答,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Nginx通過header中的標(biāo)識進(jìn)行分發(fā)
本文主要介紹了Nginx通過header中的標(biāo)識進(jìn)行分發(fā),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
nginx調(diào)用php-fpm出錯解決方法和nginx配置詳解
這篇文章介紹了nginx調(diào)用php-fpm出錯的解決方法,最后給出了nginx配置方法,需要的朋友可以參考下2014-03-03
Nginx+Tomcat配置https的實(shí)現(xiàn)
本文主要介紹了Nginx+Tomcat配置https的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04

