memc_nginx + srcache_nginx + memcached build transparent dynamic page caching

On a " nginx + memcached caching application to build the page " http://www.ttlsa.com/html/2418.html in and said, nginx is only responsible for reading data from memcached server, the data have to be written to memcached We need to complete the back-end application. Use memc-nginx and srcache-nginx module can proactively add cache to memcached, the application is transparent. Greatly improved dynamic page access speed. First visit to create a cache, follow-up visits in the cache expiration, return directly from memcached, does not require php-fpm process again. nginx_memc module nginx_srcache modules which are used to provide back-end operation of the cache server, technically, any module may provide an interface with the REST nginx_srcache cooperate to acquire and store cached child requests. Use memcached as caching backend, we need to pay attention to memcached storage size limit shall not exceed 1M. In order to use a more relaxed back-end storage services, such as recommended redis, see " srcache_nginx + redis build the cache system . " 1. memc-nginx-module module Instructions: memc_pass Syntax: memc_pass address: port or socket; default: none configuration section: http, server, location, if the specified address server memcached. memc_cmds_allowed syntax: memc_cmds_allowed <cmd> ... Default: none configuration section: http, server, location, if allowed to access the list of memcached command. By default, all the memcached commands can be accessed. memc_flags_to_last_modified syntax: memc_flags_to_last_modified on | off Default value: off configuration section: http, server, location, if read memcached logo, and set it to Last-Modified header value. For conditional get, nginx returns 304 Not Modified response, in order to save bandwidth. memc_connect_timeout syntax: memc_connect_timeout <time> default value: 60s configuration section: http, server, location and memcached server to establish a connection timeout. Shall not exceed 597 hours. memc_send_timeout Syntax: memc_send_timeout <time> Default: 60s configuration section: http, server, location is provided to the transmission request time memcached server. Shall not exceed 597 hours. memc_read_timeout Syntax: memc_read_timeout <time> Default: 60s configuration section: http, server, location defined memcached read response from the server timeout. Shall not exceed 597 hours. memc_buffer_size Syntax: memc_buffer_size <size> Default: 4k / 8k configuration section: http, server, location server memcached read response received from the buffer size. 2. memcached memcached commands supported storage command set, add, replace, prepend, append, to $ memc_key as the key. $ Memc_exptime defined expiration time, the default value is 0. $ Memc_flags as a logo, the default value is 0, to establish the appropriate memcached queries. $ Memc_value If not defined, then the request body as the value of the request, in addition to incr and decr command. Note: If the value is $ memc_value defined as an empty string, then the empty string is still as. 2.1 get $ memc_key use keys to retrieve the value.
location /foo {
set $memc_cmd 'get';
set $memc_key 'my_key';
memc_pass 127.0.0.1:11211;
add_header X-Memc-Flags $memc_flags;
}
If the key is found, the key for the response body, returns 200. Otherwise range 404 Not Found. If an error or a client or a server error occurred error is returned 502. Code set to $ memc_flags variable. It is typically used to add to the response header add_header instruction. 2.2 set $ memc_key $ memc_flags $ memc_exptime $ memc_value memcached value as the request body. If another specified value can be specified by $ memc_value variable.
location /foo {
    set $memc_cmd 'set';
    set $memc_key 'my_key';
    set $memc_flags 12345;
    set $memc_exptime 24;
    memc_pass 127.0.0.1:11211;
}
or
location /foo {
    set $memc_cmd 'set';
    set $memc_key 'my_key';
    set $memc_flags 12345;
    set $memc_exptime 24;
    set $memc_value 'my_value';
    memc_pass 127.0.0.1:11211;
}
Return to 201, the instructions to create memcached cache storage success. 200 return instructions NOT_STORED. Back 404 described NOT_FOUND. 502 description of the error or return error or client server error occurred. except the raw response being memcached body, 404 NOT FOUND. 2.3 add similar 2.4 prepend $ memc_key $ memc_flags $ memc_exptime $ memc_value and set commands like $ memc_key $ memc_flags $ memc_exptime $ memc_value and set commands 2.5 delete $ memc_key remove the key
location /foo
set $memc_cmd delete;
set $memc_key my_key;

memc_pass 127.0.0.1:11211;
}
Returned 200 Description deleted successfully. Back 404 described NOT_FOUND. 502 description of the error or return error or client server error occurred. 2.6 delete $ memc_key $ memc_exptime and delete commands similar. 2.7 incr $ memc_key $ memc_value to the specified $ memc_key corresponding $ memc_value increments.
location /foo {
set $memc_key my_key;
set $memc_value 2;
memc_pass 127.0.0.1:11211;
}
Every visit / foo will cause the value my_key plus 2. Returned 200 shows a success. Description Returns the key 404 Not Found. 502 description of the error or return error or client server error occurred. 2.8 Similarly decr $ memc_key $ memc_value and incr. 2.9 flush_all refresh all the keys on memcached.
location /foo {
set $memc_cmd flush_all;
memc_pass 127.0.0.1:11211;
}
2.10 flush_all $ memc_exptime and flush_all similar. 2.11 stats output memcached statistics.
location /foo {
set $memc_cmd stats;
memc_pass 127.0.0.1:11211;
}
2.12 version return memcached version information.
location /foo {
set $memc_cmd version;
memc_pass 127.0.0.1:11211;
}
3. srcache-nginx-module module Instructions: srcache_fetch syntax: srcache_fetch <method> <uri> <args> Default value:? No configuration section: http, server, location, location if the query cache. 200 illustrates return cache hit, the cache in response to a request from a client directly. Non-200 need the back-end processing program. srcache_fetch_skip syntax: srcache_fetch_skip <flag> Default Value: srcache_fetch_skip 0 configuration section: http, server, location, location if <flag> Support nginx variables. When the value of this parameter is not empty and not equal to 0, the data from the cache fetch process is skipped unconditionally. srcache_store syntax:? srcache_store <method> <uri> <args> default: no configuration section: http, server, location, in response to the current location if the request for cache. You can use srcache_store_skip and srcache_store_max_size instructions to disable the cache. Whether a response status line, in response to the first response member are cached. By default, the following special response header will not be cached: Connection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailers Transfer-Encoding Upgrade Set-Cookie can use srcache_store_pass_header, srcache_store_hide_header instructions to control what the head which do not want to cache. Note: Even if all the data is transmitted immediately in response to the current request lifecycle nginx not complete until complete srcache_store sub-request. This means that the server closes the TCP connection delay, the next request or service sends same TCP connection. srcache_store_max_size Syntax: srcache_store_max_size <size> Default: srcache_store_max_size 0 configuration section: http, server, location, location if this value is exceeded when the response body, will not be cached. When the back-end cache storage have to do a hard limit on the data cache, this instruction is useful. Such as memcached server, the upper limit is 1M. The default value is 0, no restrictions. srcache_store_skip syntax: srcache_store_skip <flag> Default Value: srcache_store_skip 0 configuration section: http, server, location, location if <flag> Support nginx variables. When the value of this parameter is not empty and not equal to 0, the process is skipped unconditionally stored in the buffer. srcache_store_statuses Syntax: srcache_store_statuses <status1> <status2> .. Default: srcache_store_statuses 200 301 302 configuration section: http, server, location, location if the status code in response to those control instruction is cached. srcache_header_buffer_size Syntax: srcache_header_buffer_size <size> Default: srcache_header_buffer_size 4k / 8k configuration section: http, server, location, location if the control head buffer size when serializing response header. The default size is the page size, usually 4k or 8k, depending on the specific platform. Note: The size of each head is, therefore, need to be large enough to accommodate the maximum response header. srcache_store_hide_header Syntax: srcache_store_hide_header <header> default: no configuration section: http, server, location, location if default, in addition to the first cache all response headers: Connection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailers Transfer-Encoding Upgrade Set-Cookie response header may hide more insensitive. The srcache_store_hide_header X-Foo; srcache_store_hide_header Last-Modified; srcache_store_pass_header Syntax: srcache_store_pass_header <header> default: no configuration section: http, server, location, The default location if, in addition to all of the response headers cached header: Connection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailers Transfer-Encoding Upgrade Set-Cookie may buffer a plurality of heads in response to a case-insensitive. As srcache_store_pass_header Set-Cookie; srcache_store_pass_header Proxy-Autenticate; srcache_methods syntax: srcache_methods <method> ... Default value: srcache_methods GET HEAD configuration section: http, server, location srcache_ignore_content_encoding syntax: srcache_ignore_content_encoding on | off Default value: srcache_ignore_content_encoding off configuration section: http, server, location, location if the content is encoded. Recommend back-end server to disable gzip / deflate compression. In nginx.conf configuration: location srcache_ignore_content_encoding syntax: srcache_ignore_content_encoding on | off Default value: srcache_ignore_content_encoding off configuration section: http, server, location, location if the content is encoded. Recommend back-end server to disable gzip / deflate compression. In nginx.conf configuration: location srcache_ignore_content_encoding syntax: srcache_ignore_content_encoding on | off Default value: srcache_ignore_content_encoding off configuration section: http, server, location, location if the content is encoded. Recommend back-end server to 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: srcache_request_cache_control off configuration section: http, server, location when the instruction is on, request header Cache-Control and Pragma processed in the following manner: 1. srcache_fetch when the query cache operation, when the request header Cache-Control: no-cache, Pragma: no-cache will be skipped. 2. srcache_store operation when cached, when the request header Cache-Control: no-store will be skipped. When this command is off, it will disable this feature for busy sites rely cache acceleration is considered to be the safest. srcache_response_cache_control Syntax: srcache_response_cache_control on | off Default: srcache_response_cache_control on configuration section: http, server, location when the instruction is on, in response Expires header Cache-Control and treated according to the following method: 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. The priority instruction, srcache_store_no_cache, higher than srcache_store_no_store srcache_store_private. srcache_store_no_store syntax: srcache_store_no_store on | off Default value: srcache_store_no_store off configuration section: http, server, location opened the directive will force response header Cache-Control: no-store. It is off by default. srcache_store_no_cache syntax: srcache_store_no_cache on | off Default value: srcache_store_no_cache off configuration section: http, server, location opened the directive will force response header Cache-Control: no-cache. It is off by default. srcache_store_private syntax: srcache_store_private on | off Default value: srcache_store_private off configuration section: http, server, location opened the directive will force response header Cache-Control: private. It is off by default. srcache_default_expire syntax: srcache_default_expire <time> Default value: srcache_default_expire 60s configuration section: http, server, location, location if control default expiration time. When the response header neither Cache-Control: max-age = N when not specified Expires, allowed $ srcache_expire variable value. This value must be less than 597hours. srcache_max_expire Syntax: srcache_max_expire <time> Default: srcache_max_expire 0 configuration section: http, server, location, location if the maximum cache time control, setting this higher priority than other calculation methods. This value must be less than 597hours. The default is 0, no restrictions. 4. srcache-nginx-module variable $ srcache_expire current response expiration time stored in the cache. Calculated according to the following method: 1. When the response header Cache-Control: max-age = N is specified, then N as expiration time. 2. If the response Expires header is specified, then the difference between the value and the current time as the expiration time. 3. When neither the Cache-Control: max-age = N not specified Expires, then use the value srcache_default_expire specified. If the value exceeds srcache_max_expire command, then the final value of this variable srcache_max_expire. $ Srcache_fetch_status get the cached value of three states: HIT, MISS, BYPASS. $ Srcache_store_status cached value of two states: STORE, BYPASS. 5. Installation nginx_memc module and nginx_srcache max-age = N not specified Expires, then use the value srcache_default_expire specified. If the value exceeds srcache_max_expire command, then the final value of this variable srcache_max_expire. $ Srcache_fetch_status get the cached value of three states: HIT, MISS, BYPASS. $ Srcache_store_status cached value of two states: STORE, BYPASS. 5. Installation nginx_memc module and nginx_srcache max-age = N not specified Expires, then use the value srcache_default_expire specified. If the value exceeds srcache_max_expire command, then the final value of this variable srcache_max_expire. $ Srcache_fetch_status get the cached value of three states: HIT, MISS, BYPASS. $ Srcache_store_status cached value of two states: STORE, BYPASS. 5. Installation nginx_memc module and nginx_srcache
# wget https://github.com/agentzh/memc-nginx-module/archive/master.zip
# wget https://github.com/agentzh/srcache-nginx-module/archive/master.zip
# ./configure --prefix=/usr/local/nginx-1.2.5 \
--add-module=../srcache-nginx-module
--add-module=../memc-nginx-module
# make
# make install
6. Configure
upstream memcacheds {
        server 10.1.240.166:22222;
}
server  {
        listen       8090;
        server_name  test.ttlsa.com;
        index index.html index.htm index.php;
        root  /data/wwwroot/www.ttlsa.com/webroot;

        location /memc {
                internal;
                memc_connect_timeout 100ms;
                memc_send_timeout 100ms;
                memc_read_timeout 100ms;
                set $memc_key $query_string;
                set $memc_exptime 120;
                memc_pass memcacheds;
                }

        location ~ .*\.php?$
        {
                if ($uri ~ /ttlsa/){
                        set $ttlsa_key $request_uri;
                        srcache_fetch GET /memc $ttlsa_key;
                        srcache_store PUT /memc $ttlsa_key;
                        add_header X-Cached-From $srcache_fetch_status;
                        add_header X-Cached-Store $srcache_store_status;
                }
                include fastcgi_params;
                fastcgi_pass  127.0.0.1:10081;
                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;
        }
}
7.1 7. Test first access (404 Not found created cache) ngx-1 7.2 subsequent access (directly from the cache response) ngx-2 for the first time to create the cache access, the subsequent access in the cache expiration time, returns from the memcached, does not require re php -fpm process. Greatly enhance the dynamic page access speed. " Memc_nginx + srcache_nginx + memcached problems encountered " For reprint please indicate the source: http: //www.ttlsa.com/html/2460.html

Reproduced in: https: //my.oschina.net/766/blog/211446

Guess you like

Origin blog.csdn.net/weixin_34257076/article/details/91547528