http://wgkgood.blog.51cto.com/1192594/773278/
前言* Nginx已經具備Squid所擁有的Web緩存加速功能、清除指定URL緩存的功能。而在性能上,Nginx對多核CPU的利用,勝過Squid不少。另外,在反向代理、負載均衡、健康檢查、後端服務器故障轉移、Rewrite重寫、易用性上,Nginx也比Squid強大得多。這使得一臺Nginx可以同時作爲“負載均衡服務器”與“Web緩存服務器”來使用。
一、 安裝nginx和ngx-purge
ulimit -SHn 65535
yum install pcre pcre-devel -y 安裝pcre
wget http://labs.frickle.com/files/ngx_cache_purge-1.4.tar.gz
tar zxvf ngx_cache_purge-1.4.tar.gz
wget http://nginx.org/download/nginx-1.0.11.tar.gz
tar zxvf nginx-1.0.11.tar.gz
cd nginx-1.0.11/
./configure --user=www--group=www--add-module=../ngx_cache_purge-1.4 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
二、 配置nginx.conf文件如下配置文件
user www www;
worker_processes 8;
error_log /data/logs/nginx/error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
proxy_temp_path /data/proxy_temp_dir;
#設置Web緩存區名稱爲cache_one,內存緩存空間大小爲200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小爲30GB。
proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1dmax_size=30g;
upstream backend_server {
server 192.168.5.130:8080 weight=1max_fails=2fail_timeout=30s;
server 192.168.5.131:8080 weight=1max_fails=2fail_timeout=30s;
}
server
{
listen 80;
server_name www.abc.com 192.168.5.133;
index index.html index.htm;
root /data/webapps/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;
}
location ~ /purge(/.*)
{
#設置只允許指定的IP或IP段輸入正確的密碼纔可以清除URL緩存。(Nginx限制目錄訪問,可以用apache
的htpasswd命令生成密鑰文件,如:htpasswd -c /usr/local/nginx/conf/htpasswd adm_cache ,提示輸入密碼即可。)
auth_basic “Please Insert User And Password”;
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
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 /data/logs/nginx/access.log access;
}
}
三、 啓動nginx即可
/usr/local/nginx/sbin/nginx 即可
然後配置好resin端口設置爲8080
如果需要刷新緩存的url地址爲:
http://192.168.5.133/purge/
如下圖: