nginx(7):使用nginx的proxy_cache作網站緩存

爲何要作web cache,我想你們最主要的是解決流量的壓力。隨着網站流量的提高,若是隻是單臺機器既處理靜態文件,又處理動態腳本,顯然效率很難上升,不能處理日益上漲的流量壓力。與此同時某些網站的頁面內容並非常常變化,所以咱們能夠分兩層架構來組織網站。前端web緩存+後端web服務器,能夠參看這裏配置nginx反向代理配置javascript

前端web緩存有多重方式實現,原理就是隊請求結果頁面靜態化並設置一個超時期限,緩存頁面過時後,新請求到達時從新到後端web服務器獲取內容更新;沒有nginx前比較流行的方法是squid,但squid不能充分利用處理器的多核特性,愈來愈多的網站選用nginx來作前端的web緩存。php

要想使用nginx的緩存功能要保證nginx添加了proxy模塊。咱們可使用-V選項(大寫的V,小寫的v是看版本號的)來查看nginx的編譯參數。我使用的是默認的參數編譯的,以下所示:css

root@SNDA-172-17-12-117:/usr/local/nginx# ./nginx -V
nginx version: nginx/1.2.3
built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
TLS SNI support enabled
configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.21 --with-zlib=/usr/local/src/zlib-1.2.7html

nginx的全部模塊必須在編譯的時候添加,不能再運行的時候動態加載,默認的編譯選項下包含的模塊,若是你不是顯示的用參數關閉它。前端


proxy模塊中經常使用的指令時proxy_pass和proxy_cache.java

nginx的web緩存功能的主要是由proxy_cache、fastcgi_cache指令集和相關指令集完成,proxy_cache指令負責反向代理緩存後端服務器的靜態內容,fastcgi_cache主要用來處理FastCGI動態進程緩存(這裏我不是很清楚這兩個指令的區別,好像功能上都差很少,尤爲後面這句話的意思,是我翻譯過來的)。nginx

確認proxy模塊安裝好後,下面對nginx的配置文件進行設置,重點部分如標紅字體所示。web

這是個人nginx.conf配置文件。apache

user www-data;
worker_processes 1;ubuntu

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#Compression Settings
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Some version of IE 6 don't handle compression well on some mime-types,
# so just disable for them
gzip_disable "MSIE [1-6].(?!.*SV1)";
# Set a vary header so downstream proxies don't send cached gzipped
# content to IE6
gzip_vary on;
#end gzip

#cache begin
proxy_buffering on;
proxy_cache_valid any 10m;
proxy_cache_path /data/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /data/temp;
proxy_buffer_size 4k;
proxy_buffers 100 8k;
#cache end

## Basic reverse proxy server ##
## Apache (vm02) backend for www.example.com ##
upstream apachephp {
server www.quancha.cn:8080; #Apache1
}

## Start www.quancha.cn ##
server {
listen 80;
server_name *.quancha.cn;

access_log logs/quancha.access.log main;
error_log logs/quancha.error.log;
root html;
index index.html index.htm index.php;

## send request back to apache1 ##
location / {
proxy_pass http://apachephp;
proxy_cache my-cache;
proxy_cache_valid 200;

#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
##End Proxy Settings
}
}
## End www.quancha.cn ##

}

配置文件中以proxy_開頭的指令咱們大均可以字面意思獲得理解。請務必注意一點proxy_cache_path和proxy_temp_path設置的目錄須要在同一分區,由於它們之間是硬連接的關係。

最後啓動nginx,來迎接着激動人心的時刻吧。我已經火燒眉毛了。若是文章哪裏有問題或者你遇到了什麼麻煩,能夠留言讓我知道。

相關文章
相關標籤/搜索