Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)
1. nginx模塊
--add-module=../modules/ngx_devel_kit-0.2.18 --add-module=../modules/set-misc-nginx-module-0.22rc8 --add-module=../modules/srcache-nginx-module-0.22 --add-module=../modules/redis-nginx-module-0.3.6 --add-module=../modules/redis2-nginx-module-0.10
2. redis安裝配置
# vim redis.conf
daemonize yes pidfile /var/run/redis-6379.pid port 6379 bind 127.0.0.1 timeout 0 tcp-keepalive 0 loglevel notice logfile stdout databases 16 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 maxmemory 8096mb maxmemory-policy volatile-ttl appendonly no appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
由于只把redis當(dāng)做緩存使用,因此沒(méi)有啟用持久化。
3. nginx配置
# vim nginx.conf
http
{
include 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" '
'"$gzip_ratio" $request_time $bytes_sent $request_length';
log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';
set_real_ip_from 10.0.0.0/8;
real_ip_header X-Forwarded-For;
include vhosts/test.jb51.net.conf;
}
# vim vhosts/test.jb51.net.conf
upstream redis {
server 127.0.0.1:6379;
keepalive 512;
}
server
{
listen 80;
server_name test.jb51.net;
index index.html index.htm index.php;
root /data/test.jb51.net/webroot;
location ~ .*\.php {
srcache_store_private on;
srcache_methods GET;
srcache_response_cache_control off;
if ($uri ~ /jb51.net/pp.php$){
set $key $request_uri;
set_escape_uri $escaped_key $key;
srcache_fetch GET /redis $key;
srcache_default_expire 172800;
srcache_store PUT /redis2 key=$escaped_key&exptime=$srcache_expire;
#add_header X-Cached-From $srcache_fetch_status;
#set_md5 $md5key $key;
#add_header X-md5-key $md5key;
#add_header X-Cached-Store $srcache_store_status;
#add_header X-Key $key;
#add_header X-Query_String $query_string;
#add_header X-expire $srcache_expire;
access_log /data/httplogs/test.jb51.net-photo-access.log srcache_log;
}
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location = /redis {
internal;
set_md5 $redis_key $args;
redis_pass redis;
}
location = /redis2 {
internal;
set_unescape_uri $exptime $arg_exptime;
set_unescape_uri $key $arg_key;
set_md5 $key;
redis2_query set $key $echo_request_body;
redis2_query expire $key $exptime;
redis2_pass redis;
}
error_log /data/httplogs/test.jb51.net-error.log;
access_log /data/httplogs/test.jb51.net-aceess.log main;
}
4. 測(cè)試
沒(méi)有做緩存狀態(tài):

有做緩存狀態(tài):

5. 響應(yīng)頭狀態(tài)
第一次請(qǐng)求:

再次請(qǐng)求:

6. 查看redis是否緩存以及過(guò)期時(shí)間

PS:srcache-nginx-module模塊指令說(shuō)明:
srcache_fetch
語(yǔ)法:srcache_fetch <method> <uri> <args>?
默認(rèn)值:no
配置段:http, server, location, location if
查詢緩存。返回200說(shuō)明緩存命中,直接從緩存響應(yīng)客戶端請(qǐng)求。非200需要后端程序處理。
srcache_fetch_skip
語(yǔ)法:srcache_fetch_skip <flag>
默認(rèn)值:srcache_fetch_skip 0
配置段:http, server, location, location if
<flag>支持nginx變量。當(dāng)這個(gè)參數(shù)值不為空和不等于0,則從緩存取數(shù)據(jù)過(guò)程被無(wú)條件跳過(guò)。
srcache_store
語(yǔ)法:srcache_store <method> <uri> <args>?
默認(rèn)值:no
配置段:http, server, location, location if
將當(dāng)前請(qǐng)求的響應(yīng)存入緩存。可以使用srcache_store_skip和srcache_store_max_size指令禁用緩存。不管是響應(yīng)狀態(tài)行,響應(yīng)頭,響應(yīng)體都會(huì)被緩存。默認(rèn)情況下,下列特殊響應(yīng)頭不會(huì)被緩存:
- Connection
- Keep-Alive
- Proxy-Authenticate
- Proxy-Authorization
- TE
- Trailers
- Transfer-Encoding
- Upgrade
- Set-Cookie
可以使用srcache_store_pass_header、srcache_store_hide_header指令來(lái)控制哪些頭要緩存哪些不要。
注意:即使所有的響應(yīng)數(shù)據(jù)被立即發(fā)送,當(dāng)前的nginx請(qǐng)求生命周期未必完成,直到srcache_store子請(qǐng)求完成。這意味著服務(wù)器端延遲關(guān)閉TCP連接,或下一個(gè)請(qǐng)求服務(wù)發(fā)送同一個(gè)TCP連接。
srcache_store_max_size
語(yǔ)法:srcache_store_max_size <size>
默認(rèn)值:srcache_store_max_size 0
配置段:http, server, location, location if
當(dāng)響應(yīng)體超過(guò)該值,將不會(huì)緩存。
當(dāng)后端緩存存儲(chǔ)有對(duì)緩存數(shù)據(jù)做硬限制,這個(gè)指令非常有用。比如memcached服務(wù)器,上限是1M。
默認(rèn)值0,不限制。
srcache_store_skip
語(yǔ)法:srcache_store_skip <flag>
默認(rèn)值:srcache_store_skip 0
配置段:http, server, location, location if
<flag>支持nginx變量。當(dāng)這個(gè)參數(shù)值不為空和不等于0,則從存入緩存過(guò)程被無(wú)條件跳過(guò)。
srcache_store_statuses
語(yǔ)法:srcache_store_statuses <status1> <status2> ..
默認(rèn)值:srcache_store_statuses 200 301 302
配置段:http, server, location, location if
該指令控制那些狀態(tài)碼響應(yīng)被緩存。
srcache_header_buffer_size
語(yǔ)法:srcache_header_buffer_size <size>
默認(rèn)值:srcache_header_buffer_size 4k/8k
配置段:http, server, location, location if
在序列化響應(yīng)頭時(shí)控制頭緩沖大小。默認(rèn)大小為頁(yè)面大小,通常為4k或8k,取決于具體平臺(tái)。
注意:該大小是以每個(gè)頭的,因此,需要足夠大來(lái)容納最大響應(yīng)頭。
srcache_store_hide_header
語(yǔ)法:srcache_store_hide_header <header>
默認(rèn)值:no
配置段:http, server, location, location if
默認(rèn)情況下,除了以下頭緩存所有響應(yīng)頭:
- Connection
- Keep-Alive
- Proxy-Authenticate
- Proxy-Authorization
- TE
- Trailers
- Transfer-Encoding
- Upgrade
- Set-Cookie
可以隱藏多個(gè)響應(yīng)頭,不區(qū)分大小寫(xiě)。如
srcache_store_hide_header X-Foo; srcache_store_hide_header Last-Modified;srcache_store_pass_header
語(yǔ)法:srcache_store_pass_header <header>
默認(rèn)值:no
配置段:http, server, location, location if
默認(rèn)情況下,除了以下頭緩存所有響應(yīng)頭:
- Connection
- Keep-Alive
- Proxy-Authenticate
- Proxy-Authorization
- TE
- Trailers
- Transfer-Encoding
- Upgrade
- Set-Cookie
可以緩存多個(gè)響應(yīng)頭,不區(qū)分大小寫(xiě)。如
srcache_store_pass_header Set-Cookie; srcache_store_pass_header Proxy-Autenticate;
srcache_methods
語(yǔ)法:srcache_methods <method>...
默認(rèn)值:srcache_methods GET HEAD
配置段:http, server, location
srcache_ignore_content_encoding
語(yǔ)法:srcache_ignore_content_encoding on|off
默認(rèn)值: srcache_ignore_content_encoding off
配置段:http, server, location, location if
內(nèi)容是否編碼。
建議后端服務(wù)器禁用gzip/deflate壓縮。在nginx.conf配置:
proxy_set_header Accept-Encoding "";
srcache_request_cache_control
語(yǔ)法:srcache_request_cache_control on|off
默認(rèn)值:srcache_request_cache_control off
配置段:http, server, location
當(dāng)該指令為on時(shí),請(qǐng)求頭Cache-Control和Pragma按照下面的方法處理:
1. srcache_fetch查詢緩存操作時(shí),當(dāng)請(qǐng)求頭Cache-Control: no-cache 、 Pragma: no-cache 將跳過(guò)。
2. srcache_store存入緩存操作時(shí),當(dāng)請(qǐng)求頭Cache-Control: no-store將跳過(guò)。
當(dāng)該指令為off時(shí),將禁用此功能,對(duì)于繁忙的站點(diǎn)依賴(lài)緩存加速被認(rèn)為是最安全的。
srcache_response_cache_control
語(yǔ)法:srcache_response_cache_control on|off
默認(rèn)值:srcache_response_cache_control on
配置段:http, server, location
當(dāng)該指令為on時(shí),響應(yīng)頭Cache-Control和Expires按照下面的方法處理:
Cache-Control: private skips srcache_store, Cache-Control: no-store skips srcache_store, Cache-Control: no-cache skips srcache_store, Cache-Control: max-age=0 skips srcache_store, Expires: <date-no-more-recently-than-now> skips srcache_store.
該指令優(yōu)先級(jí)比srcache_store_no_store,srcache_store_no_cache,srcache_store_private高。
srcache_store_no_store
語(yǔ)法:srcache_store_no_store on|off
默認(rèn)值:srcache_store_no_store off
配置段:http, server, location
開(kāi)啟該指令,將強(qiáng)制響應(yīng)頭Cache-Control: no-store。默認(rèn)為關(guān)閉。
srcache_store_no_cache
語(yǔ)法:srcache_store_no_cache on|off
默認(rèn)值:srcache_store_no_cache off
配置段:http, server, location
開(kāi)啟該指令,將強(qiáng)制響應(yīng)頭Cache-Control: no-cache。默認(rèn)為關(guān)閉。
srcache_store_private
語(yǔ)法:srcache_store_private on|off
默認(rèn)值:srcache_store_private off
配置段:http, server, location
開(kāi)啟該指令,將強(qiáng)制響應(yīng)頭Cache-Control: private。默認(rèn)為關(guān)閉。
srcache_default_expire
語(yǔ)法:srcache_default_expire <time>
默認(rèn)值:srcache_default_expire 60s
配置段:http, server, location, location if
控制默認(rèn)過(guò)期時(shí)間。當(dāng)響應(yīng)頭既沒(méi)有Cache-Control: max-age=N也沒(méi)有指定Expires時(shí),允許的$srcache_expire變量值。
該值必須小于597hours。
srcache_max_expire
語(yǔ)法:srcache_max_expire <time>
默認(rèn)值:srcache_max_expire 0
配置段:http, server, location, location if
控制最大緩存時(shí)間,此設(shè)置優(yōu)先級(jí)高于其他計(jì)算方法。
該值必須小于597hours。
默認(rèn)為0,不限制。
- spring結(jié)合redis如何實(shí)現(xiàn)數(shù)據(jù)的緩存
- Redis整合Spring結(jié)合使用緩存實(shí)例
- php操作redis緩存方法分享
- 圖文詳解Windows下使用Redis緩存工具的方法
- Python的Flask框架使用Redis做數(shù)據(jù)緩存的配置方法
- PHP使用redis實(shí)現(xiàn)統(tǒng)計(jì)緩存mysql壓力的方法
- Spring Boot 基于注解的 Redis 緩存使用詳解
- CI框架中redis緩存相關(guān)操作文件示例代碼
- Redis緩存詳解
- redis緩存的簡(jiǎn)單操作(get、put)
相關(guān)文章
Nginx學(xué)習(xí)之如何搭建文件防盜鏈服務(wù)的方法示例
這篇文章主要介紹了Nginx學(xué)習(xí)之如何搭建文件防盜鏈服務(wù)的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
排查Openresty獲取不到Host請(qǐng)求頭解決過(guò)程詳解
這篇文章主要為大家介紹了排查Openresty獲取不到Host請(qǐng)求頭解決過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
通過(guò)Nginx搭建Tomcat9集群并實(shí)現(xiàn)Session共享
這篇文章主要介紹了通過(guò)Nginx搭建Tomcat9集群并實(shí)現(xiàn)Session共享,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
在Nginx中實(shí)現(xiàn)URL重寫(xiě)與重定向
Nginx 的 **URL 重寫(xiě)(Rewrite)**功能允許你根據(jù)請(qǐng)求的 URL 動(dòng)態(tài)地修改請(qǐng)求路徑或進(jìn)行重定向,本文將詳細(xì)介紹如何在 Nginx 中實(shí)現(xiàn) URL 重寫(xiě),并給出一些常見(jiàn)的實(shí)際場(chǎng)景,需要的朋友可以參考下2025-01-01
nginx提示:500 Internal Server Error錯(cuò)誤的解決方法
本文章來(lái)給大家總結(jié)了大量關(guān)于導(dǎo)致nginx中提示500 Internal Server Error錯(cuò)誤的原因總結(jié)與解決方法分析有需要了解的朋友可參考參考2013-04-04
Nginx配置實(shí)現(xiàn)用IP灰度測(cè)試(不同用戶ID)
本文主要介紹了使用Nginx配置實(shí)現(xiàn)基于IP的灰度發(fā)布實(shí)驗(yàn),以及如何通過(guò)不同用戶ID測(cè)試灰度發(fā)布,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
基于nginx設(shè)置瀏覽器協(xié)商緩存過(guò)程詳解
這篇文章主要介紹了基于nginx設(shè)置瀏覽器協(xié)商緩存過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Nginx實(shí)現(xiàn)高并發(fā)的項(xiàng)目實(shí)踐
本文主要介紹了Nginx實(shí)現(xiàn)高并發(fā)的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03

