In-depth explanation of Nginx + PHP caching_PHP tutorial

WBOY
Release: 2016-07-21 15:00:45
Original
806 people have browsed it

Nginx Cache
nginx has two caching mechanisms: fastcgi_cache and proxy_cache
Let’s talk about the differences between these two caching mechanisms
proxy_cache
The role is to cache the content of the back-end server, which may be any content, including static and dynamic
fastcgi_cacheThe role is to cache the content generated by fastcgi, in many cases generated by PHP Dynamic content
proxy_cacheCache reduces the number of communications between nginx and the backend, saving transmission time and backend bandwidth
fastcgi_cacheCache reduces the number of communications between nginx and php , which further reduces the pressure on PHP and database.
proxy_cacheCache settings

Copy code The code is as follows:

#Note: proxy_temp_path and proxy_cache_path The specified path must be in the same partition
proxy_temp_path /data0/proxy_temp_dir;
#Set the name of the Web cache area to cache_one, the memory cache space size to 200MB, content that has not been accessed in 1 day will be automatically cleared, and the hard disk cache space size 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 errors such as 502, 504, execution timeout, etc. , automatically forward the request to another server in the upstream load balancing pool to achieve failover.
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#Set different cache times for different HTTP status codes
proxy_cache_valid 200 304 12h;
#Combination of domain name, URI, and parameters Into the Key value of the web cache, Nginx hashes the Key value and stores the cache content in the secondary cache 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;
}
# is used to clear the cache, assuming a URL is http://192.168.8.42/test.txt , you can clear the cache of this URL by accessing http://192.168.8.42/purge/test.txt.
location ~ /purge(/.*)
{
# Set only the specified IP or IP range to clear the URL cache.
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
#Extension ends with .php Dynamic applications ending in ., .jsp, .cgi are 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
Copy code The 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;
#Definition Cache different url requests
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;
# During the process of setting up the cache, it was found that the cookie could not be obtained. After checking, it is necessary to define this sentence
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, the cache configurations of nginx’s proxy_cache and fastcgi_cache are similar.
-------------------------------------------------- ----------------------------------
memcache cache
Before discussing the memcache cache, let’s first understand the memory cache of mysql
Mysql’s memory cache can specify the size in my.cnf: Memory tables are different from temporary tables, and so are temporary tables To store in memory, the maximum memory for temporary tables needs to be set through tmp_table_size=128M. When the data checks the maximum value setting of the temporary table, it is automatically converted to a disk table. At this time, due to the need for IO operations, the performance will be greatly reduced, but the memory table will not. When the memory is full, a data full error will be prompted.
Example:
Copy code The code is as follows:

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

Characteristics of memory table:
1. The table definition of the memory table is stored on disk on, the extension is .frm, so it will not be lost when restarting
2. The data of the memory table is stored in the memory, and the data will be lost when restarting
3. The memory table uses a fixed length format
4. The memory table does not support blob or text columns. For example, varchar and text fields will not be supported
5. The memory table supports auto_increment columns and indexes on columns that can contain null values ​​
6. The memory table does not support transactions
7. The memory table is a table lock. When it is modified frequently, the performance may decrease

Let’s take a look at memcache. Relatively speaking, the memory table of mysql has more restrictions.
Purposes of memcache
1. Improve the concurrency capability of the system
2. Reduce the burden on the database
Note: memcache linux system 32-bit only supports 4G memory. The maximum storage time of memcache is 30 days.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328049.htmlTechArticleNginx cache nginx has two caching mechanisms: fastcgi_cache and proxy_cache Let’s talk about the differences between these two caching mechanisms. The function of proxy_cache is to cache the content of the backend server, maybe...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template