Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
O
openresty-static-server
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
AFA_展荣臻
openresty-static-server
Commits
69681535
提交
69681535
authored
9月 24, 2025
作者:
zhanrongzhen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改nginx配置文件
上级
e7e6a442
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
47 行增加
和
40 行删除
+47
-40
nginx.conf
nginx.conf
+47
-40
没有找到文件。
nginx.conf
浏览文件 @
69681535
...
@@ -20,6 +20,29 @@ http {
...
@@ -20,6 +20,29 @@ http {
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
;
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"
;
set
$root_path
"/var/www/html"
;
...
@@ -31,14 +54,14 @@ http {
...
@@ -31,14 +54,14 @@ http {
local
current_uri
=
ngx.var.uri
local
current_uri
=
ngx.var.uri
--
判断是不是蒙城网站的验证码
--
判断是不是蒙城网站的验证码
local
filepath
local
filepath
if
base_fil
e
~
=
"1710935454"
then
--
如果不是蒙城验证码图片
if
filenam
e
~
=
"1710935454"
then
--
如果不是蒙城验证码图片
filepath
=
ngx.var.root_path
..
"/resources/default/"
..
filename
filepath
=
ngx.var.root_path
..
"/resources/default/"
..
filename
else
else
filepath
=
ngx.var.root_path
..
current_uri
filepath
=
ngx.var.root_path
..
current_uri
end
end
local
max_retries
=
1
0
--
重试次数(含首次检查)
local
max_retries
=
3
0
--
重试次数(含首次检查)
local
retry_delay
=
1
--
重试延迟(秒)
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
=
{
...
@@ -63,46 +86,39 @@ http {
...
@@ -63,46 +86,39 @@ http {
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
--判断文件是否存在,不存在延迟retry_delay秒后再探测,重试次数max_retries
local
attributes,
err
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)
attributes,
err
=
lfs.attributes(filepath)
if
value
then
break
if
attributes
then
break
--
文件存在,跳出循环
end
end
if
lfs.attributes(filepath)
then
--
如果文件不存在且不是最后一次尝试,则延迟后重试
cache:set(key,
true,
600
)
--
缓存600秒
if
attempt
<
max_retries
then
break
ngx.log(ngx.WARN,
"File
not
found
on
attempt
"
..
attempt
..
",
retrying
in
"
..
retry_delay
..
"s:
",
filepath)
ngx.sleep(retry_delay)
--
关键:延迟等待
end
end
ngx.log(ngx.WARN,
"File
not
found
on
attempt
"
..
attempt
..
",
retrying
in
"
..
retry_delay
..
"s:
",
filepath)
ngx.sleep(retry_delay)
--
极短等待
end
end
value
=
cache:get(key)
if
not
attributes
then
if
not
value
then
ngx.log(ngx.WARN,
"File
not
found:
",
filepath
,
",
error:
",
err
)
ngx.log(ngx.WARN,
"File
not
found:
",
filepath)
ngx.header["Content-Type"]
=
"text/html
;
charset=utf-8"
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>")
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
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
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
--
mime_type
=
get_mime_type(ext)
--
有扩展名,用download下载
--
有扩展名,用download下载
return
ngx.exec("/download/"
..
filename)
return
ngx.exec("/download/"
..
filename)
else
else
...
@@ -110,22 +126,17 @@ http {
...
@@ -110,22 +126,17 @@ http {
mime_type
=
detect(filepath)
mime_type
=
detect(filepath)
end
end
--
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"]
=
"max-age=86400"
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")
local
file
=
io.open(filepath,
"rb")
if
file
then
if
file
then
ngx.say(file:read("*all"))
ngx.say(file:read("*all"))
file:close()
file:close()
else
else
ngx.
status
=
500
ngx.
log(ngx.WARN,
"AFA
read
File
failed
:
",
filepath)
ngx.say("Failed
to
open
file"
)
return
ngx.exit(500
)
end
end
}
}
}
}
...
@@ -133,13 +144,8 @@ http {
...
@@ -133,13 +144,8 @@ http {
location
/download/
{
location
/download/
{
internal
;
# 标记为内部 location,不允许外部直接访问
internal
;
# 标记为内部 location,不允许外部直接访问
alias
/var/www/html/resources/default/
;
# 设置文件根目录
alias
/var/www/html/resources/default/
;
# 设置文件根目录
#etag on;
expires
1d
;
expires
1d
;
add_header
Accept-Ranges
bytes
;
#add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0";
#add_header Pragma "no-cache";
#add_header Expires "0";
sendfile
on
;
sendfile
on
;
}
}
# 未匹配的请求
# 未匹配的请求
...
@@ -148,3 +154,4 @@ http {
...
@@ -148,3 +154,4 @@ http {
}
}
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论