In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand Nginx + PHP caching". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand Nginx + PHP cache.
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 $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. 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 $host$1 $is_args$args
}
Dynamic applications with extensions ending in .php, .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
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 "$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
# it was found that the cookie could not be obtained while setting the cache. After checking, you need 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
}
Overall, nginx's proxy_cache and fastcgi_cache cache configurations are similar.
Memcache caching
Before we talk about memcache caching, let's take a look at mysql's in-memory cache.
The memory cache of mysql can be specified in my.cnf: unlike temporary tables, temporary tables are also stored in memory, and the maximum memory of temporary tables needs to be set through tmp_table_size=128M. When the data is checked for the maximum value of the temporary table, it is automatically converted to a disk table. Due to the need for IO operation, the performance will be greatly degraded, but the memory table will not. When the memory is full, it will prompt the data to be full of errors.
Example:
The copy 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 tables:
1. The table definition of the memory table is stored on disk with the extension .frm, so the restart will not be lost.
two。 The data of the memory table is stored in memory, and reboot will lose the data.
3. Memory tables use a fixed length format
4. Memory tables do not support blob or text columns, such as varchar and text fields will not be supported
5. Memory tables support auto_ increment columns and indexes on columns that can contain null values
6. Memory tables do not support things
7. Memory tables are table locks, and performance may degrade when frequent modifications are made
Let's take a look at memcache. Relatively speaking, mysql has more restrictions on memory tables.
The purpose of memcache
1. Improve the concurrency ability of the system
two。 Lighten the burden on the database
Note: the 32-bit memcache linux system only supports 4G memory, and the maximum storage time of memcache is 30 days.
At this point, I believe you have a deeper understanding of "how to understand Nginx + PHP cache". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.