nginx proxy_cache 做缓存代理及负载均衡

Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。

 

操作步骤:

软件下载: pcre :ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre

ngx_cache_purge : http://labs.frickle.com/files/

nginx:http://nginx.org/

nginx_proxy_cache 服务端软件安装:

[root@localhost src]# unzip pcre-8.32.zip

[root@localhost pcre-8.32]# ./configure

configure: error: in `/usr/src/pcre-8.32':

configure: error: no acceptable C compiler found in $PATH

See `config.log' for more details

[root@localhost pcre-8.32]# yum install gcc-c++ -y

[root@localhost pcre-8.32]# make && make install

-bash: make: command not found

[root@localhost pcre-8.32]# yum install make -y

[root@localhost src]# tar -xvf ngx_cache_purge-2.0.tar.gz

[root@localhost nginx-1.3.14]# ./configure --prefix=/usr/local/nginx --user=www --group=www --add-module=/usr/src/ngx_cache_purge-2.0 --with-http_stub_status_module –with-http_ssl_module

with nginx by using --with-openssl=<path> option.

[root@localhost nginx-1.3.14]# ./configure --prefix=/usr/local/nginx --user=www --group=www --add-module=/usr/src/ngx_cache_purge-2.0 --with-http_stub_status_module –with-http_ssl_module

[root@localhost nginx-1.3.14]# make && make install

[root@localhost conf]# /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

[root@localhost src]# ldd $(which /usr/local/nginx/sbin/nginx)

linux-gate.so.1 => (0x00319000)

libpthread.so.0 => /lib/libpthread.so.0 (0x008fa000)

libcrypt.so.1 => /lib/libcrypt.so.1 (0x00110000)

libpcre.so.1 => not found

libssl.so.10 => /usr/lib/libssl.so.10 (0x00367000)

libcrypto.so.10 => /usr/lib/libcrypto.so.10 (0x0091f000)

libdl.so.2 => /lib/libdl.so.2 (0x00e2b000)

libz.so.1 => /lib/libz.so.1 (0x003c4000)

libc.so.6 => /lib/libc.so.6 (0x00140000)

/lib/ld-linux.so.2 (0x0048b000)

libfreebl3.so => /lib/libfreebl3.so (0x00af0000)

libgssapi_krb5.so.2 => /lib/libgssapi_krb5.so.2 (0x00ead000)

libkrb5.so.3 => /lib/libkrb5.so.3 (0x004ab000)

libcom_err.so.2 => /lib/libcom_err.so.2 (0x002d6000)

libk5crypto.so.3 => /lib/libk5crypto.so.3 (0x00cfa000)

libresolv.so.2 => /lib/libresolv.so.2 (0x002db000)

libkrb5support.so.0 => /lib/libkrb5support.so.0 (0x00cb0000)

libkeyutils.so.1 => /lib/libkeyutils.so.1 (0x007c7000)

libselinux.so.1 => /lib/libselinux.so.1 (0x002f5000)

 

[root@localhost lib]# find / -name libpcre.so

/usr/src/pcre-8.32/.libs/libpcre.so

/usr/local/lib/libpcre.so

[root@localhost lib]# ln -s /usr/local/lib/libpcre.so /lib/libpcre.so.1

架构图:

##图解释:nginx_proxy_cache主要负责负载均衡和缓存

nginx_proxy_cache

##图二解释:前端作用:1、缓存服务器;2、web+nginx_proxy_cache 服务器中web程序访问出现502,504自动调用后端服务器,达到故障转移,如果想更完美,最好在前端再加上lvs,可以防止单点故障

 

nginx_proxy_cache_2

分别写出下面的nginx配置文件:(需要注意的是,图二配置文件红色部分)

 

图一配置文件

 

 

user www www;
worker_processes 8;
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit;
pid /usr/local/webserver/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和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /data0/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
upstream backend_server {
server 192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.8.44:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;
}

server
{
listen 80;
server_name 192.168.8.42;
index index.html index.htm;
location /
{
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;
}

#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
Allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}

#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}

access_log off;
}
}

图二配置文件

 

user www www;
worker_processes 8;
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit;
pid /usr/local/webserver/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和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /data0/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
upstream backend_server {
server 192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.8.44:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;
}

server
{
listen 80;
server_name www.yourdomain.com 192.168.8.42;
index index.html index.htm;
root /data0/htdocs/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;
}

#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
Allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}

#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}

access_log off;
}
}

后端的的服务器搭建使用使用lnmp一键搭建或者手动编译都行

该文档中间的配置文件使用的是张宴的配置文档稍微修改而成

张宴的博客:http://blog.s135.com/nginx_cache/

本文出自 “袁跃平” 博客,请务必保留此出处http://yyping2008.blog.51cto.com/6615447/1150289

转载于:https://my.oschina.net/yyping/blog/112565

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值