Using Nginx Proxy_cache for website cache backing configuration Nginx fastcgi cache nginx Cache configuration Nginx file Cach

Source: Internet
Author: User
It is well known that Nginx Proxy_cache can cache the requested response, act like a CDN, or even provide more functionality than a CDN, and can also be used to cache backing data, when the background tomcat hangs up, Nginx directly returns the backing data in the cache to the user.

Directly attach the detailed configuration

Upstream Tomcat_localhost {

Note The timeout here does not set too long
Server 127.0.0.1:8080 weight=10 Max_fails=1 fail_timeout=1s;
}

1.nginx cache space configuration, which is defined above the server of the Nginx configuration file

#proxy_cache_path: The cache file path of the proxy cache,/export/data/huishou.jd.local the directory must be created in advance, otherwise the profile detection fails, and the directory cannot be deleted, the cache disappears after deletion

# Levels=1:2 Nginx will create a two level directory under the cache file path of the above configuration, the first level directory is named one character, the second level directory is named 2 characters

#keys_z max_size=100m defines the name and cache size of the cache, the cached name is used in the location below, and the cache size can be set in combination with the size of the server's memory and the amount of content the system needs to cache.

# inactive=1d The cache file is valid, the cache reaches the time after the setting is removed from the cache path regardless of whether it is used, and then back to the source to get new data in the build cache file

Proxy_cache_path/export/datal/cache levels=1:2 keys_z max_size=300m inactive=1d;

2.nginx Cache backing interception configuration, which is within a specific location

Location/{

#配置上面定义的缓存空间名称, required configuration
Proxy_cache My_cache;
#设置缓存的key, you can also customize according to the parameters in the URL, $args represent all parameters, or you can use one of these parameters, for example: $arg _name, which means that only the name parameter in the parameter list is taken, which can be flexibly configured, the required configuration

Proxy_cache_key $host $uri$is_args$args;

#设置绕过缓存的请求url that the URL contains the value of the configuration, the request does not fetch data from the cache, it must not be configured

Proxy_cache_bypass $arg _nocache;

#根据响应码设置缓存时间, more than this time, even if the cache file has cached data, Nginx will also request new data back to the source. The values here can also be controlled based on the Cache-control cache time set in the header of the response, with the highest priority in the header, and if no cache time is set here and in the header, Nginx does not generate the cache file. Do not have to be configured, if not configured here, the header must have settings
Proxy_cache_valid 1m;

#此处是托底配置, the old is better than the error, when Nginx request the background server error, if the wrong response code to return the configuration, Nginx directly take the old data in the cache file back to the user, backing use the required configuration
Proxy_cache_use_stale Error Timeout updating http_500 http_502 http_503 http_504;

#缓存并发锁, when the Nginx cache is not hit, only one request back to the source tomcat requests the data, and the other requests wait. Non-mandatory configuration
Proxy_cache_lock on;

#等待锁超时时间设置 Non-mandatory configuration
Proxy_cache_lock_timeout 1s;
Proxy_pass tomcat_localhost ;
}

3. After the above configuration, Nginx cache backing configuration is complete, in the cache file validity time, even if the background tomcat hangs, the cached page or interface can also provide the normal service.

The above describes the use of Nginx Proxy_cache site cache backing configuration, including the nginx,cache aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.