Nginx 配置access_by_lua返回状态码和参数

1、介绍

access_by_lua是nginx权限访问控制的一个模块,通过配置相关参数可以达到访问应用权限控制的目的

2、示例

  • ngx.header.content_type = "application/json;charset=utf8" 控制返回数据的类型
  • ngx.say用来返回数据
  • ngx.exit退出并返回状态码
location ~*\/api/v4/(objects|threat_warning)(.*)/ {
	access_by_lua '
		local transfer_request_query = ngx.req.get_uri_args()
		local res = ngx.location.capture("/auth_get", { args = transfer_request_query })
		ngx.header.content_type = "application/json;charset=utf8"
		local json = require "cjson";

		if res.status == ngx.HTTP_OK then
			return
		end

		if res.status == 401 then
			ngx.status = res.status
			ngx.say(res.body)
			ngx.exit(401)
		end

		if res.status == 403 then
			ngx.say("haha 403")
			ngx.exit(ngx.HTTP_FORBIDDEN)
		end

		if res.status == 503 then
			ngx.exit(ngx.HTTP_METHOD_NOT_IMPLEMENTED)
		end

		ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
		--ngx.exit(507)
	';

猜你喜欢

转载自blog.csdn.net/u012089823/article/details/83622207