Deep Nginx + PHP cache detailed _php Tutorial

Source: Internet
Author: User
Tags table definition
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.
Proxy_cache
The role is to cache the contents of the backend server, which may be anything, including static and dynamic
Fastcgi_cacheThe role is to cache fastcgi generated content, in many cases, PHP generated dynamic content
Proxy_cacheCache reduces the number of nginx and backend traffic, saving time and back-end bandwidth
Fastcgi_cacheThe cache reduces the number of nginx and PHP communication, and reduces the pressure on PHP and the database.
Proxy_cacheCache Settings
Copy CodeThe code is as follows:
#注: The path specified by Proxy_temp_path and Proxy_cache_path must be in the same partition
Proxy_temp_path/data0/proxy_temp_dir;
#设置Web缓存区名称为cache_one, the size of the memory cache is automatically cleared for content that is not accessed for 200mb,1 days, and the 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/
{
#如果后端的服务器返回502, 504, perform timeouts and other errors, and automatically forward the request 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, parameters are combined into the Web cache key value, Nginx based on the key value hash, storage cache content in the level two 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;
}
#用于清除缓存, 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;
Proxy_set_header x-forwarded-for $remote _addr;
Proxy_pass Http://backend_server;
}
Access_log off;
}
}

Fastcgi_cache Cache Settings
Copy CodeThe code is as follows:
#定义缓存存放的文件夹
Fastcgi_cache_path/tt/cache levels=1:2 keys_zone=name:2880m inactive=2d max_size=10g;
#定义缓存不同的url请求
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 sentence needs to be defined by the search
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.
The MySQL memory cache can specify the size in my.cnf:The memory table and the temporary table are different, the temporary table is also in memory, the maximum memory of the temporary table needs to be set by 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:
Copy CodeThe 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

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.

http://www.bkjia.com/PHPjc/328049.html www.bkjia.com true http://www.bkjia.com/PHPjc/328049.html techarticle Nginx Cache Nginx has two kinds of caching mechanism: Fastcgi_cache and Proxy_cache below we say the difference between the two caching mechanisms Proxy_cache role is to cache the contents of the backend server, possibly ...

  • Related Article

    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.