提交 d755c26f authored 作者: zhanrongzhen's avatar zhanrongzhen

静态资源加缓存

上级 c6446c31
...@@ -88,6 +88,14 @@ http { ...@@ -88,6 +88,14 @@ http {
return ngx.exit(ngx.HTTP_NOT_FOUND) return ngx.exit(ngx.HTTP_NOT_FOUND)
end end
local etag = string.format("%x-%x", attributes.mtime, attributes.size)
etag = '"' .. etag .. '"'
local if_none_match = ngx.req.get_headers()["If-None-Match"]
if if_none_match == etag then
ngx.status = 304
return
end
-- 2. 判断是否有扩展名 -- 2. 判断是否有扩展名
local ext = string.match(filename, "%.([^%.]+)$") local ext = string.match(filename, "%.([^%.]+)$")
local mime_type local mime_type
...@@ -105,9 +113,11 @@ http { ...@@ -105,9 +113,11 @@ http {
-- 3. 设置响应头 -- 3. 设置响应头
ngx.header["Content-Type"] = mime_type ngx.header["Content-Type"] = mime_type
ngx.header["Content-Length"] = attributes.size ngx.header["Content-Length"] = attributes.size
ngx.header["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0" ngx.header["Cache-Control"] = "max-age=86400"
ngx.header["Pragma"] = "no-cache" ngx.header["ETag"] = etag
ngx.header["Expires"] = "0" -- ngx.header["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
-- ngx.header["Pragma"] = "no-cache"
-- ngx.header["Expires"] = "0"
local file = io.open(filepath, "rb") local file = io.open(filepath, "rb")
if file then if file then
...@@ -123,9 +133,11 @@ http { ...@@ -123,9 +133,11 @@ http {
location /download/ { location /download/ {
internal; # 标记为内部 location,不允许外部直接访问 internal; # 标记为内部 location,不允许外部直接访问
alias /var/www/html/resources/default/; # 设置文件根目录 alias /var/www/html/resources/default/; # 设置文件根目录
add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0"; etag on;
add_header Pragma "no-cache"; #add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0";
add_header Expires "0"; #add_header Pragma "no-cache";
#add_header Expires "0";
add_header Cache-Control "max-age=86400";
sendfile on; sendfile on;
} }
# 未匹配的请求 # 未匹配的请求
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论