nginx lua重置請求參數及常量備忘

本文主要講述一下nginx lua如何重置請求參數以及整理了幾類常量。nginx

重置請求參數

獲取請求參數

local strider = ngx.var.arg_strider
local strider = ngx.req.get_uri_args["strider"]

當請求uri中有多個同名參數時,ngx.var.arg_xx的作法是取第一個出現的值,ngx.req_get_uri_args["xx"]的作法是返回一個table,該table裏存放了該參數的全部值運維

重置參數

local args = ngx.req.get_uri_args()
args["userId"]="override value"
ngx.req.set_uri_args(args)

log級別常量

  • ngx.STDERR -- 標準輸出
  • ngx.EMERG -- 緊急報錯
  • ngx.ALERT -- 報警
  • ngx.CRIT -- 嚴重,系統故障,觸發運維告警系統
  • ngx.ERR -- 錯誤,業務不可恢復性錯誤
  • ngx.WARN -- 告警,業務中可忽略錯誤
  • ngx.NOTICE -- 提醒,業務比較重要信息
  • ngx.INFO -- 信息,業務瑣碎日誌信息,包含不一樣狀況判斷等
  • ngx.DEBUG -- 調試

使用實例ide

ngx.log(ngx.ERR,"error occur in ...")

http status code常量

1xx

  • ngx.HTTP_CONTINUE (100) (first added in the v0.9.20 release)
  • ngx.HTTP_SWITCHING_PROTOCOLS (101) (first added in the v0.9.20 release)

2xx

  • ngx.HTTP_OK (200)
  • ngx.HTTP_CREATED (201)
  • ngx.HTTP_ACCEPTED (202) (first added in the v0.9.20 release)
  • ngx.HTTP_NO_CONTENT (204) (first added in the v0.9.20 release)
  • ngx.HTTP_PARTIAL_CONTENT (206) (first added in the v0.9.20 release)

3xx

  • ngx.HTTP_SPECIAL_RESPONSE (300)
  • ngx.HTTP_MOVED_PERMANENTLY (301)
  • ngx.HTTP_MOVED_TEMPORARILY (302)
  • ngx.HTTP_SEE_OTHER (303)
  • ngx.HTTP_NOT_MODIFIED (304)
  • ngx.HTTP_TEMPORARY_REDIRECT (307) (first added in the v0.9.20 release)

4xx

  • ngx.HTTP_BAD_REQUEST (400)
  • ngx.HTTP_UNAUTHORIZED (401)
  • ngx.HTTP_PAYMENT_REQUIRED (402) (first added in the v0.9.20 release)
  • ngx.HTTP_FORBIDDEN (403)
  • ngx.HTTP_NOT_FOUND (404)
  • ngx.HTTP_NOT_ALLOWED (405)
  • ngx.HTTP_NOT_ACCEPTABLE (406) (first added in the v0.9.20 release)
  • ngx.HTTP_REQUEST_TIMEOUT (408) (first added in the v0.9.20 release)
  • ngx.HTTP_CONFLICT (409) (first added in the v0.9.20 release)
  • ngx.HTTP_GONE (410)
  • ngx.HTTP_UPGRADE_REQUIRED (426) (first added in the v0.9.20 release)
  • ngx.HTTP_TOO_MANY_REQUESTS (429) (first added in the v0.9.20 release)
  • ngx.HTTP_CLOSE (444) (first added in the v0.9.20 release)
  • ngx.HTTP_ILLEGAL (451) (first added in the v0.9.20 release)

5xx

  • ngx.HTTP_INTERNAL_SERVER_ERROR (500)
  • ngx.HTTP_METHOD_NOT_IMPLEMENTED (501)
  • ngx.HTTP_BAD_GATEWAY (502) (first added in the v0.9.20 release)
  • ngx.HTTP_SERVICE_UNAVAILABLE (503)
  • ngx.HTTP_GATEWAY_TIMEOUT (504) (first added in the v0.3.1rc38 release)
  • ngx.HTTP_VERSION_NOT_SUPPORTED (505) (first added in the v0.9.20 release)
  • ngx.HTTP_INSUFFICIENT_STORAGE (507) (first added in the v0.9.20 release)

主要用來設置http 返回狀態碼lua

使用實例調試

if token == nil then
    ngx.exit(ngx.HTTP_FORBIDDEN)
end

doc

相關文章
相關標籤/搜索