Nginx+proxy_cache高速缓存配置
原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://wgkgood.blog.iyunv.com/1192594/773278前言* Nginx已经具备Squid所拥有的Web缓存加速功能、清除指定URL缓存的功能。而在性能上,Nginx对多核CPU的利用,胜过Squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite重写、易用性上,Nginx也比Squid强大得多。这使得一台Nginx可以同时作为“负载均衡服务器”与“Web缓存服务器”来使用。
一、 安装nginx和ngx-purge
[*]ulimit-SHn65535
[*]yuminstallpcrepcre-devel-y安装pcre
[*]
[*]wgethttp://labs.frickle.com/files/ngx_cache_purge-1.4.tar.gz
[*]tarzxvfngx_cache_purge-1.4.tar.gz
[*]wgethttp://nginx.org/download/nginx-1.0.11.tar.gz
[*]tarzxvfnginx-1.0.11.tar.gz
[*]cdnginx-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&&makeinstall
[*]cd../
二、 配置nginx.conf文件如下配置文件
[*]userwwwwww;
[*]
[*]worker_processes8;
[*]
[*]error_log/data/logs/nginx/error.logcrit;
[*]
[*]pid/usr/local/nginx/nginx.pid;
[*]
[*]#Specifiesthevalueformaximumfiledescriptorsthatcanbeopenedbythisprocess.
[*]worker_rlimit_nofile65535;
[*]
[*]events
[*]{
[*]useepoll;
[*]worker_connections65535;
[*]}
[*]
[*]http
[*]{
[*]includemime.types;
[*]default_typeapplication/octet-stream;
[*]
[*]charsetutf-8;
[*]
[*]server_names_hash_bucket_size128;
[*]client_header_buffer_size32k;
[*]large_client_header_buffers432k;
[*]client_max_body_size300m;
[*]
[*]sendfileon;
[*]tcp_nopushon;
[*]
[*]keepalive_timeout60;
[*]
[*]tcp_nodelayon;
[*]
[*]client_body_buffer_size512k;
[*]proxy_connect_timeout5;
[*]proxy_read_timeout60;
[*]proxy_send_timeout5;
[*]proxy_buffer_size16k;
[*]proxy_buffers464k;
[*]proxy_busy_buffers_size128k;
[*]proxy_temp_file_write_size128k;
[*]
[*]gzipon;
[*]gzip_min_length1k;
[*]gzip_buffers416k;
[*]gzip_http_version1.1;
[*]gzip_comp_level2;
[*]gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
[*]gzip_varyon;
[*]
[*]proxy_temp_path/data/proxy_temp_dir;
[*]#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
[*]
proxy_cache_path/data/proxy_cache_dirlevels=1:2keys_zone=cache_one:200minactive=1dmax_size=30g;
[*]
[*]upstreambackend_server{
[*]
server192.168.5.130:8080weight=1max_fails=2fail_timeout=30s;
[*]
server192.168.5.131:8080weight=1max_fails=2fail_timeout=30s;
[*]}
[*]
[*]server
[*]{
[*]listen80;
[*]server_namewww.abc.com192.168.5.133;
[*]indexindex.htmlindex.htm;
[*]root/data/webapps/www;
[*]
[*]location/
[*]{
[*]#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
[*]proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
[*]proxy_cachecache_one;
[*]#对不同的HTTP状态码设置不同的缓存时间
[*]proxy_cache_valid20030412h;
[*]#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
[*]proxy_cache_key$host$uri$is_args$args;
[*]proxy_set_headerHost$host;
[*]proxy_set_headerX-Forwarded-For$remote_addr;
[*]proxy_passhttp://backend_server;
[*]expires1d;
[*]}
[*]
[*]location~/purge(/.*)
[*]{
[*]#设置只允许指定的IP或IP段输入正确的密码才可以清除URL缓存。
[*]auth_basic“PleaseInsertUserAndPassword”;
[*]auth_basic_user_file/usr/local/nginx/conf/htpasswd;
[*]allow127.0.0.1;
[*]allow192.168.1.0/24;
[*]denyall;
[*]proxy_cache_purgecache_one$host$1$is_args$args;
[*]}
[*]
[*]location~.*\.(php|jsp|cgi)?$
[*]{
[*]proxy_set_headerHost$host;
[*]proxy_set_headerX-Forwarded-For$remote_addr;
[*]proxy_passhttp://backend_server;
[*]}
[*]log_formataccess‘$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.logaccess;
[*]}
[*]}
三、 启动nginx即可
[*]/usr/local/nginx/sbin/nginx即可
[*]然后配置好resin端口设置为8080
[*]如果需要刷新缓存的url地址为:
[*]http://192.168.5.133/purge/
如下图:
本文出自 “烟雨楼台” 博客,请务必保留此出处http://wgkgood.blog.iyunv.com/1192594/773278
页:
[1]