|
nginx緩存
nginx有兩種緩存機(jī)制:fastcgi_cache和proxy_cache
下面我們來(lái)說(shuō)說(shuō)這兩種緩存機(jī)制的區(qū)別吧
proxy_cache作用是緩存后端服務(wù)器的內(nèi)容,可能是任何內(nèi)容,包括靜態(tài)的和動(dòng)態(tài)的
fastcgi_cache作用是緩存fastcgi生成的內(nèi)容,很多情況是php生成的動(dòng)態(tài)內(nèi)容
proxy_cache緩存減少了nginx與后端通信的次數(shù),節(jié)省了傳輸時(shí)間和后端帶寬
fastcgi_cache緩存減少了nginx與php的通信次數(shù),更減輕了php和數(shù)據(jù)庫(kù)的壓力。
proxy_cache緩存設(shè)置
#注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區(qū)
proxy_temp_path /data0/proxy_temp_dir;
#設(shè)置Web緩存區(qū)名稱為cache_one,內(nèi)存緩存空間大小為200MB,1天沒有被訪問的內(nèi)容自動(dòng)清除,硬盤緩存空間大小為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 /
{
#如果后端的服務(wù)器返回502、504、執(zhí)行超時(shí)等錯(cuò)誤,自動(dòng)將請(qǐng)求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺(tái)服務(wù)器,實(shí)現(xiàn)故障轉(zhuǎn)移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#對(duì)不同的HTTP狀態(tài)碼設(shè)置不同的緩存時(shí)間
proxy_cache_valid 200 304 12h;
#以域名、URI、參數(shù)組合成Web緩存的Key值,Nginx根據(jù)Key值哈希,存儲(chǔ)緩存內(nèi)容到二級(jí)緩存目錄內(nèi)
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;
}
#用于清除緩存,假設(shè)一個(gè)URL為http://192.168.8.42/test.txt,通過(guò)訪問http://192.168.8.42/purge/test.txt就可以清除該URL的緩存。
location ~ /purge(/.*)
{
#設(shè)置只允許指定的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;
}
#擴(kuò)展名以.php、.jsp、.cgi結(jié)尾的動(dòng)態(tài)應(yīng)用程序不緩存。
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緩存設(shè)置
#定義緩存存放的文件夾
fastcgi_cache_path /tt/cache levels=1:2 keys_zone=NAME:2880m inactive=2d max_size=10G;
#定義緩存不同的url請(qǐng)求
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;
#設(shè)置緩存的過(guò)程中發(fā)現(xiàn)無(wú)法獲取cookie,經(jīng)查需要定義這句話
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;
}
總的來(lái)說(shuō) nginx的proxy_cache和fastcgi_cache的緩存配置差不多。
--------------------------------------------------------------------------------
memcache緩存
在討論memcache緩存之前,我們先了解下mysql的內(nèi)存緩存吧
mysql的內(nèi)存緩存可以在my.cnf中指定大小:內(nèi)存表和臨時(shí)表不同,臨時(shí)表也是存放內(nèi)存中,臨時(shí)表最大的內(nèi)存需要通過(guò)tmp_table_size=128M設(shè)定。當(dāng)數(shù)據(jù)查過(guò)臨時(shí)表的最大值設(shè)定時(shí),自動(dòng)轉(zhuǎn)為磁盤表,此時(shí)因需要進(jìn)行IO操作,性能會(huì)大大下降,而內(nèi)存表不會(huì),內(nèi)存滿了后,會(huì)提示數(shù)據(jù)滿錯(cuò)誤。
例:
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
內(nèi)存表的特性:
1.內(nèi)存表的表定義存放在磁盤上,擴(kuò)展名為.frm,所以重啟不會(huì)丟失
2.內(nèi)存表的數(shù)據(jù)是存放在內(nèi)存中,重啟會(huì)丟失數(shù)據(jù)
3.內(nèi)存表使用一個(gè)固定的長(zhǎng)度格式
4.內(nèi)存表不支持blob或text列,比如varchar與text字段就不會(huì)被支持
5.內(nèi)存表支持auto_increment列和對(duì)可包含null值的列的索引
6.內(nèi)存表不支持事物
7.內(nèi)存表是表鎖,當(dāng)修改頻繁時(shí),性能可能會(huì)下降
下面我們來(lái)看看memcache,相對(duì)而言mysql的內(nèi)存表限制較多。
memcache的用途
1.提高系統(tǒng)的并發(fā)能力
2.減輕數(shù)據(jù)庫(kù)的負(fù)擔(dān)
注:memcache linux系統(tǒng)32位只支持4G內(nèi)存,同時(shí)memcache最長(zhǎng)保存時(shí)間為30天。
php技術(shù):基于php緩存的詳解,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。