Nginx缓存服务
Posted 礁之
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx缓存服务相关的知识,希望对你有一定的参考价值。
文章目录
一、缓存概述
(1)缓存的作用
- 通常情况下缓存是用来减少后端压力的,将压力尽可能的往前推,也就是往代理服务器上推,减少后端压力,提高网站并发延迟
(2)缓存常见的类型
-
后端服务器缓存
-
代理缓存
- 客户端缓存
最常用的就是代理缓存,所以这里主要说代理缓存
(3)nginx缓存的原理
二、配置Nginx缓存
(1)主配置文件中缓存的语法
proxy_cache
- 配置缓存路径
语法: proxy_cache zone\\off;
默认是off也就是关闭,zone就是指定区域名称
可以配置的区域: http、server、location
- 配置缓存周期
语法: proxy_cache_valid [code …] time;
可以配置的区域: http、server、location
示例: proxy_cache_valid 200 302 10m; 这个表示,出现了200、302状态码的页面的话,页面就缓存10分钟
- 缓存的维度
语法: proxy_cache_key string;
默认: proxy_cache_key s c h e m e scheme schemeproxy_host$request_uri;
可配置的区域: http、server、location
示例: proxy_cache_key s c h e m e scheme schemeproxy_host u r i uri uriis_args$args;
$scheme: 请求方案,http或https。
$proxy_host: 默认80端口不显示,其它显示
$uri: 对路径的资源
$is_args: 如果URL包含参数则为?,否则为空字符串
$args: 请求URL中包含的参数。
$request_uri: 完整的原始请求的URI(带有参数)
(2)配置Nginx缓存
-实验环境
系统 | 主机名 | ip地址 | Nginx版本 | 扮演角色 |
---|---|---|---|---|
Centos7.4 | proxy | 192.168.100.202 | Nginx-1.18.0 | 代理服务器 |
Centos7.4 | web | 192.168.100.203 | Nginx-1.18.0 | 后端web |
-实验目的
客户端通过代理缓存,加快访问web服务器的速度
-实验步骤
proxy的配置:
******(1)先做基础配置
[root@Centos7 ~]# hostnamectl set-hostname proxy
[root@Centos7 ~]# su
[root@proxy ~]# systemctl stop firewalld
[root@proxy ~]# setenforce 0
setenforce: SELinux is disabled
[root@proxy ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
mount: /dev/sr0 已经挂载或 /mnt 忙
/dev/sr0 已经挂载到 /mnt 上
******(2)上传Nginx源码包进行安装
[root@proxy ~]# yum -y install zlib-devel pcre-devel #安装依赖包
。。。。。。
完毕!
[root@proxy ~]# ll
总用量 1020
-rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg
-rw-r--r-- 1 root root 1039530 4月 19 10:03 nginx-1.18.0.tar.gz
[root@proxy ~]# tar xf nginx-1.18.0.tar.gz -C /usr/src/
[root@proxy ~]# cd /usr/src/nginx-1.18.0/
[root@proxy nginx-1.18.0]# useradd -M -s /sbin/nologin nginx
[root@proxy nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
[root@proxy nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@proxy nginx-1.18.0]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@proxy nginx-1.18.0]# systemctl start nginx
[root@proxy nginx-1.18.0]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3714/nginx: master
******(3)修改配置文件
[root@proxy nginx-1.18.0]# cd /usr/local/nginx/conf/
[root@proxy conf]# cp nginx.conf nginx.conf.bak
[root@proxy conf]# sed -i '/#/d' nginx.conf
[root@proxy conf]# sed -i '/^$/d' nginx.conf
[root@proxy conf]# mkdir -p /aaa/cache #创建放缓存文件的目录
[root@proxy conf]# vim nginx.conf
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 upstream cache {
11 server 192.168.100.203:8081;
12 server 192.168.100.203:8082;
13 server 192.168.100.203:8083;
14 }
15 proxy_cache_path /aaa/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
16 server {
17 listen 80;
18 server_name localhost;
19 root html;
20 index index.html;
21 location / {
22 proxy_pass http://cache; #指定跳转,cache就是上面的负载均衡
23 proxy_cache code_cache;
24 proxy_cache_valid 200 304 12h;
25 proxy_cache_valid any 10m;
26 add_header Nginx-cache "$upstream_cache_status";
27 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
28 #include proxy_params;
29 }
30 }
31 }
[root@proxy conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
******注释:
proxy_cache_path /aaa/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
/aaa/cache:指定缓存数据存放目录,在配置之前要先创建好
levels=1:2 :按照两层目录分级去存放缓存数据,也就是数据会分两层目录进行存放
keys_zone=code_cache:10m :开辟缓存空间的名称,10m表示大小,1m可以存放8000key
max_size=10g :限制缓存数据最大的大小,超过指定大小后Nginx会清除时间最远的缓存数据
inactive=60m :表示保存的缓存60分钟内没有被访问的话就会被清除
use_temp_path=off :临时文件,会影响性能,建议关闭,不关的话Nginx默认是有一个存放缓存的目录的,关了可以指定缓存的目录
proxy_cache code_cache :开启缓存,后面跟的是区域的名称,就是 proxy_cache_path中keys_zone指定的区域
proxy_cache_valid 200 304 12h :表示状态码是200和304的页面缓存12个小时
proxy_cache_valid any 10m :其他的所有状态码缓存10分钟
add_header Nginx-cache "$upstream_cache_status":修改头部信息,增加Nginx-cache变量的值,值为$upstream_cache_status
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 :出现500、502、503、504状态码的页面直接交给后端服务器处理,不进行缓存
******
[root@proxy conf]# systemctl start nginx
[root@proxy conf]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3714/nginx: master
web的配置:
******(1)和proxy相同做基础配置,安装Nginx(略)
******(2)编写网页页面
[root@web ~]# mkdir -p /zhangsan/A/
[root@web ~]# mkdir -p /zhangsan/B/
[root@web ~]# mkdir -p /zhangsan/C/
[root@web ~]# echo "A" > /zhangsan/A/A1.html
[root@web ~]# echo "B" > /zhangsan/B/B1.html
[root@web ~]# echo "C" > /zhangsan/C/C1.html
[root@web ~]# vim /usr/local/nginx/conf/nginx.conf #修改配置文件
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 server {
11 listen 8081;
12 root /zhangsan/A;
13 }
14 server {
15 listen 8082;
16 root /zhangsan/B;
17 }
18 server {
19 listen 8083;
20 root /zhangsan/C;
21 }
22 }
[root@web ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web ~]# systemctl start nginx
[root@web ~]# netstat -anpt | grep nginx #检查是否有三个端口
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 1136/nginx: master
tcp 0 0 0.0.0.0:8082 0.0.0.0:* LISTEN 1136/nginx: master
tcp 0 0 0.0.0.0:8083 0.0.0.0:* LISTEN 1136/nginx: master
使用客户端测试
使用本机查看是否有缓存,注意选项是大写的i
删除缓存,再次查看,直接清空指定的缓存目录即可
(3)配置指定页面不进行缓存
******(1)在代理服务器上做配置
#修改配置文件
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 upstream cache {
11 server 192.168.100.203:8081;
12 server 192.168.100.203:8082;
13 server 192.168.100.203:8083;
14 }
15 proxy_cache_path /aaa/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
16 server {
17 listen 80;
18 server_name localhost;
19 if ($request_uri ~ ^/(A1|login|register|password)) {
20 set $cookie_nocache 1;
21 }
22 root html;
23 index index.html;
24 location / {
25 proxy_pass http://cache;
26 proxy_cache code_cache;
27 proxy_cache_valid 200 304 12h;
28 proxy_cache_valid any 10m;
29 proxy_cache_key $host$uri$is_args$args;
30 proxy_no_cache $cookie_nocache $arg_nocache $arg_comment $http_pargma $http_authorization;
31 add_header Nginx-cache "$upstream_cache_status";
32 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
33 #include proxy_params;
34 }
35 }
36 }
[root@proxy ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@proxy ~]# nginx -s reload
******(2)进行测试
[root@proxy ~]# rm -rf /aaa/cache/* #先清除缓存
[root@proxy ~]# curl -s -I http://192.168.100.202/A1.html | grep "Nginx-cache" #不断进行访问
Nginx-cache: MISS
[root@proxy ~]# curl -s -I http://192.168.100.202/A1.html | grep "Nginx-cache"
[root@proxy ~]# curl -s -I http://192.168.100.202/A1.html | grep "Nginx-cache"
[root@proxy ~]# curl -s -I http://192.168.100.202/A1.html | grep "Nginx-cache"
Nginx-cache: MISS
[root@proxy ~]# curl -s -I http://192.168.100.202/A1.html | grep "Nginx-cache"
[root@proxy ~]# curl -s -I http://192.168.100.202/A1.html | grep "Nginx-cache"
[root@proxy ~]# curl -s -I http://192.168.100.202/A1.html | grep "Nginx-cache"
Nginx-cache: MISS
(4)统计缓存日志
******(1)修改配置文件
[root@proxy ~]# touch /usr/local/nginx/logs/proxy_cache.log
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 upstream cache {
11 server 192.168.100.203:8081;
12 server 192.168.100.203:8082;
13 server 192.168.100.203:8083;
14 }
15 log_format main '$http_user_agent' '$request_uri' '$remote_addr - $remote_user [$time_local] "$request" '
16 '$status $body_bytes_sent "$http_referer" '
17 '"$http_user_agent" "$http_x_forwarded_for"' '"$upstream_cache_status"';
18
19 access_log /var/log/nginx/proxy_cache.log main;
20 proxy_cache_path /aaa/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
21 server {
22 listen 80;
23 server_name localhost;
24 if ($request_uri ~ ^/(A1|login|register|password)) {
25 set $cookie_nocache 1;
26 }
27 root html;
28 index index.html;
29 location / {
30 proxy_pass http://cache;
31 proxy_cache code_cache;
32 proxy_cache_valid 200 304 12h;
33 proxy_cache_valid any 10m;
34 proxy_cache_key $host$uri$is_args$args;
35 proxy_no_cache $cookie_nocache $arg_nocache $arg_comment $http_pargma $http_authorization;
36 add_header Nginx-cache "$upstream_cache_status";
37 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
38 #include proxy_params;
39 }
40 }
41 }
[root@proxy ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@proxy ~]# tail -1 /usr/local/nginx/logs/proxy_cache.log
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400/A1.html192.168.100.230 - - [26/Apr/2021:00:57:49 +0800] "GET /A1.html HTTP/1.1" 200 2 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400" "-""MISS"
#最后有MISS或者HIT
以上是关于Nginx缓存服务的主要内容,如果未能解决你的问题,请参考以下文章
Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题