国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

Nginx配置srcache_nginx模塊搭配Redis建立緩存系統

2024-08-30 12:27:59
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Nginx配置srcache_nginx模塊搭配Redis建立緩存系統的方法,文中關于Nginx模塊和Redis數據庫的安裝就不再說明了,這里只關注配置搭建階段,需要的朋友可以參考下
 

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 yespidfile /var/run/redis-6379.pidport 6379bind 127.0.0.1timeout 0tcp-keepalive 0loglevel noticelogfile stdoutdatabases 16stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbslave-serve-stale-data yesslave-read-only yesrepl-disable-tcp-nodelay noslave-priority 100maxmemory 8096mbmaxmemory-policy volatile-ttlappendonly noappendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes

由于只把redis當做緩存使用,因此沒有啟用持久化。

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.vevb.com.conf;}
# vim vhosts/test.vevb.com.conf
upstream redis {    server 127.0.0.1:6379;    keepalive 512;} server    {    listen    80;    server_name test.vevb.com;    index index.html index.htm index.php;    root /data/test.vevb.com/webroot;     location ~ .*/.php {        srcache_store_private on;        srcache_methods GET;        srcache_response_cache_control off;         if ($uri ~ /vevb.com/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.vevb.com-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.vevb.com-error.log;    access_log /data/httplogs/test.vevb.com-aceess.log main; }

4. 測試
沒有做緩存狀態:

Nginx配置srcache_nginx模塊搭配Redis建立緩存系統

有做緩存狀態:

Nginx配置srcache_nginx模塊搭配Redis建立緩存系統

5. 響應頭狀態
第一次請求:

Nginx配置srcache_nginx模塊搭配Redis建立緩存系統

再次請求:

Nginx配置srcache_nginx模塊搭配Redis建立緩存系統

6. 查看redis是否緩存以及過期時間

Nginx配置srcache_nginx模塊搭配Redis建立緩存系統

 

PS:srcache-nginx-module模塊指令說明:
srcache_fetch

語法:srcache_fetch <method> <uri> <args>?
默認值:no
配置段:http, server, location, location if
查詢緩存。返回200說明緩存命中,直接從緩存響應客戶端請求。非200需要后端程序處理。
srcache_fetch_skip
語法:srcache_fetch_skip <flag>
默認值:srcache_fetch_skip 0
配置段:http, server, location, location if
<flag>支持nginx變量。當這個參數值不為空和不等于0,則從緩存取數據過程被無條件跳過。
srcache_store
語法:srcache_store <method> <uri> <args>?
默認值:no
配置段:http, server, location, location if
將當前請求的響應存入緩存??梢允褂胹rcache_store_skip和srcache_store_max_size指令禁用緩存。不管是響應狀態行,響應頭,響應體都會被緩存。默認情況下,下列特殊響應頭不會被緩存:

  • Connection
  • Keep-Alive
  • Proxy-Authenticate
  • Proxy-Authorization
  • TE
  • Trailers
  • Transfer-Encoding
  • Upgrade
  • Set-Cookie

可以使用srcache_store_pass_header、srcache_store_hide_header指令來控制哪些頭要緩存哪些不要。

注意:即使所有的響應數據被立即發送,當前的nginx請求生命周期未必完成,直到srcache_store子請求完成。這意味著服務器端延遲關閉TCP連接,或下一個請求服務發送同一個TCP連接。
srcache_store_max_size
語法:srcache_store_max_size <size>
默認值:srcache_store_max_size 0
配置段:http, server, location, location if
當響應體超過該值,將不會緩存。
當后端緩存存儲有對緩存數據做硬限制,這個指令非常有用。比如memcached服務器,上限是1M。
默認值0,不限制。
srcache_store_skip
語法:srcache_store_skip <flag>
默認值:srcache_store_skip 0
配置段:http, server, location, location if
<flag>支持nginx變量。當這個參數值不為空和不等于0,則從存入緩存過程被無條件跳過。
srcache_store_statuses
語法:srcache_store_statuses <status1> <status2> ..
默認值:srcache_store_statuses 200 301 302
配置段:http, server, location, location if
該指令控制那些狀態碼響應被緩存。
srcache_header_buffer_size
語法:srcache_header_buffer_size <size>
默認值:srcache_header_buffer_size 4k/8k
配置段:http, server, location, location if
在序列化響應頭時控制頭緩沖大小。默認大小為頁面大小,通常為4k或8k,取決于具體平臺。
注意:該大小是以每個頭的,因此,需要足夠大來容納最大響應頭。
srcache_store_hide_header
語法:srcache_store_hide_header <header>
默認值:no
配置段:http, server, location, location if
默認情況下,除了以下頭緩存所有響應頭:

  • Connection
  • Keep-Alive
  • Proxy-Authenticate
  • Proxy-Authorization
  • TE
  • Trailers
  • Transfer-Encoding
  • Upgrade
  • Set-Cookie

可以隱藏多個響應頭,不區分大小寫。如

srcache_store_hide_header X-Foo;srcache_store_hide_header Last-Modified;
srcache_store_pass_header

語法:srcache_store_pass_header <header>
默認值:no
配置段:http, server, location, location if
默認情況下,除了以下頭緩存所有響應頭:

  • Connection
  • Keep-Alive
  • Proxy-Authenticate
  • Proxy-Authorization
  • TE
  • Trailers
  • Transfer-Encoding
  • Upgrade
  • Set-Cookie

可以緩存多個響應頭,不區分大小寫。如

srcache_store_pass_header Set-Cookie;srcache_store_pass_header Proxy-Autenticate;

srcache_methods
語法:srcache_methods <method>...
默認值:srcache_methods GET HEAD
配置段:http, server, location
srcache_ignore_content_encoding
語法:srcache_ignore_content_encoding on|off
默認值: srcache_ignore_content_encoding off
配置段:http, server, location, location if
內容是否編碼。
建議后端服務器禁用gzip/deflate壓縮。在nginx.conf配置:

proxy_set_header Accept-Encoding "";

srcache_request_cache_control
語法:srcache_request_cache_control on|off
默認值:srcache_request_cache_control off
配置段:http, server, location
當該指令為on時,請求頭Cache-Control和Pragma按照下面的方法處理:
1. srcache_fetch查詢緩存操作時,當請求頭Cache-Control: no-cache 、 Pragma: no-cache 將跳過。
2. srcache_store存入緩存操作時,當請求頭Cache-Control: no-store將跳過。
當該指令為off時,將禁用此功能,對于繁忙的站點依賴緩存加速被認為是最安全的。
srcache_response_cache_control
語法:srcache_response_cache_control on|off
默認值:srcache_response_cache_control on
配置段:http, server, location
當該指令為on時,響應頭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.

該指令優先級比srcache_store_no_store,srcache_store_no_cache,srcache_store_private高。
srcache_store_no_store
語法:srcache_store_no_store on|off
默認值:srcache_store_no_store off
配置段:http, server, location
開啟該指令,將強制響應頭Cache-Control: no-store。默認為關閉。
srcache_store_no_cache
語法:srcache_store_no_cache on|off
默認值:srcache_store_no_cache off
配置段:http, server, location
開啟該指令,將強制響應頭Cache-Control: no-cache。默認為關閉。
srcache_store_private
語法:srcache_store_private on|off
默認值:srcache_store_private off
配置段:http, server, location
開啟該指令,將強制響應頭Cache-Control: private。默認為關閉。
srcache_default_expire
語法:srcache_default_expire <time>
默認值:srcache_default_expire 60s
配置段:http, server, location, location if
控制默認過期時間。當響應頭既沒有Cache-Control: max-age=N也沒有指定Expires時,允許的$srcache_expire變量值。
該值必須小于597hours。
srcache_max_expire
語法:srcache_max_expire <time>
默認值:srcache_max_expire 0
配置段:http, server, location, location if
控制最大緩存時間,此設置優先級高于其他計算方法。
該值必須小于597hours。
默認為0,不限制。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴彦县| 绍兴市| 云浮市| 若羌县| 巩留县| 江油市| 平谷区| 富平县| 高平市| 林周县| 平泉县| 阜南县| 西峡县| 炎陵县| 岳阳市| 鱼台县| 晋宁县| 衡山县| 娄烦县| 汉阴县| 巫溪县| 容城县| 信丰县| 苍溪县| 韩城市| 东台市| 得荣县| 宁武县| 河南省| 那坡县| 景洪市| 察隅县| 尼玛县| 吉隆县| 陕西省| 宁安市| 浠水县| 图木舒克市| 博湖县| 西青区| 横峰县|