nginx作为正向和反向代理服务器中的缓存设置
|
admin
2025年6月28日 22:25
本文热度 35
|
控制浏览器缓存时间
server {
listen 8399;
server_name localhost;
location / {
# 代理
proxy_pass http://api_proxy;
# 1.浏览器缓存10秒钟
expires 10s;
# 2.在晚上10点30的时候过期
# expires @22h30m;
# 3.缓存在一小时前有效,表示已过期
# expires -1h;
# 4.不设置缓存
# expires epoch;
# 5.关闭缓存,浏览器自己控制缓存
# expires off;
# 6.最大过期时间
# expires max;
}
}
反向代理缓存设置
upstream api_proxy {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
}
# proxy_cache_path:设置缓存保存的目录的位置
# keys_zone:设置共享内以及占用的空间大小
# mas_size:设置缓存最大空间
# inactive:缓存过期时间,错过此时间自动清理
# use_temp_path:关闭零时目录
proxy_cache_path /usr/local/nginx/upsteam_cache keys_zone=mycache:5m max_size=1g inactive=8h use_temp_path=off;
server {
listen 8599;
server_name localhost;
# 开启并使用缓存
proxy_cache mycache;
# 针对200和304响应码的缓存过期时间
proxy_cache_valid 200 304 8h;
location / {
proxy_pass http://api_proxy;
}
}
复用TCP连接
upstream api_test {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
# 复用连接
keepalive 32;
}
server {
listen 8699;
server_name localhost;
location / {
proxy_pass http://api_test;
# 连接的协议版本
proxy_http_version 1.1;
# 清空连接请求头
proxy_set_header Connection "";
}
}
阅读原文:原文链接
该文章在 2025/7/1 23:43:24 编辑过