Nginx web Cache Configuration

Source: Internet
Author: User

1. Scenario: Use proxy_cache to cache web static content files and html whole page cache after pseudo-static modification through rewrite.

Dynamic Content is dynamically obtained through Ajax, And the content remains consistent.

2. nginx Configuration

A. Add the cache file storage path

Vi/etc/nginx. conf

# Note: The paths specified by proxy_temp_path and proxy_cache_path must be in the same partition.
Proxy_temp_path/cache/proxy_temp_path;

# Set the name of the Web cache area to cache_one. The size of the memory cache space is 200 MB. The content not accessed within one day is automatically cleared. The size of the hard disk cache space is 30 GB.

Proxy_cache_path/cach/proxy_cache_path levels = 1:2 keys_zone = cache_one: 200 m inactive = 5 m max_size = 30g;


B. Add cache Configuration


Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf | js | css | html) $

{

# If the backend server returns errors such as 502, 504, and execution timeout, the request is automatically forwarded to another server in the upstream Server Load balancer pool for failover.
Proxy_next_upstream http_502 http_504 error timeout invalid_header;

Proxy_cache cache_one;
# Cache, using the Web cache zone cache_one

Proxy_cache_valid 200 304 5 m;
# Set different cache times for different HTTP Status Codes

Proxy_cache_valid 301 302 1 m;
Proxy_cache_valid any 1 m;
Proxy_cache_key $ host $ uri $ is_args $ args;
# Combine domain names, Uris, and parameters into the Key values of the Web cache. Nginx hashes the cached content to the second-level cache directory based on the Key values.
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ remote_addr;
Proxy_set_header Accept-Encoding "none ";
# Set proxy_set_header Accept-Encoding ''; or disable gzip on the background server. This machine will not cache compressed files, causing garbled characters.
# Proxy_set_header Accept-Encoding ""; this can also be

Proxy_ignore_headers "Cache-Control" "Expires ";
# After this configuration, proxy_cache supports the expires set in the background.


If (! -E $ request_filename)

{
Proxy_pass http: // emulsites;
}

Expires 30 m;

}


# Forward PHP file requests to the backend
Location ~ . * \. (Php | cgi )? $
{
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_pass http: // emulsites;
}


3. Reload nginx

Sudo service nginx reload


Access the page to check whether the cache directory generates cached files






Related Article

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.