在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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当做缓存使用,因此没有启用持久化。 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.ogeek.net.conf; } # vim vhosts/test.ogeek.net.conf upstream redis { server 127.0.0.1:6379; keepalive 512; } server { listen 80; server_name test.ogeek.net; index index.html index.htm index.php; root /data/test.ogeek.net/webroot; location ~ .*\.php { srcache_store_private on; srcache_methods GET; srcache_response_cache_control off; if ($uri ~ /ogeek.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.ogeek.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.ogeek.net-error.log; access_log /data/httplogs/test.ogeek.net-aceess.log main; } 4. 测试 有做缓存状态: 5. 响应头状态 再次请求: 6. 查看redis是否缓存以及过期时间 PS:srcache-nginx-module模块指令说明:
可以使用srcache_store_pass_header、srcache_store_hide_header指令来控制哪些头要缓存哪些不要。 注意:即使所有的响应数据被立即发送,当前的nginx请求生命周期未必完成,直到srcache_store子请求完成。这意味着服务器端延迟关闭TCP连接,或下一个请求服务发送同一个TCP连接。
可以隐藏多个响应头,不区分大小写。如 srcache_store_hide_header X-Foo; srcache_store_hide_header Last-Modified;srcache_store_pass_header 语法:srcache_store_pass_header <header>
可以缓存多个响应头,不区分大小写。如 srcache_store_pass_header Set-Cookie; srcache_store_pass_header Proxy-Autenticate; srcache_methods proxy_set_header Accept-Encoding ""; srcache_request_cache_control 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高。 |
请发表评论