In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Use the scene:
The page of the project needs to load a lot of data, which is not often changed, and does not involve personalized customization. The performance of dynamically generating data for each request is not as good as caching the results according to request routing and parameters. Using Nginx cache will greatly improve the speed of requests.
Basics
You only need to configure proxy_cache_path, which is used to set the path and configuration of the cache, and proxy_cache, which is used to enable caching, to turn on content caching.
Http {... Proxy_cache_path / path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; server {proxy_cache mycache; location / {proxy_pass http://localhost:8000;}
Description of the corresponding parameters:
1. The local disk directory used for caching is / path/to/cache/
2.levels sets up a two-level hierarchical directory at / path/to/cache/. Placing a large number of files in a single directory can lead to slow file access, so for most deployments, we recommend a two-level directory hierarchy. If the levels parameter is not configured, NGINX places all files in the same directory.
3.keys_zone sets up a shared memory area that stores cache keys and metadata, somewhat similar to timers. Putting a copy of the key in memory allows NGINX to quickly decide whether a request is HIT or MISS without retrieving the disk, which greatly improves the retrieval speed. The memory space of a 1MB can store about 8000 key, so the 10MB memory space configured above can store about 80000 key.
4.max_size sets the upper limit of the cache (10G in the above example). This is optional; if you do not specify a specific value, it allows the cache to grow and take up all available disk space. When the cache reaches this online, the processor calls cache manager to remove the least recently used files, thus reducing the cache space below this limit.
5.inactive specifies how long the project can remain in memory without being accessed. In the above example, if a file is not requested within 60 minutes, cache management will automatically delete it in memory, regardless of whether the file expires or not. The default value of this parameter is 10 minutes (10m). Note that inactive content is different from expired content. NGINX does not automatically delete expired content specified by the cache control header (in this case, Cache-Control:max-age=120). Expired content will be deleted only if it is not accessed within the time specified by inactive. If expired content is accessed, NGINX refreshes it from the original server and updates the corresponding inactive timer.
6.NGINX initially places files destined to write to the cache in a temporary storage area, and the use_temp_path=off command instructs NGINX to write them to the same directory when they are cached. We strongly recommend that you set the parameter to off to avoid unnecessary data copying in the file system. Use_temp_path is introduced in the NGINX1.7 version and NGINX Plus R6.
Finally, the proxy_cache command starts caching those parts of the URL that match the location (in this case, /). You can also add the proxy_cache command to the server section, which will apply the cache to all services that do not specify their own proxy_cache command in the location.
Nginx cache related processes
Two additional NGINX processes are involved in the cache:
Cache manager starts periodically to check the status of the cache. If the cache size exceeds the limit set by the max_size parameter in proxy_cache_path, the cache manager deletes the recently accessed data. Between starts of the cache manager, the amount of data cached may briefly exceed the configured size. Cache loader runs only once, after NGINX starts. It loads metadata about previously cached data into the shared memory area. Loading the entire cache at once may consume enough resources to degrade NGINX performance in the first few minutes after startup. To avoid this, configure the iterative loading of the cache by including the following parameters in the proxy_cache_path directive: loader_threshold-iteration duration, in milliseconds (200 by default) loader_files-maximum number of items loaded during an iteration (100 by default) loader_sleeps-delay between iterations in milliseconds (50 by default)
In the following example, the iteration lasts for 300 milliseconds or until 200 projects are loaded:
Proxy_cache_path / data/nginx/cache keys_zone=one:10m loader_threshold=300 loader_files=200
Other commonly used parameters
Example configuration:
Proxy_cache_path / path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;server {... Location / {proxy_cache my_cache; # proxy_cache_key "$host$request_uri$cookie_user"; proxy_cache_min_uses 3; proxy_cache_methods GET HEAD POST; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; # proxy_cache_valid any 5m; proxy_pass http://localhost:8000;}}
Description of the corresponding parameters:
Proxy_cache_key specifies the cached key to change the request characteristics used when calculating the key. This is not recommended. The example is to use the domain name, request url, and user cookie as key, which means that a page will cache n times for different users. In most cases, this operation is not required. Proxy_cache_min_uses is the minimum number of requests that must use the same key before caching the response. Proxy_cache_methods is the response value that specifies the request method to be cached. It defaults to GET and HEAD. Other new requests are listed together, as shown in the example above. Proxy_cache_valid is the cache time in response to status codes. The example can specify a time for each status code cache, or you can use any to cache all status codes.
Clear cach
A configuration needs to be added in advance to identify requests using the HTTP PURGE method and to delete the cache corresponding to the matching URL.
1. Create a new variable, such as $purge_method, in the context of http {}, which depends on the $request_method variable:
Http {... Map $request_method $purge_method {PURGE 1; default 0;}}
two。 In the location {} block, under the premise that the cache has been configured, the proxy_cache_purge parameter is introduced to specify the conditions under which the cache request is cleared. For example, $request_method specified in the previous step
Server {listen 80; server_name www.example.com; location / {proxy_pass https://localhost:8002; proxy_cache mycache; proxy_cache_purge $purge_method;}}
Once configured and effective, you can send a purge request to invalidate the cache, for example:
Curl-X PURGE-D-https://www.example.com/*
In this example, resources with public URL portions (specified by asterisk wildcards) are cleared. However, these cache entries are not completely deleted from the cache: they remain on disk until they are considered inactive (determined by the inactive parameter in proxy_cache_path), or when the cache scavenger (determined by purge in proxy_cache_path), or when the client tries to access them.
Reference link:
Nginx caching uses the official guide Nginx content to cache documents
Summary
The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.