CentOS 5.9 下nginx proxy_cache的使用
时间:2016-05-21 00:53 来源:linux.it.net.cn 作者:IT
CDN这个概念可大可小,大到蓝汛早已经上市,小到我们也可以说我们自建CDN。什么全局负载均衡的概念在这里就不再说了,单独的来说下nginx做cache的那点事吧。
一:使用nginx做cache服务器
1 需求就是缓存android的软件包,后缀名是apk。话不多说,直接上配置,供参考:
a-->nginx.conf
user www www;
worker_processes 8;
error_log /data/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 204800;
events
{
use epoll;
worker_connections 204800;
}
http
{
include mime.types;
#apk 文件类型
#default_type application/vnd.android.package-archive;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
client_body_buffer_size 512k;
#跟后端服务器连接的超时时间_发起握手等候响应超时时间
proxy_connect_timeout 600;
#连接成功后_等候后端服务器响应的时间_其实已经进入后端的排队之中等候处理
proxy_read_timeout 600;
#后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有数据
proxy_send_timeout 600;
#代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可
proxy_buffer_size 16k;
#同上 告诉Nginx保存单个用的几个Buffer最大用多大空间
proxy_buffers 4 64k;
#如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
proxy_busy_buffers_size 128k;
#proxy缓存临时文件的大小
proxy_temp_file_write_size 128k;
gzip on;
gzip_proxied expired no-cache no-store private auth;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 3;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
#log_format access '$remote_addr - $remote_user [$time_local] '
# '"$request" $status $body_bytes_sent '
# '"$http_referer" "$http_user_agent" '
# '$host $request_time $http_x_forwarded_for';
#access_log /data/logs/http.a.log;
#error_log /data/logs/http.e.log;
include vhosts/cache.peiqiang.net.conf;
}
upstream source_site {
server 192.168.1.1:80 weight=7 max_fails=2 fail_timeout=30s;
server 192.168.1.2:80 weight=4 max_fails=2 fail_timeout=30s;
}
b-->cache.peiqiang.net.conf
#用于指定本地目录来缓冲较大的代理请求
proxy_temp_path /data/soft/temp;
#设置web缓存区名为cache_one,内存缓存空间大小为12000M,自动清除超过15天没有被访问过的缓存数据,硬盘缓存空间大小200g
proxy_cache_path /data/soft/cache levels=1:2 keys_zone=cache_one:12000m inactive=15d max_size=200g;
server {
listen 80;
server_name cache.peiqiang.net;
access_log /data/logs/a.log;
error_log /data/logs/e.log notice;
# PHP Scripts is NOT allowed within this site!
location ~* \.(php|php5|jsp|asp|aspx)$ {
deny all;
}
location / {
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
#反向代理,访问后端内容源服务器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://source_site;
}
location ~* .*\.(apk)$ {
error_page 302 404 = @fallback;
#如果后端的服务器返回500、502、503、504执行超时等错误、自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
#使用web缓存区cache_one
proxy_cache cache_one;
#对不同的HTTP状态码缓存设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存,这里根据"域名、URI、参数"组合成key
proxy_cache_key $uri$is_args$args;
#反向代理,访问后端内容源服务器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://source_site;
expires 1d;
}
location @fallback {
rewrite ^ $scheme://apke.peiqiang.net$uri redirect;
expires -1;
}
}
说明:其实按这个配置location /这个匹配是多余的,因为过来一个后缀名为apk的软件包location ~* .*\.(apk)$已经给匹配上了,不会再到location /了,不过由于我们还会缓存些其他后缀名的文件,所以location /就是必须的。
c-->/etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
ulimit -HSn 65535
/usr/local/nginx/sbin/nginx
二:是时候结束了
自言自语:
用nginx做cache不是一个好决定,流量上来后,磁盘IO会是瓶颈,nginx又不支持将cache放到多磁盘里,据说有些公司对nginx二次开发后解决了这个问题,不过并不是每个公司都有这个能力的,后文将会分享下我的解决方法。
参考资料:
nginx 官网: http://nginx.org/cn/
一:使用nginx做cache服务器
1 需求就是缓存android的软件包,后缀名是apk。话不多说,直接上配置,供参考:
a-->nginx.conf
user www www;
worker_processes 8;
error_log /data/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 204800;
events
{
use epoll;
worker_connections 204800;
}
http
{
include mime.types;
#apk 文件类型
#default_type application/vnd.android.package-archive;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
client_body_buffer_size 512k;
#跟后端服务器连接的超时时间_发起握手等候响应超时时间
proxy_connect_timeout 600;
#连接成功后_等候后端服务器响应的时间_其实已经进入后端的排队之中等候处理
proxy_read_timeout 600;
#后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有数据
proxy_send_timeout 600;
#代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可
proxy_buffer_size 16k;
#同上 告诉Nginx保存单个用的几个Buffer最大用多大空间
proxy_buffers 4 64k;
#如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
proxy_busy_buffers_size 128k;
#proxy缓存临时文件的大小
proxy_temp_file_write_size 128k;
gzip on;
gzip_proxied expired no-cache no-store private auth;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 3;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
#log_format access '$remote_addr - $remote_user [$time_local] '
# '"$request" $status $body_bytes_sent '
# '"$http_referer" "$http_user_agent" '
# '$host $request_time $http_x_forwarded_for';
#access_log /data/logs/http.a.log;
#error_log /data/logs/http.e.log;
include vhosts/cache.peiqiang.net.conf;
}
upstream source_site {
server 192.168.1.1:80 weight=7 max_fails=2 fail_timeout=30s;
server 192.168.1.2:80 weight=4 max_fails=2 fail_timeout=30s;
}
b-->cache.peiqiang.net.conf
#用于指定本地目录来缓冲较大的代理请求
proxy_temp_path /data/soft/temp;
#设置web缓存区名为cache_one,内存缓存空间大小为12000M,自动清除超过15天没有被访问过的缓存数据,硬盘缓存空间大小200g
proxy_cache_path /data/soft/cache levels=1:2 keys_zone=cache_one:12000m inactive=15d max_size=200g;
server {
listen 80;
server_name cache.peiqiang.net;
access_log /data/logs/a.log;
error_log /data/logs/e.log notice;
# PHP Scripts is NOT allowed within this site!
location ~* \.(php|php5|jsp|asp|aspx)$ {
deny all;
}
location / {
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
#反向代理,访问后端内容源服务器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://source_site;
}
location ~* .*\.(apk)$ {
error_page 302 404 = @fallback;
#如果后端的服务器返回500、502、503、504执行超时等错误、自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
#使用web缓存区cache_one
proxy_cache cache_one;
#对不同的HTTP状态码缓存设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存,这里根据"域名、URI、参数"组合成key
proxy_cache_key $uri$is_args$args;
#反向代理,访问后端内容源服务器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://source_site;
expires 1d;
}
location @fallback {
rewrite ^ $scheme://apke.peiqiang.net$uri redirect;
expires -1;
}
}
说明:其实按这个配置location /这个匹配是多余的,因为过来一个后缀名为apk的软件包location ~* .*\.(apk)$已经给匹配上了,不会再到location /了,不过由于我们还会缓存些其他后缀名的文件,所以location /就是必须的。
c-->/etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
ulimit -HSn 65535
/usr/local/nginx/sbin/nginx
二:是时候结束了
自言自语:
用nginx做cache不是一个好决定,流量上来后,磁盘IO会是瓶颈,nginx又不支持将cache放到多磁盘里,据说有些公司对nginx二次开发后解决了这个问题,不过并不是每个公司都有这个能力的,后文将会分享下我的解决方法。
参考资料:
nginx 官网: http://nginx.org/cn/
(责任编辑:IT)
CDN这个概念可大可小,大到蓝汛早已经上市,小到我们也可以说我们自建CDN。什么全局负载均衡的概念在这里就不再说了,单独的来说下nginx做cache的那点事吧。 一:使用nginx做cache服务器 1 需求就是缓存android的软件包,后缀名是apk。话不多说,直接上配置,供参考: a-->nginx.conf user www www; worker_processes 8; error_log /data/logs/nginx_error.log crit; pid /usr/local/nginx/nginx.pid; worker_rlimit_nofile 204800; events { use epoll; worker_connections 204800; } http { include mime.types; #apk 文件类型 #default_type application/vnd.android.package-archive; default_type application/octet-stream; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 2k; large_client_header_buffers 4 4k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; open_file_cache max=204800 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s; tcp_nodelay on; client_body_buffer_size 512k; #跟后端服务器连接的超时时间_发起握手等候响应超时时间 proxy_connect_timeout 600; #连接成功后_等候后端服务器响应的时间_其实已经进入后端的排队之中等候处理 proxy_read_timeout 600; #后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有数据 proxy_send_timeout 600; #代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可 proxy_buffer_size 16k; #同上 告诉Nginx保存单个用的几个Buffer最大用多大空间 proxy_buffers 4 64k; #如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2 proxy_busy_buffers_size 128k; #proxy缓存临时文件的大小 proxy_temp_file_write_size 128k; gzip on; gzip_proxied expired no-cache no-store private auth; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml; gzip_disable "MSIE [1-6]\."; gzip_vary on; #log_format access '$remote_addr - $remote_user [$time_local] ' # '"$request" $status $body_bytes_sent ' # '"$http_referer" "$http_user_agent" ' # '$host $request_time $http_x_forwarded_for'; #access_log /data/logs/http.a.log; #error_log /data/logs/http.e.log; include vhosts/cache.peiqiang.net.conf; } upstream source_site { server 192.168.1.1:80 weight=7 max_fails=2 fail_timeout=30s; server 192.168.1.2:80 weight=4 max_fails=2 fail_timeout=30s; } b-->cache.peiqiang.net.conf #用于指定本地目录来缓冲较大的代理请求 proxy_temp_path /data/soft/temp; #设置web缓存区名为cache_one,内存缓存空间大小为12000M,自动清除超过15天没有被访问过的缓存数据,硬盘缓存空间大小200g proxy_cache_path /data/soft/cache levels=1:2 keys_zone=cache_one:12000m inactive=15d max_size=200g; server { listen 80; server_name cache.peiqiang.net; access_log /data/logs/a.log; error_log /data/logs/e.log notice; # PHP Scripts is NOT allowed within this site! location ~* \.(php|php5|jsp|asp|aspx)$ { deny all; } location / { proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_cache cache_one; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; #反向代理,访问后端内容源服务器 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://source_site; } location ~* .*\.(apk)$ { error_page 302 404 = @fallback; #如果后端的服务器返回500、502、503、504执行超时等错误、自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移 proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; #使用web缓存区cache_one proxy_cache cache_one; #对不同的HTTP状态码缓存设置不同的缓存时间 proxy_cache_valid 200 304 12h; #设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存,这里根据"域名、URI、参数"组合成key proxy_cache_key $uri$is_args$args; #反向代理,访问后端内容源服务器 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://source_site; expires 1d; } location @fallback { rewrite ^ $scheme://apke.peiqiang.net$uri redirect; expires -1; } } 说明:其实按这个配置location /这个匹配是多余的,因为过来一个后缀名为apk的软件包location ~* .*\.(apk)$已经给匹配上了,不会再到location /了,不过由于我们还会缓存些其他后缀名的文件,所以location /就是必须的。 c-->/etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local ulimit -HSn 65535 /usr/local/nginx/sbin/nginx 二:是时候结束了 自言自语: 用nginx做cache不是一个好决定,流量上来后,磁盘IO会是瓶颈,nginx又不支持将cache放到多磁盘里,据说有些公司对nginx二次开发后解决了这个问题,不过并不是每个公司都有这个能力的,后文将会分享下我的解决方法。 参考资料: nginx 官网: http://nginx.org/cn/ 一:使用nginx做cache服务器 1 需求就是缓存android的软件包,后缀名是apk。话不多说,直接上配置,供参考: a-->nginx.conf user www www; worker_processes 8; error_log /data/logs/nginx_error.log crit; pid /usr/local/nginx/nginx.pid; worker_rlimit_nofile 204800; events { use epoll; worker_connections 204800; } http { include mime.types; #apk 文件类型 #default_type application/vnd.android.package-archive; default_type application/octet-stream; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 2k; large_client_header_buffers 4 4k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; open_file_cache max=204800 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s; tcp_nodelay on; client_body_buffer_size 512k; #跟后端服务器连接的超时时间_发起握手等候响应超时时间 proxy_connect_timeout 600; #连接成功后_等候后端服务器响应的时间_其实已经进入后端的排队之中等候处理 proxy_read_timeout 600; #后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有数据 proxy_send_timeout 600; #代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可 proxy_buffer_size 16k; #同上 告诉Nginx保存单个用的几个Buffer最大用多大空间 proxy_buffers 4 64k; #如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2 proxy_busy_buffers_size 128k; #proxy缓存临时文件的大小 proxy_temp_file_write_size 128k; gzip on; gzip_proxied expired no-cache no-store private auth; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml; gzip_disable "MSIE [1-6]\."; gzip_vary on; #log_format access '$remote_addr - $remote_user [$time_local] ' # '"$request" $status $body_bytes_sent ' # '"$http_referer" "$http_user_agent" ' # '$host $request_time $http_x_forwarded_for'; #access_log /data/logs/http.a.log; #error_log /data/logs/http.e.log; include vhosts/cache.peiqiang.net.conf; } upstream source_site { server 192.168.1.1:80 weight=7 max_fails=2 fail_timeout=30s; server 192.168.1.2:80 weight=4 max_fails=2 fail_timeout=30s; } b-->cache.peiqiang.net.conf #用于指定本地目录来缓冲较大的代理请求 proxy_temp_path /data/soft/temp; #设置web缓存区名为cache_one,内存缓存空间大小为12000M,自动清除超过15天没有被访问过的缓存数据,硬盘缓存空间大小200g proxy_cache_path /data/soft/cache levels=1:2 keys_zone=cache_one:12000m inactive=15d max_size=200g; server { listen 80; server_name cache.peiqiang.net; access_log /data/logs/a.log; error_log /data/logs/e.log notice; # PHP Scripts is NOT allowed within this site! location ~* \.(php|php5|jsp|asp|aspx)$ { deny all; } location / { proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_cache cache_one; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; #反向代理,访问后端内容源服务器 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://source_site; } location ~* .*\.(apk)$ { error_page 302 404 = @fallback; #如果后端的服务器返回500、502、503、504执行超时等错误、自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移 proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; #使用web缓存区cache_one proxy_cache cache_one; #对不同的HTTP状态码缓存设置不同的缓存时间 proxy_cache_valid 200 304 12h; #设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存,这里根据"域名、URI、参数"组合成key proxy_cache_key $uri$is_args$args; #反向代理,访问后端内容源服务器 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://source_site; expires 1d; } location @fallback { rewrite ^ $scheme://apke.peiqiang.net$uri redirect; expires -1; } } 说明:其实按这个配置location /这个匹配是多余的,因为过来一个后缀名为apk的软件包location ~* .*\.(apk)$已经给匹配上了,不会再到location /了,不过由于我们还会缓存些其他后缀名的文件,所以location /就是必须的。 c-->/etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local ulimit -HSn 65535 /usr/local/nginx/sbin/nginx 二:是时候结束了 自言自语: 用nginx做cache不是一个好决定,流量上来后,磁盘IO会是瓶颈,nginx又不支持将cache放到多磁盘里,据说有些公司对nginx二次开发后解决了这个问题,不过并不是每个公司都有这个能力的,后文将会分享下我的解决方法。 参考资料: nginx 官网: http://nginx.org/cn/ (责任编辑:IT) |