nginx反向代理做cache配置
前序:请耐性子看完,前面的这个配置可以达到按后缀名进行缓存,但无法被purge。后面的配置可以被purge。
具体实施方案按个人情况而定。
需要第三方的ngx_cache_purge模块:
wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
tar zxvf ngx_cache_purge-1.0.tar.gz
nginx机器IP地址:192.168.2.187
编译参数: configure arguments: --add-module=../ngx_cache_purge-1.0 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
user nobody;
worker_processes 1;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request_method $scheme://$host$request_uri $server_protocol" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
server_names_hash_bucket_size 128; #指定服务器名称哈希表的框大小
large_client_header_buffers 4 128k; #以上两个是设定客户端请求的Header头缓冲区大小,对于
cookie内容较大的请求,应增大改值。(400或414错误)
client_max_body_size 8m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 32k; #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存
到本地再传给用户
proxy_connect_timeout 600; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 600; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout 600; #后端服务器数据回传时间(代理发送超时)
proxy_buffer_size 32k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 1024m; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
递请求,而不缓冲到磁盘
proxy_ignore_client_abort on; #不允许代理端主动关闭连接
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_proxied any; 前端是squid的情况下要加此参数,否则squid上不缓存gzip文件
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server_tokens off;
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /cache/proxy_temp_path;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /cache/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
upstream my_server_pool {
server 192.168.11.6:80;
}
server {
listen 80 default;
server_name _;
return 500;
access_log off;
}
server {
listen 80;
server_name testA.domian.com testB.domian.com testC.domian.com testD.domian.com;
access_log logs/access.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
expires 12h;
}
#扩展名以.gif、.jpg、.css等结尾的静态文件缓存。
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服
务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one; #进行缓存,使用Web缓存区cache_one
proxy_cache_valid 200 304 12h; #对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
proxy_cache_key $host$uri$is_args$args; #以域名、URI、参数组合成Web缓存的Key值,Nginx根据
Key值哈希,存储缓存内容到二级缓存目录内
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Accept-Encoding "none"; #设定proxy_set_header Accept-Encoding '';
(或是后台服务器关闭gzip),这样这台机器才
不会缓存被压缩的文件,造成乱码
# proxy_set_header Accept-Encoding ""; 这个也可
proxy_ignore_headers "Cache-Control" "Expires"; #这段配置加上后,proxy_cache就能支持后台设
定的expires。
proxy_pass http://my_server_pool;
expires 1h;
}
location ~ ^/NginxStatus {
stub_status on;
access_log off;
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
}
location ~ ^/(WEB-INF)/ {
deny all;
}
#设置只允许指定的IP或IP段才可以清除URL缓存。
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.0.0/16;
allow all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
测试结果正常,第一次访问时,nginx和后端反向squid都有请求日志。当请求过一次后,nginx的/cache目录下多出缓存文件,并且再次请求页面(请过浏览器缓存),后端squid没有请求日志,说明是nginx提供的cache。
但是以上的配置不能使用purge命令,比如缓存了http://testA.domain.com/css.css使用
http://testA.domain.com/purge/css.css却由后端tomcat返回了404页面。参考了同事的blog,发现自己确实不善于思考了,明明之前有一个清除squid脚本的例子了...
[root@test1 proxy_cache_path]# cat cache_purge.sh
# !/bin/sh
cache_dir=/cache/proxy_cache_path
grep -ra $1 ${cache_dir} | awk -F':' '{print $1}' >/tmp/cache_list.txt
for file in `cat /tmp/cache_list.txt`
do
rm -f ${file}
done
rm -f /tmp/cache_list.txt
这样通过执行./cache_purge.sh css.css 或./cache_purge.sh testA.domain.com/css.css就把缓存清掉了!
[root@test1 proxy_cache_path]# grep -ar "testA.domain.com/images/200807_button_3.gif" */
e/e2/5b7880ae1d30d6d23f0666e0b926ce2e:KEY: testA.domain.com/images/200807_button_3.gif
[root@test1 proxy_cache_path]# ./cache_purge.sh testA.domain.com/images/200807_button_3.gif
[root@test1 proxy_cache_path]# grep -ar "testA.domain.com/images/200807_button_3.gif" */
上述的脚本转自:http://www.wenzizone.cn/?p=330
记录一下:
问题一:开始的实验环境是nginx自己处理静态文件,将动态文件也proxy_pass到本机,及:
upstream tomcat {
ip_hash;
server 192.168.2.187:8080;
}
但是始终都缓存不上。
改成nginx作为负载均衡,反向代理时:
upstream tomcat {
ip_hash;
server 192.168.2.189:8080;
}
发现可以缓存上了。
[root@test1 data0]# ll *
proxy_cache_path:
total 4
drwx------ 3 nobody nobody 4096 Feb 2 14:09 3
是我的操作失误?还是nginx作为web服务器时,不能缓存自己?
问题二:就是上面所说的
比如缓存了http://testA.domain.com/css.css
但是使用http://testA.domain.com/purge/css.css却由后端tomcat返回了404页面。
问题二解决:这段解释来自于:http://raocl.spaces.live.com/blog/cns!3F6CFF93FD0E3B79!825.entry
因为nginx提供的过期控制是针对http_status_code的,我本想通过location中限定类型的方法完成曲线救国,结果发现:一旦location中限定了文件类型,缓存过期的定义就失效!!
#也就是说,限定文件类型后的哈希缓存,是绝绝对对的强制永久缓存——不单过期失效,下面的purge也失效——或许换一个场景,这个刚好有用。
所以换了一个配置:
server {
listen 80;
server_name testA.domian.com testB.domian.com testC.domian.com testD.domian.com;
access_log logs/access.log;
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Accept-Encoding "";
proxy_pass http://my_server_pool;
expires 12h;
}
# location ~ .*\.(html|gif|jpg|jpeg|png|bmp|swf|js|css)$
# {
# proxy_cache cache_one;
# proxy_cache_valid 200 304 12h;
# proxy_cache_valid 301 302 1m;
# proxy_cache_valid any 1m;
# proxy_cache_key $host$uri$is_args$args;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_set_header Accept-Encoding "";
# proxy_pass http://my_server_pool;
# expires 1h;
# }
#这部分定义不缓存而是透传的请求类型。介于无法通过类型来控制缓存,那么这里不缓存的控制就必须确保严格正确了
location ~ .*\.(php|jsp|cgi)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
}
这样一来的意思就是说缓存所有,除了我定义的php,jsp,cgi,当然能不能被缓存还要决定web服务器的header头了,至此。终于见到了久违的页面:
nginx反向代理做cache配置的更多相关文章
- 正向代理 forward proxy、反向代理 reverse proxy、透明代理 transparent proxy nginx反向代理原理和配置讲解 防止外部客户机获取内部内容服务器的重定向 URL 缓存命中
[大型网站技术实践]初级篇:借助Nginx搭建反向代理服务器 - Edison Chou - 博客园http://www.cnblogs.com/edisonchou/p/4126742.html 图 ...
- Nginx反向代理负载均衡配置
1.反向代理概述 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求 ...
- nginx 反向代理做域名转发简单配置
这里用的是nginx for windows 首先进入nginx配置文件,做以下配置: server { listen 80; server_name abc.com; location / { pr ...
- nginx反向代理原理和配置讲解
最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡.所以搜罗了一些关于反向代理服务器的内容,整理综合. 一 概述 反向代理(Reverse Proxy)方式 ...
- Linux下nginx反向代理服务器安装与配置实操
1.我们只要实现访问nginx服务器能跳转到不同的服务器即可,我本地测试是这样的, 在nginx服务器里面搭建了2个tomcat,2个tomcat端口分别是8080和8081,当我输入我nginx服务 ...
- Linux 下 Nginx 反向代理 负载均衡配置
转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52036350 上一篇分享了 Nginx + JDK + Tomcat + MySQ ...
- Centos7.4 Nginx反向代理+负载均衡配置
Ningx是一款高性能的HTTP和反向代理服务器,配置起来也比较简单. 测试环境: 172.16.65.190 Nginx-反向代理 172.16.65.191 Ningx-Web 172.16.65 ...
- 搭建Nginx反向代理做内网域名转发
由于公司内网有多台服务器的 http 服务要映射到公司外网静态 IP,如果用路由的端口映射来做,就只能一台内网服务器的 80 端口映射到外网 80 端口,其他服务器的 80 端口只能映射到外网的非 8 ...
- 配置LANMP环境(7)-- 配置nginx反向代理,与配置apache虚拟主机
一.配置nginx反向代理 1.修改配置文件 vim /etc/nginx/nginx.conf 在35行http下添加一下内容: include /data/nginx/vhosts/*.conf; ...
随机推荐
- 【CodeForces 788B】奇妙的一笔画问题
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61845295 题目大意 给定n个点m条边的无向图 ...
- 动态路由协议(2)--rip
1.设置pc ip 网关 192.168.1.1 192.168.1.254 192.168.4.1 192.168.4.254 2.设置路由器 (1)设置接口ip Router(config-/ R ...
- Jenkins配置Publish Junit test result report(转)
参考这篇文章:http://www.yiibai.com/jenkins/jenkins_unit_testing.html 插件:JUnit Plugin
- MySQL配置参数:wait_timeout
作者:老王 如果你没有修改过MySQL的配置,缺省情况下,wait_timeout 的初始值是. wait_timeout过大有弊端,其体现就是MySQL里大量的SLEEP进程无法及时释放,拖累系统性 ...
- python核心编程学习记录之多线程编程
- [转载]Elasticsearch Java API总汇
from: http://blog.csdn.net/changong28/article/details/38445805#comments 3.1 集群的连接 3.1.1 作为Elasticsea ...
- 18 Tar Command Examples in Linux
FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...
- css 文字超出省略号
white-space:nowrap; overflow:hidden; -o-text-overflow:ellipsis; text-overflow:ellipsis; 语法: text-ove ...
- ubuntu 查看系统服务的列表
service --status-all
- easyui datagrid 批量编辑和提交数据
easyui datagrid 行编辑和提交方,废话就不多说了,直接上代码 <div style="margin: 5px;"> <table id=" ...