最新消息:

Nginx+resin+proxy_cache高速缓存平台搭建

Linux知识总结 admin 2760浏览 0评论

前言* 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.10.tar.gz
tar zxvf nginx-1.0.10.tar.gz
cd nginx-1.0.10/
./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=1d max_size=30g;

upstream backend_server {
server 192.168.5.130:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.5.131:8080 weight=1 max_fails=2 fail_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缓存。
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/

如下图:
http://img1.51cto.com/group_attachment/201112131323751485261.png

转载请注明:爱开源 » Nginx+resin+proxy_cache高速缓存平台搭建

您必须 登录 才能发表评论!