提交 279bec0a authored 作者: AFA_ZHANRONGZHEN's avatar AFA_ZHANRONGZHEN

修改nginx配置文件和本地相同

上级 cc65fa31
worker_processes auto;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
include mime.types;
default_type application/octet-stream;
log_format main_with_time '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time';
'"$http_user_agent" $request_time';
lua_package_path "/usr/local/openresty/lualib/?.lua;;";
lua_shared_dict file_check_cache 10m;
......@@ -18,12 +18,12 @@ http {
server {
listen 8080;
server_name localhost;
access_log logs/access.log main_with_time;
access_log logs/access.log main_with_time;
error_log logs/error.log warn;
client_max_body_size 1g;
client_body_buffer_size 20m;
gzip on;
gzip_comp_level 6;
gzip_types
......@@ -34,26 +34,34 @@ http {
application/javascript
application/xml
application/x-javascript
application/octet-stream # 可选:用于 JS/CSS 打包文件
application/octet-stream
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";
#location ~ ^/static/(.+)$ {
location /fileserver/ {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, HEAD, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
add_header 'Access-Control-Max-Age' 1728000 always;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
content_by_lua_block {
ngx.header["Access-Control-Allow-Origin"] = "*"
ngx.header["Access-Control-Allow-Methods"] = "GET, HEAD, OPTIONS"
ngx.header["Access-Control-Allow-Headers"] = "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization"
local filename = string.match(ngx.var.uri, "([^/]+)$")
if filename == "2270549058.css" then
ngx.status = ngx.HTTP_NOT_FOUND
......@@ -61,8 +69,8 @@ http {
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 current_uri = ngx.var.uri
local current_uri = ngx.var.uri
local clean_uri = current_uri
local prefix = "/fileserver"
if string.sub(current_uri, 1, #prefix) == prefix then
......@@ -70,17 +78,17 @@ http {
end
local filepath
-- 判断是蒙城网站的验证码或者锐速云验证码
if filename == "1710935454" or filename == "3568918852.aspx" then
filepath = ngx.var.root_path .. clean_uri
else
filepath = ngx.var.root_path .. "/resources/default/" .. filename
end
local max_retries = 30 -- 重试次数(含首次检查)
local retry_delay = 0.5 -- 重试延迟(秒)
local max_retries = 30
local retry_delay = 0.5
ngx.log(ngx.WARN, "AFA request filpath: ", filepath, " filename: ", filename)
ngx.log(ngx.WARN, "AFA request filpath: ", filepath, " filename: ",filename)
local mime_types = {
["html"] = "text/html",
["css"] = "text/css",
......@@ -88,40 +96,47 @@ http {
["js"] = "application/javascript",
["json"] = "application/json",
["png"] = "image/png",
["apng"] = "image/apng",
["apng"] = "image/apng",
["jpg"] = "image/jpeg",
["jpeg"] = "image/jpeg",
["jfif"] = "image/jpeg",
["jpeg"] = "image/jpeg",
["jfif"] = "image/jpeg",
["gif"] = "image/gif",
["avif"] = "image/avif",
["txt"] = "text/plain",
["xml"] = "application/xml",
["pdf"] = "application/pdf",
["ashx"] = "text/css", --https://www.jingtian.com/museum/ 适配ashx文件类型
-- 添加你需要的类型
["ashx"] = "text/css",
["woff2"] = "font/woff2",
["woff"] = "font/woff",
["ttf"] = "font/ttf",
}
local function get_mime_type(ext)
return mime_types[ext] or "application/octet-stream"
end
local lfs = require('lfs_ffi')
--判断文件是否存在,不存在延迟retry_delay秒后再探测,重试次数max_retries
local attributes, err
local cache = ngx.shared.file_check_cache
local key = "exist:" .. filepath
local value
for attempt = 1, max_retries do
for attempt = 1, max_retries do
value = cache:get(key)
if value then
break
if value then
break
end
attributes, err = lfs.attributes(filepath)
if attributes then
cache:set(key, true, 600) -- 缓存600秒
attributes, err = lfs.attributes(filepath)
if attributes then
cache:set(key, true, 600)
break
end
ngx.log(ngx.WARN, "File not found on attempt " .. attempt .. ", retrying in " .. retry_delay .. "s: ", filepath)
ngx.sleep(retry_delay) -- 极短等待
ngx.sleep(retry_delay)
end
value = cache:get(key)
if not value then
ngx.log(ngx.WARN, "File not found: ", filepath)
......@@ -130,11 +145,10 @@ http {
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
if not attributes then
-- 缓存命中但attributes未设置,需重新获取文件属性
attributes, err = lfs.attributes(filepath)
if not attributes then
-- 文件可能已被删除,清除缓存并返回404
cache:delete(key)
ngx.log(ngx.WARN, "File cached as existing but not found: ", filepath)
ngx.header["Content-Type"] = "text/html; charset=utf-8"
......@@ -143,38 +157,18 @@ http {
return ngx.exit(ngx.HTTP_NOT_FOUND)
end
end
-- 判断是否有扩展名
local ext = string.match(filename, "%.([^%.]+)$")
local mime_type
local detect = require "detect_mime_by_magic"
if ext then
-- 如果是锐速云验证码文件3568918852.aspx,返回后删除文件
if filename == "3568918852.aspx" then
local file = io.open(filepath, "rb")
if file then
local content = file:read("*all")
file:close()
local mime_types = {
["html"] = "text/html",
["aspx"] = "text/html",
["css"] = "text/css",
["cssx"] = "text/css",
["js"] = "application/javascript",
["json"] = "application/json",
["png"] = "image/png",
["apng"] = "image/apng",
["jpg"] = "image/jpeg",
["jpeg"] = "image/jpeg",
["jfif"] = "image/jpeg",
["gif"] = "image/gif",
["avif"] = "image/avif",
["txt"] = "text/plain",
["xml"] = "application/xml",
["pdf"] = "application/pdf",
["ashx"] = "text/css", --https://www.jingtian.com/museum/ 适配ashx文件类型
}
mime_type = mime_types[ext] or "application/octet-stream"
ngx.header["Content-Type"] = mime_type
......@@ -184,12 +178,12 @@ http {
ngx.header["Expires"] = "0"
ngx.say(content)
local ok, err = os.remove(filepath)
local ok, remove_err = os.remove(filepath)
if ok then
cache:delete(key)
ngx.log(ngx.WARN, "File deleted after serving: ", filepath)
else
ngx.log(ngx.WARN, "Failed to delete file: ", filepath, ", error: ", err)
ngx.log(ngx.WARN, "Failed to delete file: ", filepath, ", error: ", remove_err)
end
return ngx.exit(ngx.HTTP_OK)
......@@ -198,17 +192,16 @@ http {
return ngx.exit(500)
end
end
-- 有扩展名,用download下载
return ngx.exec("/download/" .. filename)
else
-- 无扩展名:用 Lua 魔数检测
mime_type = detect(filepath)
end
-- 设置响应头
ngx.header["Content-Type"] = mime_type
ngx.header["Content-Length"] = attributes.size
ngx.header["Cache-Control"] = "max-age=86400"
local file = io.open(filepath, "rb")
if file then
ngx.say(file:read("*all"))
......@@ -219,7 +212,7 @@ http {
end
}
}
location /download/ {
internal;
alias /var/www/html/resources/default/;
......@@ -268,12 +261,12 @@ http {
local file = nil
local filepath = nil
local file_size = 0
local upload_dir =/var/www/html/upload
local upload_dir = "/var/www/html/upload/"
while true do
local typ, res, err = form:read()
local typ, res, read_err = form:read()
if not typ then
ngx.log(ngx.ERR, "failed to read: ", err)
ngx.log(ngx.ERR, "failed to read: ", read_err)
break
end
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论