Nginx Web Cache Configuration __web

Source: Internet
Author: User



Turn from: http://ttcool.blog.51cto.com/1186572/1302866

1. Scenario: Use Proxy_cache to cache Web static content files and HTML full-page caching via rewrite pseudo static.

Dynamic content is dynamically acquired through Ajax, and content consistency is maintained.

2.nginx Configuration

A. Add cache file Save path

Vi/etc/nginx/nginx.conf

#注: The paths specified by Proxy_temp_path and Proxy_cache_path must be in the same partition
Proxy_temp_path/cache/proxy_temp_path;

#设置Web缓存区名称为cache_one, the memory cache space size is 200mb,1 Day has not been accessed automatically clear, hard disk cache space size of 30GB.

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


B. Adding a cache configuration


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

{

#如果后端的服务器返回502, 504, execution timeout, and so on, automatically forwards the request to another server in the upstream load balancing pool to implement failover.
Proxy_next_upstream http_502 http_504 error timeout invalid_header;

Proxy_cache Cache_one;
#进行缓存, using Web cache Cache_one

Proxy_cache_valid 304 5m;
#对不同的HTTP状态码设置不同的缓存时间

Proxy_cache_valid 302 1m;
Proxy_cache_valid any 1m;
Proxy_cache_key $host $uri$is_args$args;
#以域名, URI, and parameters are combined into the Web cache key value, nginx the cache content to the level two cache directory according to the key value hash
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";
#设定proxy_set_header accept-encoding '; (or the backend server shuts down gzip), so that the machine will not cache the compressed files, causing garbled
#proxy_set_header accept-encoding ""; This can also be

Proxy_ignore_headers "Cache-control" "Expires";
#这段配置加上后, Proxy_cache can support background-setting expires.


if (!-e $request _filename)

{
Proxy_pass http://emulsites;
}

Expires 30m;

}


#php文件请求转发给后端
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. Heavy Duty Nginx

sudo service Nginx Reload


Access the page to see if the cache directory generates a cached file

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.