nginx緩衝
nginx有兩種緩衝機制:fastcgi_cache和proxy_cache
下面我們來說說這兩種緩衝機制的區別吧
proxy_cache作用是緩衝後端伺服器的內容,可能是任何內容,包括靜態和動態
fastcgi_cache作用是緩衝fastcgi產生的內容,很多情況是php產生的動態內容
proxy_cache緩衝減少了nginx與後端通訊的次數,節省了傳輸時間和後端頻寬
fastcgi_cache緩衝減少了nginx與php的通訊次數,更減輕了php和資料庫的壓力。
proxy_cache緩衝設定
#註:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區
proxy_temp_path /data0/proxy_temp_dir;
#設定Web緩衝區名稱為cache_one,記憶體緩衝空間大小為200MB,1天沒有被訪問的內容自動清除,硬碟緩衝空間大小為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、執行逾時等錯誤,自動將請求轉寄到upstream負載平衡池中的另一台伺服器,實現容錯移轉。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#對不同的HTTP狀態代碼設定不同的緩衝時間
proxy_cache_valid 200 304 12h;
#以網域名稱、URI、參數組合成Web緩衝的Key值,Nginx根據Key值雜湊,儲存緩衝內容到二級緩衝目錄內
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;
}
#用於清除緩衝,假設一個URL為http://192.168.8.42/test.txt,通過訪問http://192.168.8.42/purge/test.txt就可以清除該URL的緩衝。
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;
}
#副檔名以.php、.jsp、.cgi結尾的Live App程式不緩衝。
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緩衝設定
#定義緩衝存放的檔案夾
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 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;
#設定緩衝的過程中發現無法擷取cookie,經查需要定義這句話
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;
}
總的來說 nginx的proxy_cache和fastcgi_cache的緩衝配置差不多。
--------------------------------------------------------------------------------
memcache緩衝
在討論memcache緩衝之前,我們先瞭解下mysql的記憶體緩衝吧
mysql的記憶體緩衝可以在my.cnf中指定大小:記憶體表和暫存資料表不同,暫存資料表也是存放記憶體中,暫存資料表最大的記憶體需要通過tmp_table_size=128M設定。當資料查過暫存資料表的最大值設定時,自動轉為磁碟表,此時因需要進行IO操作,效能會大大下降,而記憶體表不會,記憶體滿了後,會提示資料滿錯誤。
例:
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
記憶體表的特性:
1.記憶體表的表定義存放在磁碟上,副檔名為.frm,所以重啟不會丟失
2.記憶體表的資料是存放在記憶體中,重啟會遺失資料
3.記憶體表使用一個固定的長度格式
4.記憶體表不支援blob或text列,比如varchar與text欄位就不會被支援
5.記憶體表支援auto_increment列和對可包含null值的列的索引
6.記憶體表不支援事物
7.記憶體表是表鎖,當修改頻繁時,效能可能會下降
下面我們來看看memcache,相對而言mysql的記憶體表限制較多。
memcache的用途
1.提高系統的並發能力
2.減輕資料庫的負擔
註:memcache linux系統32位只支援4G記憶體,同時memcache最長儲存時間為30天。