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

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

上级 cc65fa31
worker_processes auto;
worker_processes auto;
events { events {
worker_connections 1024; worker_connections 1024;
} }
http { http {
include mime.types; include mime.types;
default_type application/octet-stream; default_type application/octet-stream;
log_format main_with_time '$remote_addr - $remote_user [$time_local] "$request" ' log_format main_with_time '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" ' '$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_package_path "/usr/local/openresty/lualib/?.lua;;";
lua_shared_dict file_check_cache 10m; lua_shared_dict file_check_cache 10m;
...@@ -18,12 +18,12 @@ http { ...@@ -18,12 +18,12 @@ http {
server { server {
listen 8080; listen 8080;
server_name localhost; 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; error_log logs/error.log warn;
client_max_body_size 1g; client_max_body_size 1g;
client_body_buffer_size 20m; client_body_buffer_size 20m;
gzip on; gzip on;
gzip_comp_level 6; gzip_comp_level 6;
gzip_types gzip_types
...@@ -34,26 +34,34 @@ http { ...@@ -34,26 +34,34 @@ http {
application/javascript application/javascript
application/xml application/xml
application/x-javascript application/x-javascript
application/octet-stream # 可选:用于 JS/CSS 打包文件 application/octet-stream
image/svg+xml image/svg+xml
; ;
gzip_min_length 1k; gzip_min_length 1k;
# 启用压缩的 HTTP 版本
gzip_http_version 1.1; gzip_http_version 1.1;
# 不对 IE6 等老浏览器压缩(可选)
gzip_disable "MSIE [1-6]\."; gzip_disable "MSIE [1-6]\.";
# Vary Header,告诉代理服务器根据 Accept-Encoding 缓存不同版本
add_header Vary Accept-Encoding; add_header Vary Accept-Encoding;
# 静态资源根目录
set $root_path "/var/www/html"; set $root_path "/var/www/html";
#location ~ ^/static/(.+)$ {
location /fileserver/ { 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 { 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, "([^/]+)$") local filename = string.match(ngx.var.uri, "([^/]+)$")
if filename == "2270549058.css" then if filename == "2270549058.css" then
ngx.status = ngx.HTTP_NOT_FOUND ngx.status = ngx.HTTP_NOT_FOUND
...@@ -61,8 +69,8 @@ http { ...@@ -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>") 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) return ngx.exit(ngx.HTTP_NOT_FOUND)
end end
local current_uri = ngx.var.uri
local current_uri = ngx.var.uri
local clean_uri = current_uri local clean_uri = current_uri
local prefix = "/fileserver" local prefix = "/fileserver"
if string.sub(current_uri, 1, #prefix) == prefix then if string.sub(current_uri, 1, #prefix) == prefix then
...@@ -70,17 +78,17 @@ http { ...@@ -70,17 +78,17 @@ http {
end end
local filepath local filepath
-- 判断是蒙城网站的验证码或者锐速云验证码
if filename == "1710935454" or filename == "3568918852.aspx" then if filename == "1710935454" or filename == "3568918852.aspx" then
filepath = ngx.var.root_path .. clean_uri filepath = ngx.var.root_path .. clean_uri
else else
filepath = ngx.var.root_path .. "/resources/default/" .. filename filepath = ngx.var.root_path .. "/resources/default/" .. filename
end end
local max_retries = 30 -- 重试次数(含首次检查) local max_retries = 30
local retry_delay = 0.5 -- 重试延迟(秒) 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 = { local mime_types = {
["html"] = "text/html", ["html"] = "text/html",
["css"] = "text/css", ["css"] = "text/css",
...@@ -88,40 +96,47 @@ http { ...@@ -88,40 +96,47 @@ http {
["js"] = "application/javascript", ["js"] = "application/javascript",
["json"] = "application/json", ["json"] = "application/json",
["png"] = "image/png", ["png"] = "image/png",
["apng"] = "image/apng", ["apng"] = "image/apng",
["jpg"] = "image/jpeg", ["jpg"] = "image/jpeg",
["jpeg"] = "image/jpeg", ["jpeg"] = "image/jpeg",
["jfif"] = "image/jpeg", ["jfif"] = "image/jpeg",
["gif"] = "image/gif", ["gif"] = "image/gif",
["avif"] = "image/avif", ["avif"] = "image/avif",
["txt"] = "text/plain", ["txt"] = "text/plain",
["xml"] = "application/xml", ["xml"] = "application/xml",
["pdf"] = "application/pdf", ["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) local function get_mime_type(ext)
return mime_types[ext] or "application/octet-stream" return mime_types[ext] or "application/octet-stream"
end end
local lfs = require('lfs_ffi') local lfs = require('lfs_ffi')
--判断文件是否存在,不存在延迟retry_delay秒后再探测,重试次数max_retries
local attributes, err local attributes, err
local cache = ngx.shared.file_check_cache local cache = ngx.shared.file_check_cache
local key = "exist:" .. filepath local key = "exist:" .. filepath
local value local value
for attempt = 1, max_retries do
for attempt = 1, max_retries do
value = cache:get(key) value = cache:get(key)
if value then if value then
break break
end end
attributes, err = lfs.attributes(filepath)
if attributes then attributes, err = lfs.attributes(filepath)
cache:set(key, true, 600) -- 缓存600秒 if attributes then
cache:set(key, true, 600)
break break
end end
ngx.log(ngx.WARN, "File not found on attempt " .. attempt .. ", retrying in " .. retry_delay .. "s: ", filepath) 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 end
value = cache:get(key) value = cache:get(key)
if not value then if not value then
ngx.log(ngx.WARN, "File not found: ", filepath) ngx.log(ngx.WARN, "File not found: ", filepath)
...@@ -130,11 +145,10 @@ http { ...@@ -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>") 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) return ngx.exit(ngx.HTTP_NOT_FOUND)
end end
if not attributes then if not attributes then
-- 缓存命中但attributes未设置,需重新获取文件属性
attributes, err = lfs.attributes(filepath) attributes, err = lfs.attributes(filepath)
if not attributes then if not attributes then
-- 文件可能已被删除,清除缓存并返回404
cache:delete(key) cache:delete(key)
ngx.log(ngx.WARN, "File cached as existing but not found: ", filepath) ngx.log(ngx.WARN, "File cached as existing but not found: ", filepath)
ngx.header["Content-Type"] = "text/html; charset=utf-8" ngx.header["Content-Type"] = "text/html; charset=utf-8"
...@@ -143,38 +157,18 @@ http { ...@@ -143,38 +157,18 @@ http {
return ngx.exit(ngx.HTTP_NOT_FOUND) return ngx.exit(ngx.HTTP_NOT_FOUND)
end end
end end
-- 判断是否有扩展名
local ext = string.match(filename, "%.([^%.]+)$") local ext = string.match(filename, "%.([^%.]+)$")
local mime_type local mime_type
local detect = require "detect_mime_by_magic" local detect = require "detect_mime_by_magic"
if ext then if ext then
-- 如果是锐速云验证码文件3568918852.aspx,返回后删除文件
if filename == "3568918852.aspx" then if filename == "3568918852.aspx" then
local file = io.open(filepath, "rb") local file = io.open(filepath, "rb")
if file then if file then
local content = file:read("*all") local content = file:read("*all")
file:close() 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" mime_type = mime_types[ext] or "application/octet-stream"
ngx.header["Content-Type"] = mime_type ngx.header["Content-Type"] = mime_type
...@@ -184,12 +178,12 @@ http { ...@@ -184,12 +178,12 @@ http {
ngx.header["Expires"] = "0" ngx.header["Expires"] = "0"
ngx.say(content) ngx.say(content)
local ok, err = os.remove(filepath) local ok, remove_err = os.remove(filepath)
if ok then if ok then
cache:delete(key) cache:delete(key)
ngx.log(ngx.WARN, "File deleted after serving: ", filepath) ngx.log(ngx.WARN, "File deleted after serving: ", filepath)
else 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 end
return ngx.exit(ngx.HTTP_OK) return ngx.exit(ngx.HTTP_OK)
...@@ -198,17 +192,16 @@ http { ...@@ -198,17 +192,16 @@ http {
return ngx.exit(500) return ngx.exit(500)
end end
end end
-- 有扩展名,用download下载
return ngx.exec("/download/" .. filename) return ngx.exec("/download/" .. filename)
else else
-- 无扩展名:用 Lua 魔数检测
mime_type = detect(filepath) mime_type = detect(filepath)
end end
-- 设置响应头
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"] = "max-age=86400" ngx.header["Cache-Control"] = "max-age=86400"
local file = io.open(filepath, "rb") local file = io.open(filepath, "rb")
if file then if file then
ngx.say(file:read("*all")) ngx.say(file:read("*all"))
...@@ -219,7 +212,7 @@ http { ...@@ -219,7 +212,7 @@ http {
end end
} }
} }
location /download/ { location /download/ {
internal; internal;
alias /var/www/html/resources/default/; alias /var/www/html/resources/default/;
...@@ -268,12 +261,12 @@ http { ...@@ -268,12 +261,12 @@ http {
local file = nil local file = nil
local filepath = nil local filepath = nil
local file_size = 0 local file_size = 0
local upload_dir =/var/www/html/upload local upload_dir = "/var/www/html/upload/"
while true do while true do
local typ, res, err = form:read() local typ, res, read_err = form:read()
if not typ then if not typ then
ngx.log(ngx.ERR, "failed to read: ", err) ngx.log(ngx.ERR, "failed to read: ", read_err)
break break
end end
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论