Nginx page cache one

Step 1: Add http-level cache configuration in nginx.conf

copy code
##cache## 
    proxy_connect_timeout 500 ;
     #Timeout for connecting to the backend server_Initiate handshake and wait for response timeout
    proxy_read_timeout 600 ;
     #After the connection is successful _ the time to wait for the response of the back-end server_ has actually entered the back-end queue for processing
    proxy_send_timeout 500 ;
     #Backend server data return time_ means that the backend server must transmit all data within the specified time
    proxy_buffer_size 128k;
    #Proxy request cache area_This cache area will save the user's header information for Nginx to process rules_Generally, as long as the header information can be saved  
    proxy_buffers 4 128k;
     #Same as above to tell Nginx how much space can be used to save several Buffers for a single use
    proxy_busy_buffers_size 256k;
    #If the system is very busy, you can apply for a larger proxy_buffers Official recommendation* 2
    proxy_temp_file_write_size 128k;
    # proxy cache temporary file size
    proxy_temp_path /usr/ local /nginx/ temp;
     #Used to specify a local directory to buffer larger proxy requests
    proxy_cache_path /usr/ local /nginx/cache levels= 1 : 2 keys_zone =cache_one:200m inactive=1d max_size= 30g;
     #Set the name of the web cache area to cache_one, the size of the memory cache space to 12000M, and automatically clear it if it has not been accessed for more than 15 days The cached data that has been passed, the hard disk cache space size is 200g
copy code

The focus here is on the last sentence. The cache storage path is: /usr/local/nginx/cache, and levels=1:2 means that the cached directory structure is a 2-level directory.

As shown in the figure below, the cache will be generated in the /usr/local/nginx/cache directory, including a second-level directory, under which is the cache file. When testing, you can go to this directory to check whether the cache file is generated.

Step 2: Add a cache to the location where static files are accessed

copy code
#Static data save time
location ~ \.html$ {
      proxy_pass http://source.qingk.cn;
      proxy_redirect off;
      proxy_cache cache_one;
      #The cache_one here must have the same name as the cache area configured in the previous step
      proxy_cache_valid 200 304 12h;
      proxy_cache_valid 301 302 1d;
      proxy_cache_valid any 1m;
      #Different requests set different cache aging
      proxy_cache_key $uri$is_args$args;
      #The key of the production cache file is generated by combining 4 string variables
      expires 30d;
      #The other types of caches are valid for 30 days
      proxy_set_header X-Forwarded-Proto $scheme;
}
copy code

There are 3 points to note here:

1. The cache will be generated only when proxy_pass is executed. When the next request is executed to proxy_pass, it will be judged whether there is a cache. If there is, the cache will be directly read and returned to the client, and proxy_pass will not be executed; if not, proxy_pass will be executed. , and generate the cache file according to the rules; you can go to the cache folder of nginx to see whether the cache file is generated.

2. The sentence proxy_set_header Host $host may cause cache failure, so this sentence cannot be configured. I encountered this problem while testing, and I don't know why.

3. The proxy_pass uses upstream to travel, and it is feasible to replace it with a domain name or ip.

Step 3: Configure the path of static files in the location where proxy_pass jumps

location ~ .*\.(html)$ {
    default_type 'text/html';
    root "/usr/local/openresty/nginx/html";
}

将nginx本地存放静态文件的路径配到root指令处

如果没有这一句:default_type 'text/html',所有的请求都默认是下载文件,而不是访问html页面

到此,静态文件缓存已经配置完成。但是还差很重要的最后一步,缓存生成之后会阻止访问进入后台和nginx本地,如果有更新,则更新内容无法生效,还需要一种手动清除缓存的机制。

第四步:清除缓存

缓存文件是根据proxy_cache_key这个指令生成的,所以找到对应的缓存文件,删除即可

copy code
location ~ /purge(/.*) {
    #删除指定缓存区域cache_one的特定缓存文件$1$is_args$args
    proxy_cache_purge cache_one $1$is_args$args;
    #运行本机和10.0.217.0网段的机器访问,拒绝其它所有  
    allow           127.0.0.1;
    allow           10.0.217.0/24;
    deny          all;
}
copy code

To delete the cache use the proxy_cache_purge directive.

Guess you like

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