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

Detailed analysis of Nginx+PHP cache

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the relevant knowledge of "detailed analysis of Nginx+PHP cache". In the operation of actual cases, many people will encounter such a dilemma, 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!

The following is a detailed analysis and introduction of PHP cache in Nginx. Friends who need it can refer to it.

Nginx caching

Nginx has two caching mechanisms: fastcgi_cache and proxy_cache

Let's talk about the difference between these two caching mechanisms.

The role of proxy_cache is to cache the content of the back-end server, which can be anything, including static and dynamic

The role of fastcgi_cache is to cache content generated by fastcgi, and in many cases dynamic content generated by php

Proxy_cache cache reduces the number of times nginx communicates with the backend, saving transmission time and backend bandwidth.

Fastcgi_cache caching reduces the number of communications between nginx and php, and reduces the pressure on php and databases.

Proxy_cache cache settings

The copy code is as follows:

# Note: the path specified by proxy_temp_path and proxy_cache_path must be in the same partition

Proxy_temp_path / data0/proxy_temp_dir

# set the name of Web cache to cache_one, the size of memory cache to 200MB, and the size of hard disk cache to 30GB.

Proxy_cache_path / data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g

Server

{

Listen 80

Server_name www.yourdomain.com 192.168.8.42

Index index.html index.htm

Root / data0/htdocs/www

Location /

{

# if the backend server returns errors such as 502,504 or execution timeout, the request is automatically forwarded to another server in the upstream load balancer pool to achieve failover.

Proxy_next_upstream http_502 http_504 error timeout invalid_header

Proxy_cache cache_one

# set different caching time for different HTTP status codes

Proxy_cache_valid 200 304 12h

# the domain name, URI and parameters are combined to form the key value of the Web cache. According to the key value, the Nginx stores the cache content in the second-level cache directory.

Proxy_cache_key hosthosthosturiisargsis_argsisargsargs

Proxy_set_header Host $host

Proxy_set_header X-Forwarded-For $remote_addr

Proxy_pass http://backend_server;

Expires 1d

}

# is used to clear the cache. Suppose a URL is http://192.168.8.42/test.txt. You can clear the cache of the URL by accessing http://192.168.8.42/purge/test.txt.

Location ~ / purge (/. *)

{

# setting allows only the specified IP or IP segments to clear the URL cache.

Allow 127.0.0.1

Allow 192.168.0.0/16

Deny all

Proxy_cache_purge cache_one $host111is_argsKaTeX parse error: Expected 'EOF', got'}'at position 7: args;} has the extension .php, .jsp...

{

Proxy_set_header Host $host

Proxy_set_header X-Forwarded-For $remote_addr

Proxy_pass http://backend_server;

}

Access_log off

}

}

Fastcgi_cache cache settings

The copy code is as follows:

# define the folder where the cache is stored

Fastcgi_cache_path / tt/cache levels=1:2 keys_zone=NAME:2880m inactive=2d max_size=10G

# define caching different url requests

Fastcgi_cache_key "schemeschemeschemerequest_methodhosthosthosturiargfilenamearg_filenameargfilenamearg_xKaTeX parse error: Expected'}', got 'EOF' at end of input:... ation ~ (| .php) {

Root / www; http://www.iis7.com/a/lm/ftp/fastcgi_pass 127.0.0.1:9000

Fastcgi_cache NAME

Fastcgi_cache_valid 200 48h

Fastcgi_cache_min_uses 1

Fastcgi_cache_use_stale error timeout invalid_header http_500

Fastcgi_index index.php

Fastcgi_param SCRIPT_FILENAME / scripts$fastcgi_script_name

This is the end of the detailed analysis of the cache of Nginx+PHP. 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

Servers

Wechat

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

12
Report