The reason and solution of nginx cache problem _nginx

Source: Internet
Author: User
Tags current time

nginx.conf part of the content:

  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;
    #设置缓存的key
    proxy_cache_key $host $uri$is_args$args;
    #设置状态码为200和304的响应可以进行缓存, and the cache time is 1 days
    proxy_cache_valid 304 1d;
    Expires 30d;
  }

Nginx does not cache reason

By default, whether the cache is nginx by the Nginx cache server is determined by the source server, and the caching server needs to strictly follow the header of the source server response to determine whether caching and caching is frequent.

Headers are mainly as follows:

Cache-control:no-cache、no-store

If these two values are present, the Nginx cache server is definitely not cached

Expires:1980-01-01

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

Resolving non-caching scenarios

2.1 Method One:

To modify the header of a program or source server Web program response

2.2 Method Two:

The Nginx agent directly adds the following sentence:

Proxy_ignore_headers x-accel-expires Expires Cache-control Set-cookie;

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.