nginx的proxy_cache缓存配置
为什么要做web cache,我想大家最主要的是解决流量的压力。随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力。与此同时某些网站的页面内容并不是经常变化,因此我们可以分两层架构来组织网站。前端web缓存+后端web服务器。
前端web缓存有多重方式实现,原理就是队请求结果页面静态化并设置一个超时期限,缓存页面过期后,新请求到达时重新到后端web服务器获取内容更新;没有nginx前比较流行的方法是squid,但squid不能充分利用处理器的多核特性,越来越多的网站选用nginx来做前端的web缓存。
要想使用nginx的缓存功能要保证nginx添加了proxy模块。我们可以使用-V选项(大写的V,小写的v是看版本号的)来查看nginx的编译参数。我使用的是默认的参数编译的,如下所示:
- root@SNDA-172-17-12-117: /usr/local/nginx/sbin/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 1;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid;
- events {
- worker_connections 1024;
- }
- 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 0;
- keepalive_timeout 65;
- #Compression Settings
- gzip on;
- gzip_http_version 1.0;
- gzip_comp_level 2;
- gzip_proxied any;
- gzip_min_length 1100;
- gzip_buffers 16 8k;
- gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
- # Some version of IE 6 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:8080; #Apache1
- }
- ## Start www.quancha.cn ##
- server {
- listen 80;
- 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设置,总共二层目录,子目录名字由二个字符组成。
nginx的proxy_cache缓存配置的更多相关文章
- 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缓存功能取代Squid[原创]
使用Nginx的proxy_cache缓存功能取代Squid[原创] [文章作者:张宴 本文版本:v1.2 最后修改:2009.01.12 转载请注明原文链接:http://blog.zyan.cc/ ...
- NGINX负载均衡缓存配置
环境:VMware-Workstation-12-Pro,Windows-10,CentOS-7.5,Xshell5 1 概述 如果我们的架构是前端负载均衡后端WEB集群时,可以开启nginx的缓存功 ...
- nginx反向代理缓存配置
关于nginx的反向代理缓存配置,用的最多的就是CDN公司,目前CDN公司用纯nginx做缓存的已经很少了,基本都用tnginx(阿里的).openresty:但是这两款软件都是基于nignx开发的, ...
- Nginx 静态资源缓存配置
示例 # Media: images, icons, video, audio, HTC location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|m ...
- Nginx 学习笔记(三)proxy_cache 缓存配置和ngx_cache_purge模块
反向代理的缓存清理 一.proxy_cache配置 (1)如何配置和安装,都在这里了:https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Ng ...
- nginx缓存配置的操作记录梳理
web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...
随机推荐
- [COGS2479 && COGS2639]高维偏序(CDQ分治,bitset)
COGS2479:四维偏序. CDQ套CDQ CDQ:对a分治,对b排序,再对a打标记,然后执行CDQ2 CDQ2:对b分治,对c归并排序,对d树状数组. #include<cstdio> ...
- Codeforces #480 Tutorial
Problem A,B,C: 简单的模拟,注意A中p mod q时对q=0特殊处理(注意范围) Problem D: Brief Intro: 给定长度为N的数组A,将A中所有连续子序列分成最少的组, ...
- OC语言基础之NSString
1.字符串的创建 1: NSString *s1 = @"jack"; 2: 3: //NSString *s2 = [[NSString alloc] initWithStrin ...
- android出现段错误时的查找定位的方法
android出现段错误时的查找方法,例如出现log: - ::): Fatal signal (SIGSEGV) at ), thread (SurfaceFlinger) - ::): *** * ...
- java中的注解详解和自定义注解
一.java中的注解详解 1.什么是注解 用一个词就可以描述注解,那就是元数据,即一种描述数据的数据.所以,可以说注解就是源代码的元数据.比如,下面这段代码: @Override public Str ...
- 【Node.js】1.安装步骤
1.在官网找到对应版本下载 2.点击安装 3.检查安装成功 path可以看到node.js的安装目录已经自动配置在环境变量path中了 如果node -v或者node --version不能正确展示当 ...
- IOS开发使用委托delegate在不同窗口之间传递数据
IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容.在IOS开发里两个UIView窗口之间 ...
- 关于BOM UTF8
这三篇可以看下: http://www.zhihu.com/question/20167122 http://www.cnblogs.com/DDark/archive/2011/11/28/2266 ...
- Java几种常见的四舍五入的方法
/* * 在上面简单地介绍了银行家舍入法,目前java支持7中舍入法: 1. ROUND_UP:远离零方向舍入.向绝对值最大的方向舍入,只要舍弃位非0即进位. 2. ROUND_DOWN:趋向零方向舍 ...
- centos7.2 kvm虚拟化管理平台WebVirtMgr部署
在服务器上部署kvm虚拟化,虚出多台VM出来,以应对新的测试需求.当KVM宿主机越来越多,需要对宿主机的状态进行调控,决定采用WebVirtMgr作为kvm虚拟化的web管理工具,图形化的WEB,让人 ...