Preface
The advantages of using cache are to reduce data transmission, save network traffic, and speed up response speed; reduce server pressure; provide high availability on the server; disadvantages are data inconsistency; increase costs
Nginx is a web cache server, which is between the client and the application server. When a user accesses a URL through the browser, the web cache server will go to the application server to obtain the content to be displayed to the user and cache the content to its own server. When the next request comes, if the same URL is accessed, the web cache server will directly return the previously cached content to the client.
1. Web Caching Service
1.1 Principle
Nginx's web caching service mainly uses the ngx_http_proxy_module module, which is implemented based on the Proxy Store. The principle is to treat URLs and related combinations as keys. The key is hashed by using the MD5 algorithm to obtain the corresponding hash directory path on the hard disk, so as to save the cached content in this directory. It can support any URL connection, and also supports non-200 status codes such as 404/301/302. Nginx can support setting expiration time for specified URLs or status codes, or use the purge command to manually clear the cache of specified URLs.
1.2 Instructions
1.2.1 proxy_cache_path
grammar:
proxy_cache_path path [levels=number] keys_zone=zone_name:zone_size [inactive=time][max_size=size];
- path: cache path address
- levels: Specify the corresponding directory of this cache space. You can set up to 3 layers, with each layer taking the value of 1|2 (MD5 encrypted ciphertext takes the value of 1 to 2 characters from behind to front)
- keys_zone: Used to set the name and size for this cache area
- inactive: The specified cached data will be deleted if it is not accessed multiple times.
- max_size: Set the maximum cache space. If the cache space is full, the resource with the longest cache time will be overwritten by default.
Example:
http { proxy_cache_path /usr/local/proxy_cache keys_zone=cz:200m levels=1:2:1 inactive=1d max_size=20g; }
1.2.2 proxy_cache
Turn on or off the proxy cache. If it is enabled, customize which cache area to use to cache.
1.2.3 proxy_cache_key
Set the key value of the web cache, Nginx will cache the MD5 hash according to the key value
1.2.4 proxy_cache_valid
Set different cache times for URLs with different return status codes
1.2.5 proxy_cache_min_uses
Set how many times the resource is accessed and cached
1.2.6 proxy_cache_methods
Set what HTTP methods to cache
2. Cache cases
Setting in http block
proxy_cache_path /usr/local/proxy_cache keys_zone=cz:200m levels=1:2:1 inactive=1d max_size=20g;
server { listen 12305; location ~* .\.(png|jpg|css|html|js)$ { proxy_cache cz; proxy_cache_key $scheme$proxy_host$request_uri; proxy_cache_min_uses 1; proxy_cache_valid 200 1d; proxy_cache_valid any 1m; add_header nginx-cache "$upstream_cache_status"; proxy_pass http://172.18.25.50:12306; } } server { listen 12306; location ^~ /bdosp { alias /mnt/disk1/home/ZLQ_YL/myUI/bdops_ui/bdopsDist/bdosp; index ; } }
3. Clear cache
3.1 Delete the corresponding cache directory
rm -rf /usr/local/proxy_cache
3.2 Use the third-party extension module ngx_cache_purge and use the proxy_cache_purge directive
4. Set resource without cache
4.1 proxy_no_cache
Define conditions that do not cache data
Example:
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
4.2 proxy_cache_bypass
Set conditions that do not get data from cache
Example:
proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
$cookie_nocache refers to the value corresponding to the key in the currently requested cookie.
$arg_nocache and $arg_comment refer to the attribute values corresponding to the attribute names nocache and comment in the currently requested parameter.
At least one is not empty and not equal to 0, then the condition is true
4.3 Setting up a configuration plan that does not cache resources
server{ listen 8080; server_name localhost; location / { if ($request_uri ~ /.*\.js$){ set $nocache 1; } proxy_no_cache $nocache $cookie_nocache $arg_nocache $arg_comment; proxy_cache_bypass $nocache $cookie_nocache $arg_nocache $arg_comment; } }
This is the end of this article about how nginx configuration files do not use cache. For more related nginx files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!