Nginx基础-配置缓存web服务
1.缓存配置语法
1)proxy_cache配置语法
Syntax: proxy_cache zone | off;
Default: proxy_cache off;
Context: http, server, location
2)缓存路径
Syntax: proxy_cache_path path [levels=levels]
[use_temp_path=on|off] keys_zone=name:size [inactive=time]
[max_size=size] [manager_files=number] [manager_sleep=time][manager_threshold=time]
[loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off]
[purger_files=number] [purger_sleep=time] [purger_threshold=time];
Default: —
Context: http
3)缓存过期周期
Syntax: proxy_cache_valid [code ...] time;
Default: —
Context: http, server, location
#示例
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
4)缓存的维度
Syntax: proxy_cache_key string;
Default: proxy_cache_key $scheme$proxy_host$request_uri;
Context: http, server, location
#示例
proxy_cache_key "$host$request_uri $cookie_user";
proxy_cache_key $scheme$proxy_host$uri$is_args$args;
2.代理配置缓存
[root@localhost ~]# mkdir /var/cache/nginx
[root@localhost ~]# vim /etc/nginx/conf.d/proxy_cache.conf
upstream cache {
server 10.0.0.12:80;
server 10.0.0.13:80;
server 10.0.0.14:80;
}
#proxy_cache_path 存放缓存临时文件
#levels 按照两层目录分级
#keys_zone 开辟空间名, 10m:开辟空间大小, 1m可存放8000key
#max_size 控制最大大小, 超过后Nginx会启用淘汰规则
#inactive 60分钟没有被访问缓存会被清理
#use_temp_path 临时文件, 会影响性能, 建议关闭
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name 127.0.0.1;
#proxy_cache 开启缓存
#proxy_cache_valid 状态码200|304的过期为12h, 其余状态码10分钟过期
#proxy_cache_key 缓存key
#add_header 增加头信息, 观察客户端respoce是否命中
#proxy_next_upstream 出现502-504或错误, 会跳过此台服务器访问下台
location / {
proxy_pass http://cache;
proxy_cache code_cache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
include proxy_params;
}
}
3.客户端测试
[root@localhost ~]# curl -s -I http://127.0.0.1/index.html | grep "Nginx-Cache"
Nginx-Cache: MISS
[root@localhost ~]# curl -s -I http://127.0.0.1/index.html | grep "Nginx-Cache"
Nginx-Cache: HIT
4.缓存清理
1)删除已缓存数据
[root@proxy ~]# rm -rf /var/cache/nginx/*
[root@localhost ~]# curl -s -I http://127.0.0.1/index.html | grep "Nginx-Cache"
Nginx-Cache: MISS
2)使用ngx_cache_purge扩展模块清理,需要编译安装nginx(安装过程略)
修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
upstream cache {
server 10.0.0.12:80;
server 10.0.0.13:80;
server 10.0.0.14:80;
}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://cache;
proxy_cache code_cache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
include proxy_params;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
proxy_cache_purge code_cache $host$1$is_args$args;
}
}
...
清理缓存
[root@localhost ~]# curl http://127.0.0.1/purge/index.html
5.部分页面不缓存
1)指定部分页面不进行proxy_Cache缓存
[root@localhost conf.d]# vim proxy_cache.conf
upstream cache{
server 10.0.0.12:80;
server 10.0.0.13:80;
server 10.0.0.14:80;
}
proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name 127.0.0.1;
if ($request_uri ~ ^/(login |register|password)) {
set $cookie_nocache 1;
}
location / {
proxy_pass http://cache;
proxy_cache code_cache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
proxy_cache_key $host$uri$is_args$args;
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
proxy_no_cache $http_pargma $http_authorization;
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
include proxy_params;
}
}
2)清理原先的缓存
[root@localhost ~]# rm -rf /var/cache/nginx/*
3)请求测试
[root@localhost ~]# curl -s -I http://127.0.0.1/login | grep "Nginx-Cache"
Nginx-Cache: MISS
[root@localhost ~]# curl -s -I http://127.0.0.1/login | grep "Nginx-Cache"
Nginx-Cache: MISS
[root@localhost ~]# curl -s -I http://127.0.0.1/login | grep "Nginx-Cache"
Nginx-Cache: MISS
6.缓存日志记录统计
通过日志记录proxy_cache命中情况与对应url
1)修改/etc/nginx/nginx.conf中log_format格式
log_format main '$http_user_agent' '$request_uri' '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"' '"$upstream_cache_status"';
2)查看访问日志(略)
作者:wanghongwei
版权声明:本作品遵循<CC BY-NC-ND 4.0>版权协议,商业转载请联系作者获得授权,非商业转载请附上原文出处链接及本声明。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?