Nginx + PHP Cache detailed

Source: Internet
Author: User
Nginx Cache
Nginx has two kinds of caching mechanisms: Fastcgi_cache and Proxy_cache
Let's talk about the difference between the two caching mechanisms.
The Proxy_cache role is to cache the contents of the backend server, possibly any content, including static and dynamic
The Fastcgi_cache function is to cache fastcgi generated content, in many cases the dynamic content generated by PHP
Proxy_cache cache reduces the number of nginx-to-backend communications, saving time and backend bandwidth
The Fastcgi_cache cache reduces the number of nginx-to-PHP communications and reduces the pressure on PHP and the database.

Proxy_cache Cache Settings

#注: The paths specified by Proxy_temp_path and Proxy_cache_path must be proxy_temp_path/data0/proxy_temp_dir on the same partition; #设置Web缓存区名称为cache_one, The memory cache space size is automatically cleared for content that is not accessed for 200mb,1 days, and the hard disk cache 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, and so on, the request is automatically forwarded to another server in the upstream load balancer pool for failover. Proxy_next_upstream http_502 http_504 error timeout Invalid_header;proxy_cache cache_one; #对不同的HTTP状态码设置不同的缓存时间proxy_ Cache_valid 304 12h; #以域名, URI, parameter combination into the Web cache key value, Nginx based on the key value hash, storage cache content to the level two cache directory Proxy_cache_key $host $uri$is_args$ Args;proxy_set_header Host $host;p roxy_set_header x-forwarded-for $remote _addr;proxy_pass http://backend_server; Expires 1d;} #用于清除缓存, assuming that 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 (/.*) {#设置只允许指定的IP或IP段才可以清除URL缓存. 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 that end in #扩展名以. PHP,. JSP,. CGI are not cached. Location ~. *\. (php|jsp|cgi)? ${proxy_set_header Host $host;p roxy_set_header x-forwarded-for $remote _addr;proxy_pass http://backend _server;} Access_log off;}}


Fastcgi_cache Cache Settings

#定义缓存存放的文件夹fastcgi_cache_path/tt/cache levels=1:2 keys_zone=name:2880m inactive=2d max_size=10g;# Defines the cache for 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 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; #设置缓存的过程中发现无法获取cookie, the 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, Nginx Proxy_cache and Fastcgi_cache cache configuration is similar.
--------------------------------------------------------------------------------
Memcache Cache
Before we discuss the memcache cache, let's look at MySQL's memory cache.
MySQL's memory cache can be specified in MY.CNF size: the memory table and the temporary table are different, the temporary table is also in memory, the temporary table maximum memory needs to be set through tmp_table_size=128m. When the data checked the temporary table maximum setting, automatically to the disk table, at this time because of the need for IO operation, performance will be greatly reduced, and the memory table will not, when the memory is full, will prompt the data full error.
Cases:
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

Features of the memory table:
1. The table definition of the memory table is stored on disk with the extension. frm, so reboots are not lost
2. Memory table data is stored in memory, reboot will lose data
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 are not supported
5. Memory table supports auto_increment columns and indexes on columns that can contain null values
6. Memory table does not support things
7. The memory table is a table lock and performance may degrade when frequently modified

Let's take a look at Memcache, which is relatively limited by MySQL's memory table.
Use of Memcache
1. Improve the concurrency of the system
2. Reducing the burden on the database
Note: The Memcache Linux system 32-bit supports only 4G of memory, while the Memcache maximum storage time 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.