1. ngx.redirect(uri, status?) 301/302外部重定向
redirect 為外部重定向,有兩種形式:
rewrite ^ /foo? redirect; # nginx config 在 rewrite 階段重定向
等價於
return ngx.redirect('/foo'); -- Lua code
rewrite ^ /foo? permanent; # nginx config
等價於
return ngx.redirect('/foo', ngx.HTTP_MOVED_PERMANENTLY) -- Lua code
301 Moved Permanently
永久移動。請求的資源已被永久的移動到新URI,返回信息會包括新的URI,瀏覽器會自動定向到新URI。今后任何新的請求都應使用新的URI代替。
302 Found
臨時移動。與301類似。但資源只是臨時被移動。客戶端應繼續使用原有URI。
2. ngx.exec() 內部重定向
exec 會直接跳轉到另一個 location 做處理,但是不會 返回 調用 它的地方。
3. 子請求
res = ngx.location.capture( location,args ) 返回一個包含四個元素的Lua表 res.status res.header res.body res.truncated
ngx.location.capture 是非阻塞的,ngx.location.capture可以用來完成http請求,當調用使用此命令,當前請求會跳轉到location 去執行,最后會返回。