点赞
评论
收藏
分享
举报
nginx系列之六:cache服务
发表于2020-09-03 18:30

浏览 1.5k

前言

nginx系列之一:nginx入门
nginx系列之二:配置文件解读
nginx系列之三:日志配置
nginx系列之四:web服务器
nginx系列之五: 负载均衡
nginx系列之六:cache服务
nginx系列之七:限流配置
nginx系列之八:使用upsync模块实现负载均衡

转自:在此感谢原博主的整理分享

一、配置文件

1.1 nginx.conf 主配置文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
#CDN Include
    include proxy.conf;	#代理配置
    include upstrem.conf;	#负载均衡配置
    include blog.biglittleant.cn.conf;	
    server {
        listen       80;
        server_name  localhost;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

1.2 代理配置

cat proxy.conf 
#CDN
proxy_temp_path /data/cdn_cache/proxy_temp_dir;
proxy_cache_path /data/cdn_cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=1g;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404;
[root@data-1-1 conf]# cat upstrem.conf 
upstream blog.biglittleant.cn
{
        server 192.168.56.102:80 weight=10 max_fails=3;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

1.3 负载均衡配置

cat upstrem.conf 
#配置内容
upstream blog.biglittleant.cn
{
        server 192.168.56.102:80 weight=10 max_fails=3;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

1.4 虚拟主机配置

cat blog.biglittleant.cn.conf 
# 配置内容
server {
    listen 80;
    server_name blog.biglittleant.cn;
    access_log logs/blog.biglittleant.cn-access.log main;
  
    location ~ .*\.(gif|jpg|png|html|htm|css|js|ico|swf|pdf)$ {
        #Proxy 
        proxy_redirect off;
        proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
        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_pass    http://blog.biglittleant.cn;

        #Use Proxy Cache
        proxy_cache cache_one;
        proxy_cache_key "$host$request_uri";
        add_header Cache "$upstream_cache_status";
        proxy_cache_valid  200 304 301 302 8h;
        proxy_cache_valid 404 1m;
        proxy_cache_valid  any 2d;
    }
    location / {
                proxy_redirect off;
                proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
                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_pass    http://blog.biglittleant.cn;
                client_max_body_size 40m;
                client_body_buffer_size 128k;
                proxy_connect_timeout 60;
                proxy_send_timeout 60;
                proxy_read_timeout 60;
                proxy_buffer_size 64k;
                proxy_buffers 4 32k;
                proxy_busy_buffers_size 64k;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

二、测试

2.1 创建一个缓存目录

mkdir /data/cdn_cache -p
  • 1

2.2 查看进程

查看进程发现多了两个cache进程:

[root@ style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font: 400 14px "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif !important; overflow-wrap: break-word; color: rgb(153, 153, 153);">]# ps -ef |grep nginx 
root       5620      1  0 21:31 ?        00:00:00 nginx: master process sbin/nginx
nginx      5621   5620  0 21:31 ?        00:00:00 nginx: worker process
nginx      5622   5620  0 21:31 ?        00:00:00 nginx: cache manager process
nginx      5623   5620  0 21:31 ?        00:00:00 nginx: cache loader process
  • 1
  • 2
  • 3
  • 4
  • 5

2.3 查看缓存命中情况

cache未命中的情况

cache未命中的情况
cache命中的情况
cache命中的情况

2.4通过上面的图得到如下结论

  1. 访问html的时候,不走缓存。
  2. 第一次访问图片的时候,cache是miss的状态。
  3. 第二次访问图片的时候,cache是hit的状态。

登录缓存服务器查看

2.5 分析nginx缓存过程

第一步:访问了两个URL:http://192.168.56.101/index.html,http://192.168.56.101/b.jpg

第二步查看缓存目录:

[root@data-1-1 cdn_cache]# tree -A /data/cdn_cache/
/data/cdn_cache/
+-- proxy_cache_dir
|   +-- 9
|   |   +-- a8
|   |       +-- f28e02e3877f3826567907bcb0ebea89
|   +-- e
|       +-- 88
|           +-- 114250cf63938b2f9c60b2fb3e4bd88e
+-- proxy_temp_dir

6 directories, 2 files
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

第三步:
缓存配置参数:

proxy_cache_path /data/cdn_cache/proxy_cache_dir levels=1:2
  • 1

第四步查看缓存内容:

第五步:分析过程
通过对key加密

echo -n '192.168.56.101/index.html' |md5sum |awk '{print $1}'       
114250cf63938b2f9c60b2fb3e4bd88e
 echo -n '192.168.56.101/b.jpg' |md5sum |awk '{print $1}'       
f28e02e3877f3826567907bcb0ebea89
  • 1
  • 2
  • 3
  • 4

分析结果:

  1. nginx根据配置levels=1:2进行缓存。
  2. 其中1表示MD5的最后一位。
  3. 其中2表示MD5的倒数第三位和第三位。
  4. 一个冒号表示一层。

参考

NGINX缓存使用官方指南
HTTP 缓存


已修改于2023-03-09 02:07
创作不易,留下一份鼓励
守望

暂无个人介绍

关注



写下您的评论
发表评论
全部评论(0)

按点赞数排序

按时间排序

关于作者
守望
这家伙很懒还未留下介绍~
89
文章
0
问答
17
粉丝
相关文章
介绍nginx网页配置工具QQ技术交流群1:1106758598QQ技术交流群2:560797506邮箱: cym1102@qq.com官网地址: http://www.nginxwebui.cn码云: https://gitee.com/cym1102/nginxWebUIgithub: https://github.com/cym1102/nginxWebUI功能特点nginxWebUI也可管理多个nginx服务器集群,随时一键切换到对应服务器上进行nginx配置,也可以一键将某台服务器配置同步到其他服务器,方便集群管理.部署此项目后,配置nginx再也不用上网各种搜索配置代码,再也不用手动申请和配置ssl证书,只需要在本项目中进行增删改查就可方便的配置和启动nginx。技术说明本项目是基于springBoot的web系统,数据库使用sqlite,因此服务器上不需要安装任何数据库项目启动时会释放一个.sqlite.db到系统用户文件夹中,注意进行备份本系统通过Let'sencrypt申请证书,使用acme.sh脚本
点赞 6
浏览 6.3k
  前三周学习了陶辉老师的“NGINX基础培训系列课程”,感觉受益良多,在这里想把一些知识点记录一下,和大家分享一下知识点,也方便日后的随手查看,温故知新。  首先,我们了解到了Nginx的版本,Nginx发布版本分为主线版本和稳定版本,区分两个版本也非常简单,主线版本版本号为单数,比如1.19,稳定版本为双数,比如1.18,今天我要说的是稳定版本,这个版本会尽量少的减少Nginx的bug问题,适用于生产环境,这里我不建议使用Nginx和其他软件一样在生产环境中落后一个或多个大版本使用,之前生产环境做过漏扫,发现我们编译自带的Nginx版本为:nginx/1.13.3(查询命令为nginx-V),结果出现了多个漏洞,四个高危和一个中危漏洞:        通过升级Nginx到稳定版最新版本后修复!  其次,是Nginx发行版本的选择,目前比较流行的有:nginx、nginxplus、Tengine、openresty、ope
点赞 1
浏览 3.4k
感谢您参加“NGINX从入门到精通进阶系列培训”!以下为培训的问答、课件和录像,希望您能通过此培训学有所得,祝学习进步!>问与答:- 基础篇+高级篇 - 应用篇+实战篇(New)>课件(PPT):基础篇:-NGINX概要、安装、配置:https://interact.f5.com/rs/653-SMC-783/images/CNFEB22-NginxCoreCourse-Setup.pdf-NGINX日志、运维:https://interact.f5.com/rs/653-SMC-783/images/cnfeb22-nginxcorecourse-maintenance.pdf高级篇:-NGINX变量、API:https://interact.f5.com/rs/653-SMC-783/images/CNFEB22-NginxCoreCourse-API.pdf-NGINXSSL、NJS:https://interact.f5.com/rs/653-SMC-783/images/CNFEB22-NginxCoreCourse-SSL.pdf
点赞 10
浏览 4.9k