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

修改nginx配置文件

上级 e7e6a442
......@@ -20,6 +20,29 @@ http {
server_name localhost;
access_log logs/access.log main_with_time;
error_log logs/error.log warn;
gzip on;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/javascript
application/json
application/javascript
application/xml
application/x-javascript
application/octet-stream # 可选:用于 JS/CSS 打包文件
image/svg+xml
;
gzip_min_length 1k;
# 启用压缩的 HTTP 版本
gzip_http_version 1.1;
# 不对 IE6 等老浏览器压缩(可选)
gzip_disable "MSIE [1-6]\.";
# Vary Header,告诉代理服务器根据 Accept-Encoding 缓存不同版本
add_header Vary Accept-Encoding;
# 静态资源根目录
set $root_path "/var/www/html";
......@@ -31,14 +54,14 @@ http {
local current_uri = ngx.var.uri
-- 判断是不是蒙城网站的验证码
local filepath
if base_file ~= "1710935454" then -- 如果不是蒙城验证码图片
if filename ~= "1710935454" then -- 如果不是蒙城验证码图片
filepath = ngx.var.root_path .. "/resources/default/" .. filename
else
filepath = ngx.var.root_path .. current_uri
end
local max_retries = 10 -- 重试次数(含首次检查)
local retry_delay = 1 -- 重试延迟(秒)
local max_retries = 30 -- 重试次数(含首次检查)
local retry_delay = 0.5 -- 重试延迟(秒)
ngx.log(ngx.WARN, "AFA request filpath: ", filepath, " filename: ",filename)
local mime_types = {
......@@ -63,46 +86,39 @@ http {
return mime_types[ext] or "application/octet-stream"
end
local lfs = require('lfs_ffi')
-- 判断文件是否存在,不存在延迟retry_delay秒后再探测,重试次数max_retries
--判断文件是否存在,不存在延迟retry_delay秒后再探测,重试次数max_retries
local attributes, err
for attempt = 1, max_retries do
-- 获取文件属性
attributes, err = lfs.attributes(filepath)
if attributes then
break -- 文件存在,跳出循环
local cache = ngx.shared.file_check_cache
local key = "exist:" .. filepath
local value
for attempt = 1, max_retries do
value = cache:get(key)
if value then
break
end
-- 如果文件不存在且不是最后一次尝试,则延迟后重试
if attempt < max_retries then
ngx.log(ngx.WARN, "File not found on attempt " .. attempt .. ", retrying in " .. retry_delay .. "s: ", filepath)
ngx.sleep(retry_delay) -- 关键:延迟等待
if lfs.attributes(filepath) then
cache:set(key, true, 600) -- 缓存600秒
break
end
ngx.log(ngx.WARN, "File not found on attempt " .. attempt .. ", retrying in " .. retry_delay .. "s: ", filepath)
ngx.sleep(retry_delay) -- 极短等待
end
if not attributes then
ngx.log(ngx.WARN, "File not found: ", filepath, ", error: ", err)
value = cache:get(key)
if not value then
ngx.log(ngx.WARN, "File not found: ", filepath)
ngx.header["Content-Type"] = "text/html; charset=utf-8"
ngx.status = ngx.HTTP_NOT_FOUND
ngx.status = ngx.HTTP_NOT_FOUND
ngx.say("<html><body><h1>404 - File Not Found</h1><p>The requested file does not exist.</p></body></html>")
return ngx.exit(ngx.HTTP_NOT_FOUND)
end
-- local etag = attributes.mtime
-- 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. 判断是否有扩展名
-- 判断是否有扩展名
local ext = string.match(filename, "%.([^%.]+)$")
local mime_type
local detect = require "detect_mime_by_magic"
if ext then
-- mime_type = get_mime_type(ext)
-- 有扩展名,用download下载
return ngx.exec("/download/" .. filename)
else
......@@ -110,22 +126,17 @@ http {
mime_type = detect(filepath)
end
-- 3. 设置响应头
-- 设置响应头
ngx.header["Content-Type"] = mime_type
ngx.header["Content-Length"] = attributes.size
ngx.header["Cache-Control"] = "max-age=86400"
-- ngx.header["ETag"] = etag
-- 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")
if file then
ngx.say(file:read("*all"))
file:close()
else
ngx.status = 500
ngx.say("Failed to open file")
ngx.log(ngx.WARN, "AFA read File failed : ", filepath)
return ngx.exit(500)
end
}
}
......@@ -133,13 +144,8 @@ http {
location /download/ {
internal; # 标记为内部 location,不允许外部直接访问
alias /var/www/html/resources/default/; # 设置文件根目录
#etag on;
expires 1d;
#add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0";
#add_header Pragma "no-cache";
#add_header Expires "0";
add_header Accept-Ranges bytes;
sendfile on;
}
# 未匹配的请求
......@@ -148,3 +154,4 @@ http {
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论