Nginx cache configuration

foreword

I talked about the forward proxy and reverse proxy of Nginx before, but for our yum source proxy, this is definitely not enough, it is better to be able to cache resources. After all, most of the things in the yum source are static resources and will not change. If these resources can be cached, it will not only speed up the acquisition of resources by users, but also reduce export traffic and bandwidth. This is actually the original intention of CDN.

cache configuration

Next, let's talk about the cache configuration of nginx. Not much to say, go directly to the configuration.

proxy_cache_path /cache/nginx/cache levels=1:2 keys_zone=yum:500m inactive=1000d max_size=80g;

server {
        server_name www.baidu.com;
        proxy_cache yum;
        proxy_cache_valid 200 304 302 1000d;
        proxy_cache_valid any 1m;
        proxy_ignore_headers Set-Cookie;
        proxy_ignore_headers Cache-Control;
        proxy_ignore_headers Expires;
        proxy_hide_header Cache-Control;
        proxy_hide_header Set-Cookie;
        proxy_hide_header Expires;
        location / {
                proxy_pass http://www.baidu.com/;
        }
}
in,

proxy_cache_path represents the path of the system cache file, which must be created before it can be used, otherwise nginx will fail to start

levels set the cache file directory level, levels=1:2 means two-level directory

keys_zone sets the cache name and cache shared memory size

Inactive will be deleted if no one is accessed within the specified time. The default is 10min. Here it is set to 1000d, which is so bold. In fact, it can be completely replaced later as a yum source.

max_size maximum cache space, if the cache space is full, the resource with the longest cache time will be overwritten by default

proxy_cache uses the corresponding cache configuration named yum, which should be consistent with the name set by keys_zone above

proxy_cache_valid sets the cache time for the response of the specified http status code. The expiration time is also set to 1000d here. Use it as long as you can, and don't go back to the source for verification.

proxy_ignore_headers ignores a header field of the http response of the origin site. The three fields ignored here are to ensure that the cache does not expire, and static resources do not need to expire.

proxy_hide_header sets a field not to be used when the local server interacts with the origin site, also to ensure that the cache does not expire

proxy_pass sets the server address we want to proxy, that is, the source site address

The last few header fields of the HTTP response are ignored because many website resources are not set to be cached, or set to expire, so that our proxy server cannot cache, and every time we need to retrieve the source, or verify it. As shown in the figure below, the Baidu homepage is set to not allow caching.

write picture description here

in,
private: Indicates that the whole or part of the response message for a single user cannot be processed by the shared cache, and can only respond to the user who previously requested the content with the cached content
no-cache: Indicates that the request or response message cannot be cached, it can actually be stored in the local cache, but the cache cannot provide it to the client before the freshness verification with the origin server, that is, donot-serve -from-cache-without-revalidation
no-store: The cache should remove all traces of the document from storage as soon as possible, as it may contain sensitive information
in addition,
Pragma: Used to contain implementation-specific instructions, the most commonly used is Pragma:no-cache. In the HTTP/1.1 protocol, its meaning is the same as Cache-Control:no-cache.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606729&siteId=291194637