This article is a mirror article of machine translation, please click here to jump to the original article.

View: 2246|Reply: 0

[Web] Nginx's Web Resource Caching (CDN)

[Copy link]
Posted on 7/17/2024 10:10:31 PM | | | |
Before talking about web caching, let's talk about CDN, a content delivery network (CDN) is a distributed network built and covered on top of a bearer network, consisting of edge node server fleets all over the world. CDN can share the pressure on the origin server, avoid network congestion, ensure the distribution of website content in different regions and scenarios, and improve the speed of resource access. CDN caches origin server resources to IDC's acceleration nodes around the world, so that when end users request access to and obtain origin server resources, they can obtain the cached resources on CDN nodes nearby, improve resource access speed, and share the pressure on origin servers.

As shown in the following figure:



Simply put, when users access some static resources, they will not obtain them from the origin server after using the CDN, but will find the CDN service closest to the current user through calculation and return data.

Web caching values are a service between the user and the origin server, and Nginx provides web caching capabilities natively. Others include Varnish, Squid, and Cachewall, which are web caching proxies (some of which don't just provide web caching).

About the Nginx cache configuration documentation:The hyperlink login is visible.

First, when we first access a resource through a caching service, the cache state isMISS, will request the origin server to pull it, as shown in the figure below:



When we access the resource for the second time, we will find that the resource has been cached and the state isHIT, as shown in the figure below:



The main configuration parameters of Nginx are as follows:

Main parameters:

proxy_cache_path/data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=180d use_temp_path=off;

●path: A mandatory parameter that specifies the storage path of cached files.
●levels: Defines the hierarchy of the cache directory. Each layer can be represented by 1 (up to 16 choices, 0-f) or 2 (up to 256 choices, 00-ff), separated by : in the middle.
proxy_cache_path /data/nginx/cache;  This means that all caches have only one directory, such as /data/nginx/cache/d7b6e5978e3f042f52e875005925e51b
proxy_cache_path /data/nginx/cache levels=1:2;  It means that the cache is a second-tier directory (with 16*256=4096 directories), such as /data/nginx/cache/b/51/d7b6e5978e3f042f52e875005925e51b
● keys_zone: Mandatory parameter, define the name and size of the shared memory area, which is used to store the metadata of the cache item (all active keys and cached data-related information), so that nginx can quickly determine whether a request hits or misses the cache, 1m can store 8000 keys, 10m can store 80000 keys, 10m can store 80000 keys.
●inactive: Deletes cache files that have not been accessed within the specified time, default for 10 minutes.
●max_size: The upper limit of cache storage is set, and if not specified, all disk space will be used up at most.
● use_temp_path: Place temporary files directly in the cache directory.


proxy_cachemy_cache;      #指定用于页面缓存的共享内存, the zone name is defined by proxy_cache_path directive
proxy_cache_min_uses1;   ## stipulates the caching threshold, how many times the request is cached, and does not cache low-frequency requests to avoid waste.
proxy_cache_valid200 3m;  #为不同的响应状态码设置不同的缓存时间, this is a request with a cache status code of 200 and a cache duration of 3 minutes
proxy_cache_key$request_uri; #指定缓存文件的key为请求的URI
add_headerNginx-Cache-Status $upstream_cache_status;  #把缓存状态设置为头部信息, respond to the client
proxy_passhttp://webservers;  #设置代理转发的后端服务器的协议和地址

Use an Nginx caching proxy to ignore query strings

(End)




Previous:Use the dotnet-ef tool to generate a rollback script
Next:Use curl to check the HTTP/3 support of the test site
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com