JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

NGINX 增加 缓存解决一些高并发的一些请求

wys521 2024-09-17 02:48:31 精选教程 24 ℃ 0 评论

NGINX 增加 缓存解决一些高并发的一些请求

一、缓存的作用

(1)缓解后台服务在一定时间内处理同样的请求【同样的请求地址 和 参数】

(2)过滤前端向后台在一定时间内重复提交的请求

二、配置操作

(1)增加一个 缓存文件夹目录

在nginx 的目录 /usr/local/nginx/ 下 创建一个 cache_data



(2) 在 nginx 的配置文件里 /usr/local/nginx/conf/nginx.conf 增加如下配置










http {
#配置缓存路径,10m 可以缓存 80000个key,失效时间10分钟,最大缓存100g
proxy_cache_path "./cache_data/" levels=1:2 keys_zone=hot_cache:10m inactive=60s max_size=100g;

server {
location /gis-rest/ {
proxy_pass http://appserver/;
#用户真实ip配置
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_cache hot_cache;
proxy_cache_valid 200 60s;
proxy_cache_methods GET POST;
proxy_cache_use_stale updating;
proxy_cache_key "$request_uri|$request_body";
add_header cache $upstream_cache_status;
}
}
}






(3)重启 nginx

systemctl stop run_nginx.service

systemctl start run_nginx.service

systemctl status run_nginx.service

(4) 刷新页面 /gis-rest/ 的请求都会用缓存机制



Response Header 里的 Cache 值是 HIT 代表缓存返回 MISS 代表不用缓存的。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表