小知识:Nginx配置srcache_nginx模块搭配Redis建立缓存系统

1. nginx模块

?
1
2
3
4
5
addmodule=../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安装配置

?
1
# vim redis.conf
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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当做缓存使用,因此没有启用持久化。

3. nginx配置

?
1
# vim nginx.conf
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.tuohang.net.conf;
}
?
1
# vim vhosts/test.tuohang.net.conf
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
upstream redis {
server 127.0.0.1:6379;
keepalive 512;
}
server
{
listen    80;
server_name test.tuohang.net;
index index.html index.htm index.php;
root /data/test.tuohang.net/webroot;
location ~ .*\.php {
srcache_store_private on;
srcache_methods GET;
srcache_response_cache_control off;
if ($uri ~ /tuohang.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.tuohang.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.tuohang.net-error.log;
access_log /data/httplogs/test.tuohang.net-aceess.log main;
}

4. 测试 没有做缓存状态:

%小知识:Nginx配置srcache_nginx模块搭配Redis建立缓存系统-猿站网-插图

有做缓存状态:

%小知识:Nginx配置srcache_nginx模块搭配Redis建立缓存系统-1猿站网-插图

5. 响应头状态 第一次请求:

%小知识:Nginx配置srcache_nginx模块搭配Redis建立缓存系统-2猿站网-插图

再次请求:

%小知识:Nginx配置srcache_nginx模块搭配Redis建立缓存系统-3猿站网-插图

6. 查看redis是否缓存以及过期时间

%小知识:Nginx配置srcache_nginx模块搭配Redis建立缓存系统-4猿站网-插图

PS:srcache-nginx-module模块指令说明:

srcache_fetch

语法:srcache_fetch ?

默认值:no

配置段:http, server, location, location if

查询缓存。返回200说明缓存命中,直接从缓存响应客户端请求。非200需要后端程序处理。

srcache_fetch_skip

语法:srcache_fetch_skip

默认值:srcache_fetch_skip 0

配置段:http, server, location, location if

支持nginx变量。当这个参数值不为空和不等于0,则从缓存取数据过程被无条件跳过。

srcache_store

语法:srcache_store ?

默认值:no

配置段:http, server, location, location if

将当前请求的响应存入缓存。可以使用srcache_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

默认值:srcache_store_max_size 0

配置段:http, server, location, location if

当响应体超过该值,将不会缓存。

当后端缓存存储有对缓存数据做硬限制,这个指令非常有用。比如memcached服务器,上限是1M。

默认值0,不限制。

srcache_store_skip

语法:srcache_store_skip

默认值:srcache_store_skip 0

配置段:http, server, location, location if

支持nginx变量。当这个参数值不为空和不等于0,则从存入缓存过程被无条件跳过。

srcache_store_statuses

语法:srcache_store_statuses ..

默认值:srcache_store_statuses 200 301 302

配置段:http, server, location, location if

该指令控制那些状态码响应被缓存。

srcache_header_buffer_size

语法:srcache_header_buffer_size

默认值:srcache_header_buffer_size 4k/8k

配置段:http, server, location, location if

在序列化响应头时控制头缓冲大小。默认大小为页面大小,通常为4k或8k,取决于具体平台。

注意:该大小是以每个头的,因此,需要足够大来容纳最大响应头。

srcache_store_hide_header

语法:srcache_store_hide_header

默认值:no

配置段:http, server, location, location if

默认情况下,除了以下头缓存所有响应头: Connection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailers Transfer-Encoding Upgrade Set-Cookie

可以隐藏多个响应头,不区分大小写。如

?
1
2
srcache_store_hide_header X-Foo;
srcache_store_hide_header Last-Modified;
srcache_store_pass_header

语法:srcache_store_pass_header

默认值:no

配置段:http, server, location, location if

默认情况下,除了以下头缓存所有响应头: Connection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailers Transfer-Encoding Upgrade Set-Cookie

可以缓存多个响应头,不区分大小写。如

?
1
2
srcache_store_pass_header Set-Cookie;
srcache_store_pass_header Proxy-Autenticate;

srcache_methods

语法:srcache_methods

默认值: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配置:
?
1
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按照下面的方法处理:
?
1
2
3
4
5
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

默认值:srcache_default_expire 60s

配置段:http, server, location, location if

控制默认过期时间。当响应头既没有Cache-Control: max-age=N也没有指定Expires时,允许的$srcache_expire变量值。

该值必须小于597hours。

srcache_max_expire

语法:srcache_max_expire

默认值:srcache_max_expire 0

配置段:http, server, location, location if

控制最大缓存时间,此设置优先级高于其他计算方法。

该值必须小于597hours。

默认为0,不限制。
声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:双线双IP和BGP双线有什么区别?

2023-5-1 23:56:32

建站知识

小知识:通过Nginx服务器获取大文件MD5值的配置方法

2023-5-2 1:04:29

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索