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

1 篇文章 0 订阅

1、介绍

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

2、示例

  • ngx.header.content_type = "application/json;charset=utf8" 控制返回数据的类型
  • ngx.say用来返回数据
  • ngx.exit退出并返回状态码
location ~*\/api/v4/(objects|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)
	';

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值