为什么要做web cache,我想大家最主要的是解决流量的压力。随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力。与此同时某些网站的页面内容并不是经常变化,因此我们可以分两层架构来组织网站。前端web缓存+后端web服务器,可以参看这里配置nginx反向代理配置

前端web缓存有多重方式实现,原理就是队请求结果页面静态化并设置一个超时期限,缓存页面过期后,新请求到达时重新到后端web服务器获取内容更新;没有nginx前比较流行的方法是squid,但squid不能充分利用处理器的多核特性,越来越多的网站选用nginx来做前端的web缓存。

要想使用nginx的缓存功能要保证nginx添加了proxy模块。我们可以使用-V选项(大写的V,小写的v是看版本号的)来查看nginx的编译参数。我使用的是默认的参数编译的,如下所示:

root@SNDA-172-17-12-117:/usr/local/nginx# ./nginx -V
nginx version: nginx/1.2.3
built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
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.7

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 /data/cache levels=: keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /data/temp;
proxy_buffer_size 4k;
proxy_buffers 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 ; #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 ;
proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_buffer_size 4k;
proxy_buffers 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,来迎接着激动人心的时刻吧。我已经迫不及待了。如果文章哪里有问题或者你遇到了什么麻烦,可以留言让我知道。

使用nginx的proxy_cache做网站缓存的更多相关文章

  1. 高性能Web服务器Nginx的配置与部署研究(13)应用模块之Memcached模块+Proxy_Cache双层缓存模式

    通过<高性能Web服务器Nginx的配置与部署研究——(11)应用模块之Memcached模块的两大应用场景>一文,我们知道Nginx从Memcached读取数据的方式,如果命中,那么效率 ...

  2. 使用Nginx的proxy_cache缓存功能取代Squid(转)

    Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...

  3. 【摘自张宴的"实战:Nginx"】使用nginx的proxy_cache模块替代squid,缓存静态文件

    #user nobody;worker_processes 1; error_log logs/static_source.error.log;#error_log logs/error.log no ...

  4. 使用Nginx的proxy_cache缓存功能取代Squid

    Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...

  5. 使用Nginx的proxy_cache缓存功能取代Squid[原创]

    使用Nginx的proxy_cache缓存功能取代Squid[原创] [文章作者:张宴 本文版本:v1.2 最后修改:2009.01.12 转载请注明原文链接:http://blog.zyan.cc/ ...

  6. HappyAA服务器部署笔记2(nginx的静态资源缓存配置)

    我近期对服务器进行了少量改进,虽然之前使用了nginx反向代理之后性能有所提高,但仍然不够,需要使用缓存来大幅度提高静态资源的访问速度. 服务器上的静态资源主要有这些:png, jpg, svg, j ...

  7. Nginx的静态资源缓存以及压缩

    Nginx是一款轻量级的网页服务器.反向代理器以及电子邮件代理服务器.Nginx采用的是异步非阻塞的通信机制(epoll模型),支持更大的并发连接.所谓的epoll模型:当事件没有准备好时,就放入ep ...

  8. 初识nginx反向代理和缓存机制

    实现的需求图:   环境: nginx缓存和反向代理服务器:192.168.0.224 实际存储数据机器:192.168.0.37 一.实现反向代理 1.安装nginx,两台服务器都需要安装 1)安装 ...

  9. nginx利用proxy_cache来缓存文件

    为什么要做web cache,我想大家最主要的是解决流量的压力.随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力.与此同时某些网站的页面 ...

随机推荐

  1. 第十六课:一些奇葩的元素节点object,video

    object元素 object这个元素,现在前端很少用到,但是像flash,svg等奇葩元素,必须嵌套在object对象元素中.现代浏览器用video,canvas代替这些元素. 之前做过图表和地图的 ...

  2. 【CodeForces 602C】H - Approximating a Constant Range(dijk)

    Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...

  3. 【Aizu 2305】Beautiful Currency

    题 题意 给你n个货币价格,然后通过调整一些货币的大小,使得所有比自己小的货币都是该货币的约数,调整前第 i 货币为a,调整后为b 那么变化率为 ri=|a-b|/a ,总变化率为max(ri).求最 ...

  4. Oracle查看锁表

     查看锁表进程SQL语句1: select sess.sid,     sess.serial#,     lo.oracle_username,     lo.os_user_name,     a ...

  5. Oracle修改数据表

    (1)修改数据表中某个字段值为空: update 表明   别名  set 别名.字段名 例子:update JWT_JYWF t set t.jdsbh=''(2)设置数据表中某个字段值和rownu ...

  6. USACO 3.2 butter 最短路

    堆优化dijkstra /* PROB:butter LANG:C++ */ #include <iostream> #include <cstdio> #include &l ...

  7. Mac OS X 10.9 Mavericks安装后,Xcode调试时模拟器黑屏的处理方法

    请耐心的等下去吧,少年! 装了Mac OS X 10.9 Mavericks的同学,如果碰到Xcode调试App时,模拟器黑屏(重置也无效),请耐心的等下去吧,大约10来分钟左右黑屏就会消失,App启 ...

  8. 用Filter程序实现静态HTML页面的访问保护

    今天为练习Filter的用法编写了一个小程序. 当用户通过article的超链接读取文章的时候,会通过Filter进行检测有没有登录.只有登录的读者才能跳到文章页面,否则跳到登录页面. 文章就用简单的 ...

  9. 架构(Architecture)和框架(Framework)杂谈

    1. 架构和框架的设计层次不同       类似于硬件设计,软件设计也分为不同的层次.典型的软件设计层次如下图:        在这个图中我们可以看到,Framework处于Micro-archite ...

  10. 解决IE apk变成zip:Android 手机应用程序文件下载服务器Nginx+Tomcat配置解决方法

    APK文件其实是zip格式,但后缀名被修改为apk,通过UnZip解压后,可以看到Dex文件,Dex是Dalvik VM executes的全称,即Android Dalvik执行程序,并非Java ...