当前位置: 首页 > news >正文

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默认安装的模块如下

模块名称描述版本如何禁用
CoreControl ports, locations, error pages, aliases, and other essentials. --without-http
AccessAllow/deny based on IP address. --without-http_access_module
Auth BasicBasic HTTP authentication. --without-http_auth_basic_module
Auto IndexGenerates automatic directory listings. --without-http_autoindex_module
BrowserInterpret "User-Agent" string.0.4.3--without-http_browser_module
CharsetRecode web pages. --without-http_charset_module
Empty GIFServe a 1x1 image from memory.0.3.10--without-http_empty_gif_module
FastCGIFastCGI Support. --without-http_fastcgi_module
GeoSet config variables using key/value pairs of IP addresses.0.1.17--without-http_geo_module
GzipGzip responses. --without-http_gzip_module
HeadersSet arbitrary HTTP response headers. 
IndexControls which files are to be used as index. 
Limit RequestsLimit frequency of connections from a client.0.7.20--without-http_limit_req_module
Limit ZoneLimit simultaneous connections from a client. Deprecated in 1.1.8, use Limit Conn Instead.0.5.6--without-http_limit_zone_module
Limit ConnLimit concurrent connections based on a variable. --without-http_limit_conn_module
LogCustomize access logs. 
MapSet config variables using arbitrary key/value pairs.0.3.16--without-http_map_module
MemcachedMemcached support. --without-http_memcached_module
ProxyProxy to upstream servers. --without-http_proxy_module
RefererFilter requests based on Referer header. --without-http_referer_module
RewriteRequest rewriting using regular expressions. --without-http_rewrite_module
SCGISCGI protocol support.0.8.42--without-http_scgi_module
Split ClientsSplits clients based on some conditions0.8.37--without-http_split_clients_module
SSIServer-side includes. --without-http_ssi_module
UpstreamFor load-balancing. --without-http_upstream_ip_hash_module (ip_hash directive only)
User IDIssue identifying cookies. --without-http_userid_module
uWSGIuWSGI protocol support.0.8.40--without-http_uwsgi_module
X-AccelX-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设置,总共二层目录,子目录名字由二个字符组成。

相关文章:

  • 打包Scala jar 包的正确步骤
  • docker python 配置
  • scala函数和方法的差别
  • 新CIO:Mark Schwartz认为的领先IT
  • 论文笔记:Variational Capsules for Image Analysis and Synthesis
  • 与 TensorFlow 的初次相遇
  • npm怎么配置下包最神速?
  • Docker折腾记: (1)构建yapi容器,从构建发布到可用
  • 新形式下触电新闻如何打造内容安全领域新标杆
  • 3.保安队的日子(下)我当程序员的那些事1
  • python3+selenium入门04-元素定位
  • RocketMQ概述
  • Go 语言的垃圾回收演化历程:垃圾回收和运行时问题
  • 第八课-第一讲 08_01_facl及用户及Linux终端
  • python学习日记2
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • Docker 笔记(2):Dockerfile
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • Fabric架构演变之路
  • Java多态
  • Java小白进阶笔记(3)-初级面向对象
  • MobX
  • MQ框架的比较
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • Redis字符串类型内部编码剖析
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • 成为一名优秀的Developer的书单
  • 给github项目添加CI badge
  • 记一次用 NodeJs 实现模拟登录的思路
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • d²y/dx²; 偏导数问题 请问f1 f2是什么意思
  • Mac 上flink的安装与启动
  • NLPIR智能语义技术让大数据挖掘更简单
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • ​MySQL主从复制一致性检测
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • ​水经微图Web1.5.0版即将上线
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • $.ajax,axios,fetch三种ajax请求的区别
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (LeetCode 49)Anagrams
  • (八)c52学习之旅-中断实验
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (四)图像的%2线性拉伸
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (转)视频码率,帧率和分辨率的联系与区别
  • (转载)虚函数剖析
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .net core Swagger 过滤部分Api