Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Nginx proxy caching mechanism

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what is the Nginx proxy caching mechanism". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Introduction to Nginx caching

The http_proxy module of nginx can implement the caching function similar to Squid.

Nginx establishes a local copy of the content that the customer has visited on the Nginx server, so that if the data is accessed again within a period of time, there is no need to send a request to the back-end server through the Nginx server, so it can reduce the network traffic between the Nginx server and the back-end server, reduce network congestion, reduce data transmission delay and improve user access speed.

At the same time, when the back-end server is down, the replica resources on the Nginx server can also respond to relevant user requests, which can improve the robustness (robustness) of the back-end server.

2. Nginx cache 1. Where do I put the cache files?

Proxy_cache_path:Nginx uses this parameter to specify the cache location.

Proxy_cache: this parameter is the previously specified cache name.

Proxy_cache_path: there are two required parameters

The first parameter, the weight cache directory.

The second parameter, keys_zone, specifies the cache name and the size of the memory footprint.

User www-data;worker_processes auto;pid / run/nginx.pid;http {proxy_cache_path / data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com {server 127.0.0.1 server 8881; server 127.0.0.1 data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com 8882; server 127.0.0.1 data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com 8883;} server {listen 80; proxy_cache one Server_name zp.purple.com; location / {proxy_pass http://zp.purple.com; proxy_set_header Host $host; proxy_set_header X-Real_IP $remote_addr;}

Note: 10m in the example is a limit on the size of cache metadata information in memory. If you want to limit the total cache size, you need to use the max_size parameter.

two。 How do I specify which requests are cached?

Nginx caches the request results of all get and head methods by default, and the cached key uses the request string by default.

Custom key: for example, proxy_cache_key "$host$request_uri$cookie_user"

Specifying at least how many times a request has been sent before caching can prevent low-frequency requests from being cached.

For example, proxy_cache_min_user 5

Specify which method requests are cached

For example, proxy_cache_methods GET HEAD POST

User www-data;worker_processes auto;pid / run/nginx.pid;http {proxy_cache_path / data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com {server 127.0.0.1 server 8881; server 127.0.0.1 data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com 8882; server 127.0.0.1 data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com 8883;} server {listen 80; proxy_cache one Server_name zp.purple.com; location / {proxy_pass http://zp.purple.com; proxy_set_header Host $host; proxy_set_header X-Real_IP $remote_addr; proxy_cache_key $host$request_uri$cookie_user;}} 3. Cache validity period

By default, cached content is persisted for a long time unless the total amount of caching exceeds the limit. You can specify how long the cache is valid, for example:

Proxy_cache_valid 200302 10m * valid any 5m

Valid for 5 minutes for any status code.

When the response status code is 200302, it is valid for 10 minutes.

User www-data;worker_processes auto;pid / run/nginx.pid;http {proxy_cache_path / data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com {server 127.0.0.1 server 8881; server 127.0.0.1 data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com 8882; server 127.0.0.1 data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com 8883;} server {listen 80; proxy_cache one Server_name zp.purple.com; location / {proxy_pass http://zp.purple.com; proxy_set_header Host $host; proxy_set_header X-Real_IP $remote_addr; proxy_cache_valid 200 302 10m;}} 4. For some requests, is it possible not to cache?

Proxy_cache_bypass: this instruction response comes from the original server rather than the cache.

For example, proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment

If the value of any parameter is not empty, or if it is not equal to 0Maginnginx, the cache will not be looked up and will be forwarded directly by proxy.

User www-data;worker_processes auto;pid / run/nginx.pid;http {proxy_cache_path / data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com {server 127.0.0.1 server 8881; server 127.0.0.1 data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com 8882; server 127.0.0.1 data/nginx/cache keys_zone=one:10m max_size=10g; upstream zp.purple.com 8883;} server {listen 80; proxy_cache one Server_name zp.purple.com; location / {proxy_pass http://zp.purple.com; proxy_set_header Host $host; proxy_set_header X-Real_IP $remote_addr; proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;}} 3. Web caching

The caching of web pages is controlled by the "Cache-control" in the HTTP message header. The common values are private, no-cache, max-age, must-revalidate and so on. The default is private. Its function can be divided into the following situations according to different ways of re-browsing.

Cache-durective states that all public content will be cached (both client and proxy server can be cached) private content is cached only in private cache (client only, proxy server cannot) no-cache must confirm with the server whether the response returned has been changed before it can be used to satisfy subsequent requests for the same URL. Therefore, if there is an appropriate verification token (ETag), no-cache initiates round-trip communication to verify the cached response, and downloads can be avoided if the resource has not been modified. All the contents of no-store are not cached in the cache or Internet temporary files. Must-revalidation/proxy-revalidation if the cached content is invalid, the request must be sent to the server / agent to reverify the max-age=xxx (xxx is numeric) cached content will expire after xxx seconds, this option is only available in HTTP1.1, and if used with Last-Modified, the higher priority "what is the Nginx proxy caching mechanism" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report