您的位置:首页 > 汽车 > 时评 > Openresty 中 ngx.exit(403)的时候,给403页面的body中传递一个参数展示出来

Openresty 中 ngx.exit(403)的时候,给403页面的body中传递一个参数展示出来

2025/3/9 8:12:06 来源:https://blog.csdn.net/xuanyuanjiaqi/article/details/141458208  浏览:    关键词:Openresty 中 ngx.exit(403)的时候,给403页面的body中传递一个参数展示出来

Openresty 中 ngx.exit(403)的时候,我想给403页面的body中传递一个参数展示出来

在 OpenResty 中,ngx.exit(403) 会立即终止请求并返回 HTTP 403 状态码,默认情况下不会带有自定义的响应体。要在返回 403 状态码时自定义响应体并传递参数,你可以采用以下方式:

方法:使用 ngx.say 输出自定义的响应体

你可以在调用 ngx.exit(403) 之前使用 ngx.say 来输出自定义的响应体内容。这样可以确保在返回 403 状态码时,响应体包含你想要展示的内容。

local message = "Access Denied. Please contact support with error code: 12345"-- 输出自定义的响应体内容
ngx.status = ngx.HTTP_FORBIDDEN
ngx.say(message)-- 确保响应立即生效并停止处理
return ngx.exit(ngx.HTTP_FORBIDDEN)

说明:

  1. ngx.status:设置 HTTP 响应状态码为 403(ngx.HTTP_FORBIDDEN)。
  2. ngx.say:输出响应体内容。在这种情况下,你可以输出任何字符串、HTML、JSON 等内容。
  3. ngx.exit:确保立即结束请求处理并发送响应。

示例:返回自定义的 HTML 页面

如果你想返回一个自定义的 HTML 页面并在页面中展示特定的参数,可以如下处理:

local error_code = "ERR12345"local html_content = [[
<html>
<head><title>403 Forbidden</title></head>
<body>
<h1>403 Forbidden</h1>
<p>Access Denied. Please contact support with error code: ]] .. error_code .. [[</p>
</body>
</html>
]]ngx.status = ngx.HTTP_FORBIDDEN
ngx.header["Content-Type"] = "text/html"
ngx.say(html_content)return ngx.exit(ngx.HTTP_FORBIDDEN)

说明:

  • ngx.header["Content-Type"]:设置响应的内容类型为 text/html
  • html_content:自定义的 HTML 内容,其中包括了你想传递的参数 error_code

总结

在 OpenResty 中,通过 ngx.say 输出内容并结合 ngx.exit(403),你可以实现返回 403 状态码时自定义响应体,并传递参数到前端展示。

将自定义的 HTML 页面内容抽离到外部文件或模板中管理

在 OpenResty 中,你可以将自定义的 HTML 页面内容抽离到外部文件中进行管理,然后在 Lua 脚本中读取并返回这些内容。这使得你的代码更加整洁和易于维护。

方法:从外部文件读取 HTML 模板

你可以将自定义的 HTML 内容存储在一个文件中,然后使用 Lua 的文件 I/O 功能读取该文件的内容,并将其作为响应体返回。

1. 创建 HTML 模板文件

假设你将自定义的 403 页面存储在 /usr/local/openresty/nginx/html/403.html 文件中:

<!-- /usr/local/openresty/nginx/html/403.html -->
<html>
<head><title>403 Forbidden</title></head>
<body>
<h1>403 Forbidden</h1>
<p>Access Denied. Please contact support with error code: {{error_code}}</p>
</body>
</html>

2. 在 Lua 脚本中读取并处理 HTML 模板

使用 Lua 代码读取 HTML 文件的内容,然后替换模板中的占位符,最后输出给客户端。

local function read_file(path)local file = io.open(path, "r")  -- 以只读模式打开文件if not file thenreturn nilendlocal content = file:read("*a")  -- 读取文件内容file:close()return content
endlocal error_code = "ERR12345"
local template_path = "/usr/local/openresty/nginx/html/403.html"
local html_content = read_file(template_path)if html_content then-- 替换占位符html_content = string.gsub(html_content, "{{error_code}}", error_code)-- 设置响应头和响应体ngx.status = ngx.HTTP_FORBIDDENngx.header["Content-Type"] = "text/html"ngx.say(html_content)
else-- 如果文件读取失败,返回一个简单的403消息ngx.status = ngx.HTTP_FORBIDDENngx.say("403 Forbidden - Template file not found")
endreturn ngx.exit(ngx.HTTP_FORBIDDEN)

3. 将代码添加到 OpenResty 配置中

确保将上述 Lua 代码添加到 OpenResty 的处理阶段,比如 content_by_lua_block 中。

server {listen 80;server_name example.com;location / {content_by_lua_block {-- 这里是你上面的 Lua 代码}}location /403 {content_by_lua_block {-- 也可以为特定路径返回403页面-- 这里是你上面的 Lua 代码}}
}

说明

  • read_file 函数:读取指定路径的文件内容。
  • string.gsub:将模板中的占位符 {{error_code}} 替换为实际的错误代码。
  • ngx.header["Content-Type"]:设置响应的内容类型为 text/html

优化建议

  1. 模板引擎:如果你的模板较复杂,可以使用 Lua 的模板引擎(例如 lua-resty-template)来处理模板文件和变量替换。
  2. 文件缓存:为了提高性能,可以将模板文件缓存起来,避免每次请求都重新读取文件。

a. 引入 Lua 模板引擎,如 lua-resty-template,优化模板处理。

b. 为其他错误状态码(如 404、500)创建类似的模板管理机制。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com