Nginx + PHP Cache details

Source: Internet
Author: User
Nginx + PHP Cache details Nginx cache
Nginx has two caching mechanisms: fastcgi_cache and proxy_cache.
Let's talk about the differences between the two cache mechanisms.
Proxy_cache is used to cache the content of backend servers. it may be any content, including static and dynamic content.
Fastcgi_cache is used to cache the content generated by fastcgi. in many cases, it is the dynamic content generated by php.
The proxy_cache cache reduces the number of communications between nginx and the backend, saving the transmission time and backend bandwidth.
Fastcgi_cache reduces the number of communications between nginx and php, and reduces the pressure on php and databases.

Proxy_cache cache settings

# Note: the paths specified by proxy_temp_path and proxy_cache_path must be in the same partition: proxy_temp_path/data0/proxy_temp_dir; # set the Web cache area name to cache_one and the memory cache size to 200 MB, the content not accessed within one day is automatically cleared, and the size of the hard disk cache space is 30 GB. Proxy_cache_path/data0/proxy_cache_dir levels = 1:2 keys_zone = cache_one: 200 m 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, and execution timeout, the request is automatically forwarded to another server in the upstream server load balancer pool, implement 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 12 h; # combine domain names, Uris, and parameters into the Web cache Key values. Nginx stores the cached content to the proxy_cache_key $ host $ URI $ is_args $ args in the second-level cache directory based on the hash of the Key values; proxy_set_header Host $ host; proxy_set_header X-Forwarded-For $ remote_addr; proxy_pass http://backend_server ; Expires 1d;} # used to clear the cache. assume that a URL is http://192.168.8.42/test.txt By accessing http://192.168.8.42/purge/test.txt You can clear the cache of the URL. Location ~ /Purge (/. *) {# sets only the specified IP address or IP segment 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. php ,. jsp ,. dynamic Applications ending with 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

# Define the cache folder fastcgi_cache_path/tt/cache levels = keys_zone = NAME: 2880 m inactive = 2d max_size = 10G; # define the request fastcgi_cache_key for caching different URLs "$ 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 48 h; limit 1; fatal error timeout invalid_header http_500; fastcgi_index index. php; fastcgi_param SCRIPT_FILENAME/scripts $ fastcgi_script_name; include fastcgi. conf; # The cookie cannot be obtained when the cache is Set. 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 ;}


In general, the proxy_cache configuration of nginx is similar to that of fastcgi_cache.
--------------------------------------------------------------------------------
Memcache cache
Before discussing the memcache cache, let's take a look at the mysql memory cache.
The memory cache of mysql can be specified in my. cnf: the memory table is different from the temporary table, and the temporary table is also in memory. The maximum memory of the temporary table must be set through tmp_table_size = m. When the maximum value of the temporary table is queried, it is automatically converted to a disk table. in this case, the performance will be greatly reduced due to IO operations, but the memory table will not. when the memory is full, A message indicating full data error is displayed.
Example:
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 memory table is defined to be stored on the disk. the extension is. frm, so it will not be lost after restart.
2. data in the memory table is stored in the memory, and data will be lost after restart.
3. memory tables use a fixed length format
4. the memory table does not support blob or text columns. for example, the varchar and text fields are not supported.
5. the memory table supports the auto_increment column and the index of a column that can contain null values.
6. memory tables do not support transactions
7. the memory table is a table lock. when the modification is frequent, the performance may decrease.

Next, let's take a look at memcache. In contrast, there are many restrictions on mysql memory tables.
Usage of memcache
1. improve system concurrency
2. reduce the burden on the database
Note: The 32-bit memcache linux system only supports 4 GB memory, and the maximum storage time of memcache is 30 days.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.