nginx學習之auth_request


先貼一下官方文檔

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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM