先贴一下官方文档
The ngx_http_auth_request_module
module (1.5.4+) implements client authorization based on the result of a subrequest. If the subrequest returns a 2xx response code, the access is allowed. If it returns 401 or 403, the access is denied with the corresponding error code. Any other response code returned by the subrequest is considered an error.
For the 401 error, the client also receives the “WWW-Authenticate” header from the subrequest response.
This module is not built by default, it should be enabled with the --with-http_auth_request_module
configuration parameter.
--------------------------------------------渣英语路过------------------------------------------------
可以看到,跨域授权,如果授权通过,则返回2XX,若授权失败,则返回401或403,简单流程图如下
那么,ngx_http_auth_request_module是如何工作的呢?
流程一、可以看到我们的好奇宝宝开心的点开了一个链接,然后一个请求balabalabal的就到了nginx这里,这个时候,ngx_http_auth_request_module跳了出来,拦在了好奇宝宝的面前,开始验证他的通行证,pass,好奇宝宝开开心心的打开了他的三年高考五年模拟。
流程二、好奇宝宝又来到了另一个神奇的网站面前,然后小心翼翼的摸着大门,这时候,ngx_http_auth_request_module又窜了出来,开始验证好奇宝宝的通行证,refused,好奇宝宝看着门神403和401,在葫芦娃面前流下了眼泪。
可以看到,我们的ngx_http_auth_request_module
可以用来做门限卡控,特别是在跨域鉴权时,避免了彼此间的相互转发,极大的提高了效率。
那么如何配置呢,再次有请官方文档。
location /三年高考五年模拟/ { auth_request /study; ... }
location = /study { proxy_pass http://ilovestudy/ proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header X-Original-URI $request_uri; }
未完待续
参考文档
http://nginx.org/en/