This is the 28th day of my participation in the November Gwen Challenge. Please check the details: the last Gwen Challenge in 2021

Introduction of cache

Nginx http_proxy module, can achieve similar to Squid cache function.

Nginx creates a local copy of the content that has been accessed by the client on the Nginx server. In this way, when the client accesses the data again within a certain period of time, it does not need to send requests to the backend server again through the Nginx server. Therefore, the network traffic between the Nginx server and the backend server can be reduced, reducing network congestion. It also reduces data transmission delay and improves user access speed.

Also, when the backend server goes down, replica resources on the Nginx server can respond to relevant user requests, which improves the robustness of the backend service.

  • For caching, we probably have the following problems:
  1. Where do I put the cache files?
  2. How do I specify which requests are cached?
  3. How long is the cache valid?
  4. For some requests, can I leave the cache out?

Once these issues are resolved, Nginx’s cache is basically configured.

  • Where do I put the cache files?
  1. Proxy_cache_path: Nginx uses this parameter to specify the cache location.

  2. Proxy_cache: Specifies the cache name.

  3. Proxy_cache_path: There are two mandatory parameters. The first parameter is the cache directory. The second parameter, keys_zone, specifies the name of the cache and the amount of memory used.

    Nginx. confg configuration file HTTP {proxy_cache_path /data/nginx/cache keys_zone=one:10m max_size=10g; Upstream {www.nicole.com server 127.0.0.1:8881; Server 127.0.0.1:8882; Server 127.0.0.1:8883; } server{ listen 80 ; proxy_cache one ; server_name www.nicole.com; }}Copy the code

Note: in the example, 10m is the limit on the size of metadata information cached in memory. To limit the total size of the cache, use the max_size parameter.

  • How do I specify which requests are cached?
  1. Nginx caches the results of all get and HEAD requests by default, and caches keys using request strings by default.

  2. For example, proxy_cache_key “hosthosthostrequest_uri$cookie_user”;

  3. To prevent low-frequency requests from being cached, specify the number of times a request has been sent. Example: proxy_cache_min_uses 5;

  4. Specify which method requests are cached, for example, proxy_cache_methods GET HEAD POST;

    Nginx. confg configuration file HTTP {proxy_cache_path /data/nginx/cache keys_zone=one:10m; Upstream {www.nicole.com server 127.0.0.1:8881; Server 127.0.0.1:8882; Server 127.0.0.1:8883; } server{ listen 80 ; proxy_cache one ; server_name www.nicole.com; location / { proxy_pass http://www.nicole.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_cache_key $host$request_uri$cookie_user; }}}Copy the code
  • Cache validity

By default, cached content is persisted for a long time unless the total amount of the cache exceeds the limit. You can specify the cache validity time, for example:

  1. When the response status code is 200 or 302, the value is valid for 10 minutes. Proxy_cache_valid 200 302 10M.

  2. The value is valid for 5 minutes. Proxy_cache_valid any 5m.

    Nginx. confg configuration file HTTP {proxy_cache_path /data/nginx/cache keys_zone=one:10m; Upstream {www.nicole.com server 127.0.0.1:8881; Server 127.0.0.1:8882; Server 127.0.0.1:8883; } server{ listen 80 ; proxy_cache one ; server_name www.nicole.com; location / { proxy_pass http://www.nicole.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_cache_valid 200 302 10m ; }}}Copy the code
  • For some requests, can I leave the cache out?

Proxy_cache_bypass: This directive response comes from the original server rather than the cache.

For example, proxy_cache_bypass cookie_nocachecookie\_nocache cookie_nocachearg_nocache $arg_comment;

If either parameter value is not null, or not equal to 0, nginx does not search the cache and does proxy forwarding directly.

Nginx. confg configuration file HTTP {proxy_cache_path /data/nginx/cache keys_zone=one:10m; Upstream {www.nicole.com server 127.0.0.1:8881; Server 127.0.0.1:8882; Server 127.0.0.1:8883; } server{ listen 80 ; proxy_cache one ; server_name www.nicole.com; location / { proxy_pass http://www.nicole.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment ; }}}Copy the code

Principle of cache

The web page Cache is controlled by the CACHE-control in the HTTP message header. The common values include private, no-cache, max-age, and must-revalidate. The default value is private. Its functions are divided into the following situations according to different re-browsing modes:

Cache configuration

After the configuration, run the curl -i command to verify the cache status.