These locally saved copies have an expiration time, after which they will be updated. There are many ways to judge whether a copy of data is out of date, which can be judged by retention time or data integrity.
Many Web servers also have the function of checking, that is, when some copy data is out of date, they first send a check request to the back-end server, and the back-end server checks these data. If there is no difference between the original data and the copy, the expired copy will be reset to the usable copy.
The above nginx configurations are used in combination:
The proxy_params file is configured as follows:
Visit the page once and send an interface data request to the cache result in the directory: 8080/api/ View /cache/nginx:
Ngx_cache_purge is a third-party module of nginx, which can help me clear the cache in nginx.
In the previous nginx compilation and installation, we did not compile the cache clearing module ngx_cache_purge. If the cache is started without installing this module, when we try to clear the cache in later debugging, an exception will appear:
This exception shows that we can't find the driver of this instruction, and we need to follow the relevant modules.
Ngx_cache_purge is only a third-party module of nginx, not specialized software, so we need to recompile nginx as follows:
Configure nginx:
Nginx successfully started again;
Next, perform the cache clearing test, and visit:/clear _ cache/API/index.php. Visiting this connection will clear the cached data of the interface:/API/index.php. ..
The results are as follows:
In project development, it is impossible to cache everything. Caching is only suitable for caching frequently queried data and does not need real-time updating. This is its suitable scenario. Our above configuration will cache interface data as long as we access the api interface directory, which is unreasonable for some interface data that need to be updated in real time. We need to control what nginx caches and what it doesn't.
When the set instruction is set as a variable, multiple values can be set in the proxy_no_cache parameter, but as long as one of the multiple values is not 0, the data will be responded through the cache.
This directive is used to configure the name of the public memory area where cached index data can be stored. These data are established by the cache index reconstruction process when Nginx server is started, and the cache management process is responsible for regularly detecting expired data and searching during the whole operation of Nginx server.
This directive is used to configure the condition that the Nginx server will not get the response data from the cache when sending the response data to the client. These conditions support public variables configured with Nginx.
Look at an example:
Among them, Scookie_nocache, Sarg_nocache, Sarg_comment, Shttp_pragma and Shttp_authorization are all variables of Nginx configuration file.
This instruction is used to set the keywords that Nginx server uses to index the cached data in memory.
If we want the cached data to contain keywords such as the host name of the server, we can set the directive to:
This instruction is used to set whether to turn on the locking function of the cache. In cache, some data items can be filled with response data returned by multiple requests at the same time. When this function is enabled, the Nginx server can only have one request to fill a data item in the cache at the same time, which is equivalent to locking the data item and not allowing other requested operations. If other requests want to fill this data item, they must wait for the lock of this data item to be released. The waiting time is configured by the proxy_cache_lock_timeout instruction.
This instruction is used to set the timeout of locking after the locking function of the cache is turned on. Please refer to the relevant contents of proxy_cache_lock instruction for details.
Among them, the time is the set time, and the default is 5s.
This instruction is used to set the number of times the client requests to send. When the client sends the same request to the proxy server for the number of times set by the instruction, the Nginx server caches the response data of the request. Setting this value reasonably can effectively reduce the amount of hard disk cache data and improve the cache hit rate.
Where, number is the set number of times. The default setting is 1.
This instruction is used to set the path where Nginx server stores cached data and the contents related to cache index.
The setting of this instruction is complicated, and generally the first three instructions need to be set. The latter variables are related to the performance of Nginx server cache index reconstruction process and management process, so it is generally enough to keep the default settings. Let's look at a few simple configuration examples:
If Nginx accesses the proxy server, the proxy server cannot access it or an access error occurs, the Nginx server can use the history cache to respond to the client's request. These data may be inconsistent with the latest data on the proxy server, but for the back-end server with low update frequency, this function of Nginx server can provide uninterrupted access to the client to some extent. This instruction is used to set some states, and when the back-end proxy server is in these states, the Nginx server enables this function.
The states that the instruction can support are displayed in the syntax structure.
This instruction can set different cache times for different HTTP response states,
This instruction is the same as the configuration without using the caching function.
This directive configures whether to cache the response data from the proxy server on the local disk. This is another method of caching data provided by Nginx server, but this function is simpler than proxy caching. It does not provide functions such as cache expiration update and memory index establishment, and does not occupy memory space, which is better for static data.
The proxy storage method is usually used to cache the response data of the proxy server when an error occurs.
This instruction is used to set the access rights of users or user groups to the data cached by the proxy storage.
Regarding the use of proxy storage methods, we have a deeper understanding through an official example, in which the author explains the configuration through comments: