nginx 缓存配置 expires 和 add_header Cache-Control 的总结

hello ,大家好,我是jordy;欢迎大家光临我的博客,我的联系方式有(qq): 1760282809,363232564  欢迎同行多多交流,一起学习,一起进步!

nginx 开启静态缓存:

location ~*\.(js|css|png|jpg|jpeg|gif|ico)$ {

       expires 24h;

       log_not_found off;

       #add_header Cache-Control no-store;

}

 

如果expires 和 add_header 同时开启的情况下,则add_header优于expires生效;


所以使用过期时间属性一定要确认你的Web服务器时间设置正确,一个途径是通过网络时间同步协议(Network Time Protocol NTP)。

Expires/Cache-Control Header是控制浏览器是否直接从浏览器缓存取数据还是重新发请求到服务器取数据。只是Cache-Control比Expires可以控制的多一些, 而且Cache-Control会重写Expires的规则。

Last-Modified/If-Modified-Since和ETag/If-None-Match是浏览器发送请求到服务器后判断文件是否 已经修改过,如果没有修改过就只发送一个304回给浏览器,告诉浏览器直接从自己本地的缓存取数据;如果修改过那就整个数据重新发给浏览器。

 【大家可搜索并关注我的微信公众号,名称:jordy的世界
后续会有持续更新,更多精彩等着您!】

server { listen 9007; server_name localhost; client_max_body_size 600M; proxy_connect_timeout 60s; # 代理连接超时时间 proxy_read_timeout 60s; # 代理读取超时时间 proxy_send_timeout 60s; # 代理发送超时时间 fastcgi_read_timeout 60s; # FastCGI 读取超时时间 send_timeout 60s; # 发送数据超时时间 #前端打的dist资源存放目录 root D:/ZrWrjServer/nginx-1.18.0/html; # 静态资源的缓存压缩设置 location ~* ^/a-map/(.*)$ { alias /home/map/AMap/AMap/$1; etag on; expires 24h; gzip on; gzip_types image/jpeg image/png image/gif image/jpg; } location ~* ^/map-image/(.*)$ { alias /home/map/AMap/AMap/$1; etag on; expires 24h; gzip on; gzip_types image/jpeg image/png image/gif image/jpg; } location / { # 用于配合 browserHistory使用 try_files $uri $uri/ /index.html; } location /power-system/ { proxy_pass http://10.168.1.105:9006/power-system/; proxy_redirect off; #真实IP获取 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; set $my_proxy_add_x_forwarded_for $proxy_add_x_forwarded_for; if ($proxy_add_x_forwarded_for ~* "127.0.0.1"){ set $my_proxy_add_x_forwarded_for $remote_addr; } proxy_set_header X-Forwarded-For $my_proxy_add_x_forwarded_for; } location /api/ { proxy_pass http://10.168.1.105:9006/power-system/; proxy_redirect off; #真实IP获取 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; set $my_proxy_add_x_forwarded_for $proxy_add_x_forwarded_for; if ($proxy_add_x_forwarded_for ~* "127.0.0.1"){ set $my_proxy_add_x_forwarded_for $remote_addr; } proxy_set_header X-Forwarded-For $my_proxy_add_x_forwarded_for; } location /static/defect { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root /power/defect; } location /static/video { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root /power/video; } location /static/uav { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root /power/uav; } location /static/imageManage { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root /power/uav; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 我在windows上使用这个配置但是报错2025/07/14 16:17:59 [emerg] 7236#7276: "server" directive is not allowed here in D:\ZrWrjServer\nginx-1.18.0/conf/nginx.conf:1
07-16
server { listen 9007; server_name localhost; client_max_body_size 600M; proxy_connect_timeout 60s; # 代理连接超时时间 proxy_read_timeout 60s; # 代理读取超时时间 proxy_send_timeout 60s; # 代理发送超时时间 fastcgi_read_timeout 60s; # FastCGI 读取超时时间 send_timeout 60s; # 发送数据超时时间 #前端打的dist资源存放目录 root D:/ZrWrjServer/nginx-1.18.0/html; # 静态资源的缓存压缩设置 location ~* ^/a-map/(.*)$ { alias /home/map/AMap/AMap/$1; etag on; expires 24h; gzip on; gzip_types image/jpeg image/png image/gif image/jpg; } location ~* ^/map-image/(.*)$ { alias /home/map/AMap/AMap/$1; etag on; expires 24h; gzip on; gzip_types image/jpeg image/png image/gif image/jpg; } location / { # 用于配合 browserHistory使用 try_files $uri $uri/ /index.html; } location /power-system/ { proxy_pass http://192.168.127.16:9006/power-system/; proxy_redirect off; #真实IP获取 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; set $my_proxy_add_x_forwarded_for $proxy_add_x_forwarded_for; if ($proxy_add_x_forwarded_for ~* "127.0.0.1"){ set $my_proxy_add_x_forwarded_for $remote_addr; } proxy_set_header X-Forwarded-For $my_proxy_add_x_forwarded_for; } location /api/ { proxy_pass http://192.168.127.16:9006/power-system/; proxy_redirect off; #真实IP获取 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; set $my_proxy_add_x_forwarded_for $proxy_add_x_forwarded_for; if ($proxy_add_x_forwarded_for ~* "127.0.0.1"){ set $my_proxy_add_x_forwarded_for $remote_addr; } proxy_set_header X-Forwarded-For $my_proxy_add_x_forwarded_for; } location /static/defect { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root D:/ZrWrjServer/zrgn_wrj/defect; } location /static/video { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root D:/ZrWrjServer/zrgn_wrj/video; } location /static/uav { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root D:/ZrWrjServer/zrgn_wrj/uav; } location /static/imageManage { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root D:/ZrWrjServer/zrgn_wrj/uav; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 我在windows上使用这个nginx配置但是报错2025/07/14 16:17:59 [emerg] 7236#7276: "server" directive is not allowed here in D:\ZrWrjServer\nginx-1.18.0/conf/nginx.conf:1
最新发布
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值