nginx利用proxy_cache来缓存文件
为什么要做web cache,我想大家最主要的是解决流量的压力。随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力。与此同时某些网站的页面内容并不是经常变化,因此我们可以分两层架构来组织网站。前端web缓存+后端web服务器。
前端web缓存有多重方式实现,原理就是队请求结果页面静态化并设置一个超时期限,缓存页面过期后,新请求到达时重新到后端web服务器获取内容更新;没有nginx前比较流行的方法是squid,但squid不能充分利用处理器的多核特性,越来越多的网站选用nginx来做前端的web缓存。
要想使用nginx的缓存功能要保证nginx添加了proxy模块。我们可以使用-V选项(大写的V,小写的v是看版本号的)来查看nginx的编译参数。我使用的是默认的参数编译的,如下所示:
root@SNDA----:/usr/local/nginx# ./nginx -V
nginx version: nginx/1.2.
built by gcc 4.4. (Ubuntu 4.4.-4ubuntu5.)
TLS SNI support enabled
configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.21 --with-zlib=/usr/local/src/zlib-1.2.
nginx的所有模块必须在编译的时候添加,不能再运行的时候动态加载,默认的编译选项下包含的模块,如果你不是显示的用参数关闭它。
nginx默认安装的模块如下
模块名称 | 描述 | 版本 | 如何禁用 |
---|---|---|---|
Core | Control ports, locations, error pages, aliases, and other essentials. | --without-http | |
Access | Allow/deny based on IP address. | --without-http_access_module | |
Auth Basic | Basic HTTP authentication. | --without-http_auth_basic_module | |
Auto Index | Generates automatic directory listings. | --without-http_autoindex_module | |
Browser | Interpret "User-Agent" string. | 0.4.3 | --without-http_browser_module |
Charset | Recode web pages. | --without-http_charset_module | |
Empty GIF | Serve a 1x1 image from memory. | 0.3.10 | --without-http_empty_gif_module |
FastCGI | FastCGI Support. | --without-http_fastcgi_module | |
Geo | Set config variables using key/value pairs of IP addresses. | 0.1.17 | --without-http_geo_module |
Gzip | Gzip responses. | --without-http_gzip_module | |
Headers | Set arbitrary HTTP response headers. | ||
Index | Controls which files are to be used as index. | ||
Limit Requests | Limit frequency of connections from a client. | 0.7.20 | --without-http_limit_req_module |
Limit Zone | Limit simultaneous connections from a client. Deprecated in 1.1.8, use Limit Conn Instead. | 0.5.6 | --without-http_limit_zone_module |
Limit Conn | Limit concurrent connections based on a variable. | --without-http_limit_conn_module | |
Log | Customize access logs. | ||
Map | Set config variables using arbitrary key/value pairs. | 0.3.16 | --without-http_map_module |
Memcached | Memcached support. | --without-http_memcached_module | |
Proxy | Proxy to upstream servers. | --without-http_proxy_module | |
Referer | Filter requests based on Referer header. |
--without-http_referer_module | |
Rewrite | Request rewriting using regular expressions. | --without-http_rewrite_module | |
SCGI | SCGI protocol support. | 0.8.42 | --without-http_scgi_module |
Split Clients | Splits clients based on some conditions | 0.8.37 | --without-http_split_clients_module |
SSI | Server-side includes. | --without-http_ssi_module | |
Upstream | For load-balancing. | --without-http_upstream_ip_hash_module (ip_hash directive only) | |
User ID | Issue identifying cookies. | --without-http_userid_module | |
uWSGI | uWSGI protocol support. | 0.8.40 | --without-http_uwsgi_module |
X-Accel | X-Sendfile-like module. |
proxy模块中常用的指令时proxy_pass和proxy_cache.
nginx的web缓存功能的主要是由proxy_cache、fastcgi_cache指令集和相关指令集完成,proxy_cache指令负责反向代理缓存后端服务器的静态内容,fastcgi_cache主要用来处理FastCGI动态进程缓存(这里我不是很清楚这两个指令的区别,好像功能上都差不多,尤其后面这句话的意思,是我翻译过来的)。
确认proxy模块安装好后,下面对nginx的配置文件进行设置,重点部分如标红字体所示。
这是我的nginx.conf配置文件。
user www-data;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; #Compression Settings
gzip on;
gzip_http_version 1.0;
gzip_comp_level ;
gzip_proxied any;
gzip_min_length ;
gzip_buffers 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Some version of IE don't handle compression well on some mime-types,
# so just disable for them
gzip_disable "MSIE [1-6].(?!.*SV1)";
# Set a vary header so downstream proxies don't send cached gzipped
# content to IE6
gzip_vary on;
#end gzip #cache begin
proxy_buffering on;
proxy_cache_valid any 10m;
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /data/temp;
proxy_buffer_size 4k;
proxy_buffers 100 8k;
#cache end ## Basic reverse proxy server ##
## Apache (vm02) backend for www.example.com ##
upstream apachephp {
server www.quancha.cn:; #Apache1
} ## Start www.quancha.cn ##
server {
listen ;
server_name *.quancha.cn; access_log logs/quancha.access.log main;
error_log logs/quancha.error.log;
root html;
index index.html index.htm index.php; ## send request back to apache1 ##
location / {
proxy_pass http://apachephp;
proxy_cache my-cache;
proxy_cache_valid 200; #Proxy Settings
proxy_redirect off;
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
##End Proxy Settings
}
}
## End www.quancha.cn ## }
配置文件中以proxy_开头的指令我们大都可以字面意思得到理解。请务必注意一点proxy_cache_path和proxy_temp_path设置的目录需要在同一分区,因为它们之间是硬链接的关系。
最后启动nginx,来迎接着激动人心的时刻吧。我已经迫不及待了。如果文章哪里有问题或者你遇到了什么麻烦,可以留言让我知道。
当我们浏览http://localhost:10000/222.jpg时,在代理端就把图片缓存了,不用到代理终端去缓存了,可以节省资源。缓存的内容放在你设置的proxy_cache_path路径下面,看下图
nginx proxy_cache
第一层目录只有一个字符,是由levels=1:2设置,总共二层目录,子目录名字由二个字符组成。突然发现,我在我的系统里面,配置过varnish,squid,apache和mod_cache,nginx和proxy_cache,他们都是可以对文件进行缓存,不知道他们哪一个对文件的缓存效果最好呢?有空比较一下,对了这4种缓存,这个博客里面都有的,有兴趣的朋友,可以看一下
nginx利用proxy_cache来缓存文件的更多相关文章
- 使用Nginx的proxy_cache缓存功能取代Squid(转)
Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...
- 使用Nginx的proxy_cache缓存功能取代Squid
Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...
- nginx 的 proxy_cache 缓存配置
开头:某个项目涉及到 视频切片下载到本地,然后提供服务给客户端.一个视频有多个文件,存储在本地路径下.当客户端请求的视频在本地没有就会回源, 回源拿到的视频文件再返回给客户端,同时在本地缓存一份,提供 ...
- 使用Nginx的proxy_cache缓存功能取代Squid[原创]
使用Nginx的proxy_cache缓存功能取代Squid[原创] [文章作者:张宴 本文版本:v1.2 最后修改:2009.01.12 转载请注明原文链接:http://blog.zyan.cc/ ...
- nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录
geo指令使用ngx_http_geo_module模块提供的.默认情况下,nginx有加载这个模块,除非人为的 --without-http_geo_module.ngx_http_geo_modu ...
- nginx利用limit模块设置IP并发防CC攻击
nginx利用limit模块设置IP并发防CC攻击 分类: 系统2013-01-21 09:02 759人阅读 评论(0) 收藏 举报 来源:http://blog.xencdn.net/nginx- ...
- Nginx利用lua剪辑FastDFS图片
Nginx利用lua剪辑FastDFS中的图片 我们经常用FastDFS来做图片服务器,通过nginx来上传或者获取图片.本文要实现的功能是,当客户端要获取不同尺寸的图片是,lua根据url中的尺寸大 ...
- (转)nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录
nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录 原文:http://www.cnblogs.com/kevingrace/p/6165572.html Nginx的geo模块 ...
- nginx利用lua实现nginx反向代理proxy_store缓存文件自删除
标题有点绕口.我尽量把关键词都贴进去.之前因为自己的nginx安装了ngx_lua模块,但是又需要引入 但是安装luafilesystem又需要先安装luarocks,比较繁琐.这里就想记录一下安装过 ...
随机推荐
- 内存使用分析工具Valgrind简单用法
转载自 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html 暂时还未使用过,记录下,记录下,记录下 Valgrind的主要作者 ...
- STL中vector怎么实现邻接表
最近,同期的一位大佬给我出了一道题目,改编自 洛谷 P2783 有机化学之神偶尔会做作弊 这道题好坑啊,普通链表过不了,只能用vector来存边.可能更快一些吧? 所以,我想记录并分享一下vector ...
- 我的Android进阶之旅------>如何为ListView组件加上快速滑块以及修改快速滑块图像
使用布局文件需要将android:fastScrollEnabled="true" ,如下代码所示: <ListView android:id="@+id/list ...
- Android系统移植与调试之------->如何修改Android设备添加重启、飞行模式、静音模式等功能(二)
今天要说的是为Android设备添加重启.飞行模式.静音模式按钮,客户需求中需要添加这项功能,在长按电源键弹出的菜单中没有这些选项,谨以此文记录自己添加这个功能的过程. 首先找到长按电源键弹出的对话框 ...
- cocos2d-x3.6 生成带类图的离线文档
我的博客:http://blog.csdn.net/dawn_moon cocos2d-x的官网有点慢,并且最新3.6的在线API文档居然没有了类图,不知道什么原因,之前2.2.6都是有的. 只是能够 ...
- ubuntun下安装Fiddler
对于分析网页或者写爬虫的时候经常需要用到抓包工具进行网页数据的抓包.在Windows下可以安装Fiddler来抓包.在ubuntun下不能直接安装Fiddler.需要先安装mono 1 首先安装mon ...
- HDU - 1033 Edge 【模拟】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1033 题意 给定一个起始点 300 420 走的第一步是 310 420 下面的每一步 都由 输入决定 ...
- 【二】MongoDB入门
下面是mongodb的一些基本概念: 文档是MongoDB中数据的基本单元,类似关系数据库中的行. 集合,是存储文档的容器,类似关系数据库中的表. MongoDB的单个实例容纳多个数据库,每个数据库都 ...
- 在ubuntu上为android系统编写Linux驱动程序【转】
本文转载自:http://blog.csdn.net/luoshengyang/article/details/6568411 在智能手机时代,每个品牌的手机都有自己的个性特点.正是依靠这种与众不同的 ...
- mysql中的内连接,外连接实例详解
内连接: 只连接匹配的行左外连接: 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行右外连接: 包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边 ...