Nginx Cache Control

Source: Internet
Author: User

Nginx has several parameters to control the cache. This example uses nginx to cache image files.

First, cache pool
Proxy_cache_path/var/cache/nginx/proxy_cache levels = 1:2 keys_zone = static: 50 m max_size = 10g inactive = 3d;

This parameter controls the size of the cache pool and the frequency of interaction with the source server. Cache elimination is very passive, just to maintain the size of the cache pool.

In this example, a 50 m shared memory is used to maintain the list of cached files. If a file is inactive for three days, that is, it is not accessed twice, it is eliminated in the cache pool, the maximum number of files cached on the hard disk is 10 Gb. If all the files in the cache pool are files within three days, and the total number of files has reached 10 Gb, in this case, the cache pool's elimination rule is LRU -- Last Recent Used (with the lowest access frequency eliminated ).

Second, the cache Lifecycle
Proxy_cache_valid 200 302 3d;

This parameter directly controls the cache lifecycle,
In this example, if the image is accessed again within three days, the cache pool is HIT and HIT is returned,
After three days, the cache pool will be skipped, retrieved from the source server, and compared with the cache pool.
If there is a cache pool, the timestamp of the cache is updated and EXPIRE is returned,
If the cache pool does not exist, the cache is regenerated and MISS is returned,
This value should be <= the file expiration time of the cache pool to increase the hit rate.

Third, browser cache Lifecycle

Expires 7d

This parameter controls how long the browser will save images, which can be divided into several stages:
(Different browser behaviors are slightly different. Take chrome as an example)


1. When the customer opens the webpage for the first time using the browser, the server returns 200, and the image enters the browser cache. The lifecycle is 7 days.
2. The next day, when the customer opened the webpage again in a browser, The Returned Code was still 200, but hit the local cache.
3. The customer refreshed the browser using F5 and forced communication with the server. After comparing with the server, the customer found that the file is the same as the browser cache, and thus got 304. If the file is different, it is 200.

The first two parameters have a greater impact on the cache server.

First, we should maintain a buffer pool as large as possible to provide wide coverage;
Second, we need to increase the cache hit rate and reduce the interaction frequency between the cache and the source server.

Finally, if it is a small cache, we can choose to put the cache in the shared memory to save expensive disk IO.

Recommended reading:

 

Configure and optimize reverse proxy and load balancing in Nginx

 

Nginx load balancing: nginx: [emerg] cocould not build the types_hash

 

Nginx Load Balancing module ngx_http_upstream_module details

 

Nginx + Firebug allows the browser to tell you which Server Load balancer distributes requests

 

Ubuntu install Nginx php5-fpm MySQL (LNMP environment setup)

 

Nginx details: click here
Nginx: click here

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.