srcache_nginx+redis构建缓存系统

默北 Nginx1437,5622字数 3833阅读12分46秒阅读模式

在《memc_nginx+srcache_nginx+memcached构建透明的动态页面缓存》一文中,我们使用到memcached来作为缓存载体。想必大家都知道memcached有存储大小的限制,不得超过1M。 本文将使用redis来作为缓存载体。nginx的srcache_nginx模块指令参数解释参见《memc_nginx+srcache_nginx+memcached构建透明的动态页面缓存》。

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

nginx模块安装参见ttlsa.com中相关文档。文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

2. redis安装配置

安装步骤参见:https://www.ttlsa.com/html/1646.html
配置参数解释参见:https://www.ttlsa.com/html/1226.html
配置实例:
# vim redis.conf文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

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当做缓存使用,因此没有启用持久化。文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

3. nginx配置

# vim nginx.conf文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

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.ttlsa.com.conf;
}

# vim vhosts/test.ttlsa.com.conf文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

upstream redis {
        server 127.0.0.1:6379;
        keepalive 512;
}

server
       {
        listen       80;
        server_name  test.ttlsa.com;
        index index.html index.htm index.php;
        root  /data/test.ttlsa.com/webroot;

        location ~ .*\.php {
                srcache_store_private on;
                srcache_methods GET;
                srcache_response_cache_control off;

                if ($uri ~ /ttlsa.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.ttlsa.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.ttlsa.com-error.log;
        access_log  /data/httplogs/test.ttlsa.com-aceess.log main;

}

4. 测试

没有做缓存状态:
memc-nginx
有做缓存状态:
memc-nginx文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

5. 响应头状态

第一次请求:
memc-nginx
再次请求:
memc-nginx文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

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

memc-nginx文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

如需转载请注明出处: https://www.ttlsa.com/html/3156.html文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/ 文章源自运维生存时间-https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 24/09/2013 15:19:20
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/
评论  14  访客  10
    • Aceslup
      Aceslup 4

      赞。

      • jerry
        jerry 3

        您好,博主,很喜欢你的文章,但这里有一个地方看不明白,/redis2 到底是什么意思?和/redis有什么区别呢

        • […] redis 清除缓存 26.?nginx动态IP黑白名单构建web防火墙(ngx_white_black_list) 27.?srcache_nginx+redis构建缓存系统 28.?nginx模块nginx-http-footer-filter研究使用 29.?nginx本地缓存模块ngx_slowfs_cache […]

          • 恩和
            恩和 0

            为什么用了redis2模块,还要用redis老版本的。。。我发现用/redis中用redis2_pass 会不起作用,能否解惑。。不胜感激。

              • nian
                nian 0

                @ 恩和 redis-nginx-module 也找不到了,你解决了吗?

              • 陌城落年
                陌城落年 9

                nginx: [emerg] unknown "echo_request_body" variable
                这个难道要装echo模块?

                • 朱瑞卿
                  朱瑞卿 0

                  能否缓存后台为IIS 动态文件问aspx的数据

                  • 小万哥
                    小万哥 9

                    pp.php是做什么的

                      • 默北
                        默北

                        @ 小万哥 这个是业务程序,通过这个php文件调用存储系统的图片。

                    评论已关闭!