Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
O
openresty-static-server
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
AFA_展荣臻
openresty-static-server
Commits
279bec0a
提交
279bec0a
authored
6月 26, 2026
作者:
AFA_ZHANRONGZHEN
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改nginx配置文件和本地相同
上级
cc65fa31
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
42 行增加
和
49 行删除
+42
-49
nginx.conf
nginx.conf
+42
-49
没有找到文件。
nginx.conf
浏览文件 @
279bec0a
worker_processes
auto
;
worker_processes
auto
;
events
{
worker_connections
1024
;
}
...
...
@@ -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",
...
...
@@ -97,31 +105,38 @@ http {
["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
value
=
cache:get(key)
if
value
then
break
end
attributes,
err
=
lfs.attributes(filepath)
if
attributes
then
cache:set(key,
true,
600
)
--
缓存600秒
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"))
...
...
@@ -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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论