Nginx Configure the Srcache_nginx module with Redis to establish a caching system _nginx

Source: Internet
Author: User
Tags hash redis

1. Nginx Module

--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 Installation Configuration

# Vim Redis.conf
 daemonize Yes Pidfile/var/run/redis-6379.pid port 6379 bind 127.0.0.1 Timeout 0 tcp-ke Epalive 0 loglevel Notice logfile stdout databases Yes Stop-writes-on-bgsave-error Yes Rdbcompression Yes D
Bfilename 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 auto-aof-rewrite-min-size 64MB Lua-time-limit 5000 Slowlog-log-slower-than 10000
Slowlog-max-len 128 hash-max-ziplist-entries Hash-max-ziplist-value list-max-ziplist-entries 512
List-max-ziplist-value set-max-intset-entries 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 client-output-b Uffer-limit pubsub 32mb 8mb Hz aof-rewrite-incremental-fsync Yes 

Because Redis is used only as a cache, persistence is not enabled.

3. Nginx Configuration

# 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 $MD 5key $key;
            #add_header X-md5-key $MD 5key;
            #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. Test
No cache status done:

Have to do the cache state:

5. Response Head Status
First time Request:

Request again:

6. See if Redis cache and expiration time

Ps:srcache-nginx-module Module Instruction Description:
Srcache_fetch
Grammar: Srcache_fetch <method> <uri> <args>?
Default value: No
Configuration segment: HTTP, server, location, location if
Query caching. Returns 200 indicating cache hits, which respond directly to client requests from the cache. Non-200 requires back-end program processing.
Srcache_fetch_skip
Syntax: Srcache_fetch_skip <flag>
Default value: Srcache_fetch_skip 0
Configuration segment: HTTP, server, location, location if
<flag> supports nginx variables. When the value of this parameter is not null and is not equal to 0, the process of fetching data from the cache is skipped unconditionally.
Srcache_store
Grammar: Srcache_store <method> <uri> <args>?
Default value: No
Configuration segment: HTTP, server, location, location if
Stores the response of the current request to the cache. You can disable caching using the Srcache_store_skip and srcache_store_max_size directives. The response body is cached whether it is a response-state row or a response header. By default, the following special response headers are not cached:

    • Connection
    • Keep-alive
    • Proxy-authenticate
    • Proxy-authorization
    • TE
    • Trailers
    • Transfer-encoding
    • Upgrade
    • Set-cookie

You can use Srcache_store_pass_header, srcache_store_hide_header directives to control which headers to cache and which to not.

Note: Even if all response data is sent immediately, the current Nginx request lifecycle may not complete until the Srcache_store request completes. This means that the server side delays shutting down the TCP connection, or the next request service sends the same TCP connection.
srcache_store_max_size
Syntax: Srcache_store_max_size <size>
Default value: Srcache_store_max_size 0
Configuration segment: HTTP, server, location, location if
When the response body exceeds this value, it is not cached.
This instruction is useful when back-end cache storage has a hard limit on cached data. For example memcached server, the upper limit is 1M.
The default value is 0, not restricted.
Srcache_store_skip
Syntax: Srcache_store_skip <flag>
Default value: Srcache_store_skip 0
Configuration segment: HTTP, server, location, location if
<flag> supports nginx variables. When the value of this parameter is not null and is not equal to 0, it is skipped unconditionally from the cache process.
Srcache_store_statuses
Syntax: srcache_store_statuses <status1> <status2>.
Default value: Srcache_store_statuses 200 301 302
Configuration segment: HTTP, server, location, location if
The directive controls those status code responses being cached.
srcache_header_buffer_size
Syntax: Srcache_header_buffer_size <size>
Default value: Srcache_header_buffer_size 4k/8k
Configuration segment: HTTP, server, location, location if
Controls the size of the head buffer when serializing the response header. The default size is page size, usually 4k or 8k, depending on the platform.
Note: The size is in each header, so it needs to be large enough to accommodate the maximum response head.
Srcache_store_hide_header
Syntax: Srcache_store_hide_header Default value: No
Configuration segment: HTTP, server, location, location if
By default, all response headers are cached except for the following headers:

    • Connection
    • Keep-alive
    • Proxy-authenticate
    • Proxy-authorization
    • TE
    • Trailers
    • Transfer-encoding
    • Upgrade
    • Set-cookie

Multiple response headers can be hidden, case-insensitive. Such as

Srcache_store_hide_header X-foo;
Srcache_store_hide_header last-modified;
Srcache_store_pass_header

Syntax: Srcache_store_pass_header Default value: No
Configuration segment: HTTP, server, location, location if
By default, all response headers are cached except for the following headers:

    • Connection
    • Keep-alive
    • Proxy-authenticate
    • Proxy-authorization
    • TE
    • Trailers
    • Transfer-encoding
    • Upgrade
    • Set-cookie

Multiple response headers can be cached, case-insensitive. Such as

Srcache_store_pass_header Set-cookie;
Srcache_store_pass_header proxy-autenticate;

Srcache_methods
Grammar: Srcache_methods <method>
Default value: Srcache_methods get head
Configuration segment: HTTP, server, location
Srcache_ignore_content_encoding
Syntax: srcache_ignore_content_encoding on|off
Default value: Srcache_ignore_content_encoding off
Configuration segment: HTTP, server, location, location if
Whether the content is encoded.
It is recommended that the back-end server disable Gzip/deflate compression. In nginx.conf configuration:

Proxy_set_header accept-encoding "";

Srcache_request_cache_control
Syntax: Srcache_request_cache_control on|off
Default value: Srcache_request_cache_control off
Configuration segment: HTTP, server, location
When the directive is on, the request headers Cache-control and pragma are handled as follows:
1. Srcache_fetch query cache operation, when the request header Cache-control:no-cache, Pragma:no-cache will skip.
2. When the srcache_store is stored in the cache operation, the request header Cache-control:no-store will be skipped.
When the directive is off, this feature is disabled and is considered the safest for busy sites to rely on cache acceleration.
Srcache_response_cache_control
Syntax: Srcache_response_cache_control on|off
Default value: Srcache_response_cache_control on
Configuration segment: HTTP, server, location
When the directive is on, the response headers Cache-control and expires are treated in the following way:

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.

This instruction has higher precedence than srcache_store_no_store,srcache_store_no_cache,srcache_store_private.
Srcache_store_no_store
Syntax: Srcache_store_no_store on|off
Default value: Srcache_store_no_store off
Configuration segment: HTTP, server, location
Turn on the command to force the response header Cache-control:no-store. The default is off.
Srcache_store_no_cache
Syntax: Srcache_store_no_cache on|off
Default value: Srcache_store_no_cache off
Configuration segment: HTTP, server, location
Turn on the command to force the response header Cache-control:no-cache. The default is off.
srcache_store_private
Syntax: Srcache_store_private on|off
Default value: Srcache_store_private off
Configuration segment: HTTP, server, location
Turn on the command to force the response header cache-control:private. The default is off.
Srcache_default_expire
Syntax: Srcache_default_expire <time>
Default value: Srcache_default_expire 60s
Configuration segment: HTTP, server, location, location if
Controls the default expiration time. The $srcache_expire variable value that is allowed when the response header has neither CACHE-CONTROL:MAX-AGE=N nor specified expires.
The value must be less than 597hours.
Srcache_max_expire
Syntax: Srcache_max_expire <time>
Default value: Srcache_max_expire 0
Configuration segment: HTTP, server, location, location if
Control the maximum cache time, this setting takes precedence over other calculation methods.
The value must be less than 597hours.
The default is 0, not limited.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.