Reasons and solutions for nginx cache not caching problems

  • 2020-05-12 06:48:55
  • OfStack

nginx.conf


  proxy_temp_path  /nginx/cache/temp;
  proxy_cache_path /nginx/cache/path levels=1:2 keys_zone=cache_test:2048m inactive=7d max_size=10g;
......
 location ~ .(gif|jpg|jgep|png)$ {
    proxy_pass http://upstreams;
   
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
    proxy_cache cache_test;
    # Set cache key
    proxy_cache_key $host$uri$is_args$args;
    # Set the status code to 200 and 304 The response can be cached, and the cache time is 1 day 
    proxy_cache_valid 200 304 1d;
    expires 30d;
  }

nginx does not cache reasons

By default, whether or not nginx is cached is determined by the nginx cache server and the source server, and the cache server needs to strictly follow the header response of the source server to decide whether or not to cache and how often to cache.

header mainly includes the following:

Cache-control:no-cache、no-store

If these two values are present, the nginx cache server will never cache them

Expires:1980-01-01

If the date appears earlier than the current time, it will not be cached.

Resolve the non-caching solution

2.1 method 1:

Modify the program or the header response of the source server web program

2.2 method 2:

The nginx agent simply adds the following sentence:


proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: