Dive into Nginx + PHP cache details

  • 2020-07-21 07:20:11
  • OfStack

Nginx cache
nginx has two caching mechanisms :fastcgi_cache and proxy_cache
Let's talk about the differences between the two caching mechanisms
proxy_cache is used to cache the contents of the backend server, which can be anything, both static and dynamic
fastcgi_cache is used to cache content generated by fastcgi, in many cases dynamic content generated by php
The proxy_cache cache reduces the number of times that nginx communicates with the back end, saving transmission time and back end bandwidth
The fastcgi_cache cache reduces the number of communications between nginx and php, reducing the stress on php and the database.
proxy_cache cache Settings


# Note: proxy_temp_path and proxy_cache_path The specified path must be in the same location 1 partition 
proxy_temp_path /data0/proxy_temp_dir;
# Set up the Web The cache area name is cache_one , the size of memory cache space is 200MB . 1 Contents not accessed in days are automatically cleared, and the size of the hard disk cache is 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 502 , 504 , execution timeout, etc., automatically forward the request to upstream Another in the load balancer pool 1 Table server, to achieve failover. 
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
# For different HTTP The status code sets different caching times 
proxy_cache_valid 200 304 12h;
# In order to domain name, URI And the parameters are combined into Web The cache Key Value, Nginx According to the Key The value hashes, storing the contents of the cache to 2 Level 1 cache in the directory 
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
expires 1d;
}
# To clear the cache, let's say 1 a URL for http://192.168.8.42/test.txt , by visiting http://192.168.8.42/purge/test.txt You can clear that URL The cache. 
location ~ /purge(/.*)
{
# Only specified Settings are allowed IP or IP Segments can only be cleared URL The cache. 
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
# The extension to .php , .jsp , .cgi The ending dynamic application is not cached. 
location ~ .*\.(php|jsp|cgi)?$
{
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

# Define the folder where the cache resides 
fastcgi_cache_path /tt/cache levels=1:2 keys_zone=NAME:2880m inactive=2d max_size=10G;
# Define different caches url request 
fastcgi_cache_key "$scheme$request_method$host$uri$arg_filename$arg_x$arg_y";
server {
listen 8080;
server_name www.example .com;
location / {
root /www;
index index.html index.htm index.php;
}
location ~ (|.php)$ {
root /www;
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;
include fastcgi.conf;
# Unable to get while setting the cache cookie It needs to be defined 
fastcgi_pass_header Set-Cookie;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /httplogs/access.log access;
}

In general, proxy_cache and fastcgi_cache have similar cache configurations.
--------------------------------------------------------------------------------
memcache cache
Before discussing the memcache cache, let's take a look at the memory cache of mysql
The memory cache for mysql can be specified in ES52en.cnf: Memory tables are different from temporary tables. Temporary tables are also stored in memory. The maximum memory for temporary tables needs to be set by tmp_table_size=128M. When the data has checked the maximum setting of the temporary table, it will be automatically converted to the disk table. At this time, due to the need for IO operation, the performance will be greatly reduced, but the memory table will not. When the memory is full, the data full error will be prompted.
Ex. :

create table test
(
id int unsigned not null auto_increment primary key
state char(10),
type char(20),
date char(30)
)engine=memory default charset=utf8

Memory table features:
1. The table definitions for the in-memory tables are stored on disk with the.frm extension, so the restart is not lost
2. The data of memory table is stored in memory, and data will be lost if restarted
3. The memory table USES a fixed length format
4. The memory table does not support blob or text columns, such as varchar and text fields will not be supported
5. The in-memory table supports columns auto_increment and indexes to columns that can contain null values
6. Memory tables do not support things
7. Memory tables are table locks and performance can degrade when changes occur frequently

Looking at memcache, mysql has relatively more limited memory tables.
The purpose of the memcache
1. Improve the concurrency of the system
2. Lighten the burden of database
Note: THE 32-bit memcache linux system only supports 4G memory, and the maximum storage time of memcache is 30 days.


Related articles: