Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of establishing cache system by configuring srcache_nginx module with Redis in Nginx

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "Nginx configuration srcache_nginx module with Redis to establish a cache system", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Nginx configuration srcache_nginx module with Redis method to establish a cache system" article.

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.confdaemonize yespidfile / var/run/redis-6379.pidport 6379bind 127.0.0.1timeout 0tcp-keepalive 0loglevel noticelogfile stdoutdatabases 16stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbslave-serve-stale-data yesslave-read-only yesrepl-disable-tcp-nodelay noslave-priority 100maxmemory 8096mbmaxmemory-policy volatile-ttlappendonly noappendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128hashMaxMuziplist- Entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes

Since redis is used only as a cache, persistence is not enabled.

3. Nginx configuration

# vim nginx.confhttp {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.0Comp8; real_ip_header x-forwarded-for Include vhosts/test.jb51.net.conf;} # vim vhosts/test.jb51.net.confupstream redis {server 127.0.0.1 keepalive 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 $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.jb51.net-photo-access.log srcache_log } include fastcgi_params; fastcgi_pass 127.0.0.1 fastcgi_read_timeout 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 is done:

Do cache status:

5. Response head state

First request:

Request again:

6. Check whether redis is cached and when it expires

Ps:srcache-nginx-module module instruction description:

Srcache_fetch

Grammar: srcache_fetch?

Default value: no

Configuration segment: http, server, location, location if

Query cache. A return of 200 indicates that the cache hits and responds to client requests directly from the cache. Non-200 requires back-end processing.

Srcache_fetch_skip

Syntax: srcache_fetch_skip

Default value: srcache_fetch_skip 0

Configuration segment: http, server, location, location if

The nginx variable is supported. When the value of this parameter is not empty and not equal to 0, the data caching process is skipped unconditionally.

Srcache_store

Grammar: srcache_store?

Default value: no

Configuration segment: http, server, location, location if

Caches the response to the current request. You can disable caching using the srcache_store_skip and srcache_store_max_size directives. Whether it is the response status line, the response header, and the response body are cached. 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 and srcache_store_hide_header instructions to control which headers are cached and which are not.

Note: even if all response data is sent immediately, the current nginx request life cycle may not be complete until the srcache_ store request is completed. This means that the server side delays closing the tcp connection, or the next requesting service sends the same tcp connection.

Srcache_store_max_size

Syntax: srcache_store_max_size

Default value: srcache_store_max_size 0

Configuration segment: http, server, location, location if

When the response body exceeds this value, it will not be cached.

This instruction is useful when the back-end cache store has a hard limit on cached data. For example, the upper limit of the memcached server is 1m.

The default value is 0 and there is no limit.

Srcache_store_skip

Syntax: srcache_store_skip

Default value: srcache_store_skip 0

Configuration segment: http, server, location, location if

The nginx variable is supported. When the value of this parameter is not empty and not equal to 0, it is skipped unconditionally from the cache process.

Srcache_store_statuses

Grammar: srcache_store_statuses..

Default value: srcache_store_statuses 200301 302

Configuration segment: http, server, location, location if

This instruction controls which status code responses are cached.

Srcache_header_buffer_size

Syntax: srcache_header_buffer_size

Default value: srcache_header_buffer_size 4k/8k

Configuration segment: http, server, location, location if

Controls the head buffer size when serializing the response header. The default size is the page size, usually 4k or 8k, depending on the platform.

Note: this size is based on each head, 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 the following headers:

Connection

Keep-alive

Proxy-authenticate

Proxy-authorization

Te

Trailers

Transfer-encoding

Upgrade

Set-cookie

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

Srcache_store_hide_header xmurfootersrcacheft storefronts hidewings 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 the following headers:

Connection

Keep-alive

Proxy-authenticate

Proxy-authorization

Te

Trailers

Transfer-encoding

Upgrade

Set-cookie

Multiple response headers can be cached without case sensitivity. Such as

Srcache_store_pass_header set-cookie;srcache_store_pass_header proxy-autenticate

Srcache_methods

Syntax: srcache_methods...

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 gzip/deflate compression be disabled on the back-end server. 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 processed as follows:

1. During the srcache_fetch query cache operation, the request headers cache-control: no-cache and pragma: no-cache will be skipped.

2. When 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 relying on cache acceleration is considered the safest for busy sites.

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 instruction is on, the response headers cache-control and expires are processed as follows:

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: skips srcache_store.

This instruction has a higher priority 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

Turning on this directive forces 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

Turning on this directive forces 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

Turning on this directive forces the response header cache-control: private. The default is off.

Srcache_default_expire

Syntax: srcache_default_expire

Default value: srcache_default_expire 60s

Configuration segment: http, server, location, location if

Controls the default expiration time. The allowed value of the $srcache_expire variable 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

Default value: srcache_max_expire 0

Configuration segment: http, server, location, location if

Controls the maximum cache time, which takes precedence over other calculation methods.

The value must be less than 597hours.

The default is 0 and there is no limit.

The above is the content of this article on "how to configure srcache_nginx modules with Redis to build a cache system in Nginx". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report