nginx添加srcache-nginx模块进行页面缓存
来源:互联网 发布:信息与网络安全管理 编辑:程序博客网 时间:2024/05/18 00:45
期间在网上看了几篇博客,发现内容都基本一致,而我自己按照博客指示来不奏效的,可能我是自己问题
后来经过一系列的摸索,成功对页面进行了缓存,所以以此篇博客记录下来,也希望能帮助到有需要的人
nginx、redis的安装配置就不记录,只记录最核心关键的部分:
我的环境:
系统:Ubuntu 14.04.5 LTS
nginx:1.9.9
一、首先先介绍一个网址
https://www.nginx.com/resources/wiki/modules/
这是nginx的第三方模块列表,有模块名字、模块介绍、和github的地址,github里面有详细的介绍和教程
srcache-nginx-module就是我要用到的模块
二、下载srcache-nginx-module
我们来到以下地址,可下载该模块
https://github.com/openresty/srcache-nginx-module
三、nginx添加srcache-nginx-module之前
nginx.conf的配置:
user www-data;worker_processes 1;worker_rlimit_nofile 65535;events { use epoll; worker_connections 10240;}http { include mime.types; default_type application/octet-stream; access_log on; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location / { } }}
我把注释和其他无关配置都删除,更简单明了
除了开启了gzip等一些配置项以外,基本没改动,同时我们再看看nginx现有的模块
没有任何的添加模块
首页和css文件内容如下:
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>首页</title><link rel="stylesheet" type="text/css" href="css/index.css" /></head><body></body></html>
@CHARSET "UTF-8";body { background:blue;}目录结构如下:
页面和css也非常简单,首页引入index.css,body背景为蓝色
如无意外启动nginx成功后,访问首页会看到如下图:
F12打开console,查看Response Headers,方便后面对比
四、准备srcache-nginx-module和其他模块
下载srcache-nginx-module之前,我们先不妨看一下用redis作缓存的介绍
https://github.com/openresty/srcache-nginx-module#caching-with-redis
会看到一个配置的例子,和另外需要到的模块的说明
echo-nginx-module、HttpRedisModule、redis2-nginx-module、ngx_devel_kit-0.2.19、set-misc-nginx-module
把各模块都下载解压好后,我的目录结构如下图:
五、重新编译nginx
重新编译就是通过./configure的--add-module来添加模块
./configure --add-module=/usr/local/nginx/ngx_redis_module/echo-nginx-module-master --add-module=/usr/local/nginx/ngx_redis_module/ngx_http_redis-0.3.8 --add-module=/usr/local/nginx/ngx_redis_module/redis2-nginx-module-master --add-module=/usr/local/nginx/ngx_redis_module/srcache-nginx-module-master
模块存放的目录要自行修改
之后make,但不要用make install
我们进到objs目录下
只需要把nginx复制到之前nginx的安装目录的sbin即可
再看看nginx现有的模块
六、修改nginx.conf
user www-data;worker_processes 1;worker_rlimit_nofile 65535;events { use epoll; worker_connections 10240;}http { include mime.types; default_type application/octet-stream; access_log on; sendfile on; keepalive_timeout 65; gzip on; upstream redisServer{ server 127.0.0.1:20169; keepalive 10; } server {listen 80; server_name localhost; location / { }location /css {default_type text/css; set $key $uri; set_escape_uri $escaped_key $key; add_header X-Cached-From $srcache_fetch_status; add_header X-Cached-Store $srcache_store_status; add_header X-Key $key; srcache_fetch GET /redis $key; srcache_store PUT /redis2 key=$escaped_key&exptime=120; } location = /redis {internal;set_md5 $redis_key $args; redis_pass redisServer;}location = /redis2 {internal;set_unescape_uri $exptime $arg_exptime;set_unescape_uri $key $arg_key;set_md5 $key;redis2_query set $key $echo_request_body;redis2_query expire $key $exptime;redis2_pass redisServer;} }}
七、对比开启缓存后的区别
打开浏览器访问首页(为了更好测试,先把Disable cache勾上)
因为在nginx.conf加了X-Cached-From、X-Cached-Store、X-Key的显示,所以这里是可以看到缓存的情况
这是配置了redis缓存后第一次访问,所以是没有击中缓存的
查看下redis,发现有记录了
整个css的内容都进来了
再刷新页面
发现命中了缓存
后续:
如果在redis的缓存没过期前修改了css内容,会发现页面缓存没变,浏览器刷新也还是看到旧的
方法可以用知乎上的一个精彩回答
https://www.zhihu.com/question/20790576/answer/32602154?utm_source=qq&utm_medium=social
通过数据摘要、非覆盖式发布来完成
下面来简单演示:
- nginx添加srcache-nginx模块进行页面缓存
- 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
- 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
- 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
- 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
- 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
- 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
- Nginx缓存模块proxy_store
- nginx-页面缓存
- Nginx之页面缓存
- Nginx之页面缓存
- Nginx 页面缓存指令
- nginx memcache 页面缓存
- nginx 添加C++模块
- (转)nginx添加模块
- nginx添加模块
- nginx重新添加模块
- nginx添加tcp模块
- RabbitMQ-Centos6.X环境配置
- 协议解析小窍门- readchar
- 【模拟试题】Scout YYF I
- Fastboot使用详解
- 高效的jQuery代码编写技巧总结
- nginx添加srcache-nginx模块进行页面缓存
- 华为机试 字符串最后一个单词的长度
- MapReduce中的shuffle机制
- 442. Find All Duplicates in an Array
- C#使用itextsharp生成PDF文件
- No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/tx_search_frien
- forward() 与redirect()的区别
- 四旋翼微型飞行器设计
- Android开发Mp3播放器工具类