高并發網站架構的核心原則其實就一句話"把所有的使用者通路請求都盡量往前推",即:能緩存在使用者電腦本地的,就不要讓他去通路CDN。 能緩存CDN伺服器上的,就不要讓CDN去通路源(靜态伺服器)了。能通路靜态伺服器的,就不要去通路動态伺服器。以此類推:能不通路資料庫和存儲就一定不要去通路資料庫和存儲。
在主配置檔案(nginx.conf)中添加緩存域
astcgi_cache_path /data/nginx/fcgicache levels=1:2 keys_zone=fcache:10m max_size=2g inactive=1d;
fastcgi_cache_path:緩存檔案的路徑,/dev/shm/為tmfs緩存檔案系統,
實際儲存在記憶體中,是以讀寫IO性能更高。
levels:緩存目錄的結構層次,例如1:2,緩存檔案會就生成在指定目錄的再下兩層目錄中。
keys_zone:緩存域名稱,在vhost内進行緩存時需要調用。
inactive:緩存不活動時間,若緩存内容在指定時間内未被通路将會被清理出緩存域。
2. 站點配置(然後在配置中的server的location中加入)
location /archives/ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /data/webroot/$fastcgi_script_name;
include fastcgi_params;
fastcgi_cache fcache;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_key $request_method$host$request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Cache “$upstream_cache_status”;
}
fastcgi_cache:指定緩存域
fastcgi_cache_methods:指定緩存的請求方式
fastcgi_cache_key:指定緩存檔案的辨別,這個辨別會MD5轉碼存儲在緩存域的目錄下
fastcgi_cache_valid:指定緩存狀态,例如上文中隻緩存響應狀态碼為200和302的請求所産生的傳回頁面10分鐘
fastcgi_ignore_headers:預設情況下fastcgi_cache會忽略有特殊header的請求,并不進行緩存,官網說明。但當我們添加這個參數後,這些限制将不在存在。
add_header 将會在傳回請求的response的header中添加一個X-Cache字段表示是否進行了緩存。如果需要也可以在nginx日志中通過log_format添加$upstream_cache_status字段
·MISS 未命中,請求被傳送到後端
·HIT 緩存命中
·EXPIRED 緩存已經過期請求被傳送到後端
·UPDATING 正在更新緩存,将使用舊的應答
·STALE 後端将得到過期的應答
fastcgi_keep_conn on; 開啟保持連接配接,性能也有一定的加速作用.
使用ab工具壓測網站,1:可以測試再沒有緩存之前的通路速度,2:看看緩存是否生效以及生效後的通路速度是否有提升。
Ab壓測後已經又緩存資料了
Nginx内置FastCgi緩存是不支援自動清除緩存更新緩存,如果是WordPress程式的可以使用Nginx Helper插件,專為wordpress的nginx fastcgi_cache緩存更新而開發的插件。
WordPress程式安裝使用Nginx Helper插件自動清除更新緩存
安裝Nginx ngx_cache_purge子產品
1:安裝Nginx ngx_cache_purge子產品
檢視ngx_cache_purge是否安裝
nginx -V 2>&1 | grep -o ngx_cache_purge
顯示ngx_cache_purge表示已經安裝
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
wget http://nginx.org/download/nginx-1.9.10.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
tar xzf nginx-1.9.10.tar.gz
cd nginx-1.9.10
nginx -V #檢視nginx編譯參數,最後加上--add-module=../ngx_cache_purge-2.3
./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-http_stub_status_module --with-http_v2_module --with-http_ssl_module \
--with-ipv6 --with-http_gzip_static_module --with-http_realip_module \
--with-http_flv_module --with-ld-opt=-ljemalloc \
--add-module=../ngx_cache_purge-2.3
make #編譯
mv /usr/local/nginx/sbin/nginx{,_`date +%F`} #備份nginx
cp objs/nginx /usr/local/nginx/sbin
nginx -V 2>&1 | grep -o ngx_cache_purge
# 顯示ngx_cache_purge表示已經安裝成功
不出意外會出現ngx_cache_purge ,表示已經成功在LNMP環境下添加了ngx_cache_purge元件
2:Nginx配置
建議将fastcgi_cache_path設定tmpfs記憶體中,作業系統不同tmpfs路徑也不同,如下:
CentOS:/dev/shm
Ubuntu和Debian:/run/shm
修改nginx虛拟主機配置檔案/usr/local/nginx/conf/vhost/linuxeye.com.conf:
fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
server {
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/vhost/linuxeye_blog.crt;
ssl_certificate_key /usr/local/nginx/conf/vhost/linuxeye_blog.key;
ssl_ciphers "CHACHA20:GCM:HIGH:!DH:!RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
#ssl_stapling on;
#ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
server_name linuxeye.com;
access_log /home/wwwlogs/blog_nginx.log combined;
index index.html index.htm index.php;
include wordpress.conf;
root /home/wwwroot/blog;
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 60m;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
3:使nginx配置生效
service nginx reload
重新開機系統後shm記憶體中nginx-cache檔案夾會丢失,為了使重新開機生效(自動建立檔案夾),修改/etc/init.d/nginx的make_dirs下一行(大概52行)添加:
[ ! -d '/dev/shm/nginx-cache' ] && { mkdir /dev/shm/nginx-cache; chown -R ${user}.$user /dev/shm/nginx-cache; }