深入了解Nginx auth_request
模塊
nginx-auth-request-module
該模塊是nginx一個安裝模塊,使用配置都比較簡單,只要作用是實現(xiàn)權(quán)限控制攔截作用。默認高版本nginx(比如1.12)已經(jīng)默認安裝該模塊,下面介紹下使用該模塊實現(xiàn)多個站點之間的統(tǒng)一權(quán)限控制。
例子1
這里用一個例子來說明下,如下例子是包含site1(對應(yīng)web1)、site2(對應(yīng)web2)、auth(20.131:7001)在內(nèi)的三個應(yīng)用項目,auth項目主要做權(quán)限攔截,比如jwt校驗等,site1、site2分別為兩個受保護的資源站點,只有auth授權(quán)通過后才能訪問該站點。
實現(xiàn)上述要求nginx配置詳情如下(nginx地址為20.198):
upstream web1 { server 192.168.20.131:3000; } upstream web2 { server 192.168.20.131:3001; } server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location /api/web1 { auth_request /auth; error_page 401 = @error401; auth_request_set $user $upstream_http_x_forwarded_user; proxy_set_header X-Forwarded-User $user; proxy_pass http://web1; } location /api/web2 { auth_request /auth; error_page 401 = @error401; auth_request_set $user $upstream_http_x_forwarded_user; proxy_set_header X-Forwarded-User $user; proxy_pass http://web2; } location /auth { internal; proxy_set_header Host $host; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_pass http://192.168.20.131:7001/auth; } location @error401 { add_header Set-Cookie "NSREDIRECT=$scheme://$http_host$request_uri;Path=/"; return 302 http://192.168.20.131:7001/login; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
配置好之后,要明白一點,那就是nginx-auth-request-module模塊基本使用原理就是:
1、auth_request對應(yīng)的路由返回401 or 403時,會攔截請求直接nginx返回前臺401 or 403信息;
2、auth_request對應(yīng)的路由返回2xx狀態(tài)碼時,不會攔截請求,而是構(gòu)建一個subrequest請求再去請求真實受保護資源的接口;
所以,基于此,auth模塊只需要校驗然后返回相應(yīng)的狀態(tài)碼即可實現(xiàn)權(quán)限攔截操作,簡單測試如下:
auth代碼:
// 授權(quán)認證接口 async auth() { console.log(Date.now()); this.ctx.status = 200; } // 失敗后的登錄頁面 async login() { console.log('失敗了........'); this.ctx.body = { msg: '授權(quán)失敗', code: 10001 } }
這里的auth授權(quán)接口我們直接返回200,login是上述auth項目下配置的路由,用于授權(quán)失敗后302至登錄頁面用的。
site1和site2代碼相同,只羅列一個如下:
/* /api/web1/users,如果是web2則為/api/web2/users */ router.all('/', function(req, res, next) { res.send('respond with a resource from web1'); });
這里只是簡單渲染輸出一個字符串而已,測試如下:
瀏覽器訪問:http://192.168.20.198/api/web1/users,輸出:
改變auth接口如下:
// 授權(quán)認證接口 async auth() { console.log(Date.now()); this.ctx.status = 401; } // 失敗后的登錄頁面 async login() { console.log('失敗了........'); this.ctx.body = { msg: '授權(quán)失敗', code: 10001 } }
這里將狀態(tài)碼改為了401,再次訪問:http://192.168.20.198/api/web1/users,輸出:
這里可以看到,瀏覽器直接進行了302跳轉(zhuǎn),因為鑒權(quán)失敗,直接重定向到登錄頁面了。
以上就是關(guān)于nginx-auth-request-module模塊的基本操作及配置,多個項目下部署統(tǒng)一的權(quán)限接口時還是相當(dāng)有用的。
例子2
首先,確保Nginx已經(jīng)安裝并啟用了auth_request模塊。然后,編輯Nginx配置文件(通常是nginx.conf或某個虛擬主機配置文件)。
通過–with-http_auth_request_module添加auth_request模塊
http { # 定義認證服務(wù)的邏輯 server { listen 127.0.0.1:8080; location /auth { # 此處為簡單示例,實際應(yīng)用中應(yīng)調(diào)用外部認證服務(wù) if ($http_authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") { # 假設(shè)認證使用Basic Auth return 200; } return 401; } } server { listen 80; server_name example.com; location / { # 使用 auth_request 調(diào)用認證服務(wù) auth_request /auth; # 處理認證服務(wù)的響應(yīng)結(jié)果 error_page 401 = @error401; error_page 403 = @error403; # 正常處理請求 proxy_pass http://backend; } # 定義認證失敗時的處理邏輯 location @error401 { return 401 "Unauthorized"; } location @error403 { return 403 "Forbidden"; } # 認證服務(wù)的代理設(shè)置 location /auth { proxy_pass http://127.0.0.1:8080/auth; proxy_pass_request_body off; # 不代理請求體到認證服務(wù) proxy_set_header Content-Length ""; proxy_set_header X-Original-URI $request_uri; } } }
配置說明
定義認證服務(wù):
server { listen 127.0.0.1:8080; location /auth { if ($http_authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") { return 200; } return 401; } }
這個server塊模擬了一個簡單的認證服務(wù),它監(jiān)聽127.0.0.1:8080,根據(jù)請求頭Authorization判斷用戶是否經(jīng)過認證。在實際應(yīng)用中,這個應(yīng)該是一個調(diào)用外部服務(wù)的代理配置。
主站點配置:
server { listen 80; server_name example.com; location / { auth_request /auth; error_page 401 = @error401; error_page 403 = @error403; proxy_pass http://backend; } location @error401 { return 401 "Unauthorized"; } location @error403 { return 403 "Forbidden"; } location /auth { proxy_pass http://127.0.0.1:8080/auth; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header X-Original-URI $request_uri; } }
- auth_request /auth;:該指令告訴Nginx,在處理用戶請求前,先將請求發(fā)送到/auth進行認證。
- error_page 401 = @error401;和error_page 403 = @error403;:定義認證失敗時的處理邏輯,將401或403錯誤重定向到相應(yīng)的處理塊。
- proxy_pass http://backend;:成功認證后,將請求代理到后端服務(wù)器。
認證失敗處理:
location @error401 { return 401 "Unauthorized"; } location @error403 { return 403 "Forbidden"; }
認證失敗時,根據(jù)實際情況返回401或403狀態(tài)碼,并附帶相應(yīng)的錯誤信息。
測試與驗證
啟動Nginx,嘗試訪問http://example.com,并使用不同的Authorization頭部測試認證行為。如果頭部包含正確的用戶名和密碼(在本例中為"Basic dXNlcm5hbWU6cGFzc3dvcmQ="),請求應(yīng)被允許訪問后端資源,否則返回相應(yīng)的錯誤狀態(tài)碼。
例子3
upstream web1 { server 192.168.20.131:3000; } upstream web2 { server 192.168.20.131:3001; } location ^~ /session/ { charset utf-8; auth_request /session-backend-info/; auth_request_set $backend $upstream_http_backend; proxy_set_header Forwarded $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Port $remote_port; proxy_set_header Host $http_host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_pass $backend/$request_uri; }
$backend為web1、web2 upstream
到此這篇關(guān)于深入了解Nginx auth_request的文章就介紹到這了,更多相關(guān)Nginx auth_request內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決httpd占用80端口導(dǎo)致Nginx啟動失敗報錯的解決辦法
今天在建自己小網(wǎng)站時啟動Nginx時,發(fā)現(xiàn)其報下列錯誤,意思是因為80端口被占用導(dǎo)致Nginx啟動失敗,所以本文小編給大家介紹介紹如何解決解決httpd占用80端口導(dǎo)致Nginx啟動不成功報nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)2023-11-11PHP的Symfony和CodeIgniter框架的Nginx重寫規(guī)則配置
這篇文章主要介紹了PHP的Symfony和CodeIgniter框架的Nginx重寫規(guī)則配置,文中截取配置中關(guān)鍵的一些rewrite寫法進行講解,需要的朋友可以參考下2016-01-01nginx有哪些常規(guī)調(diào)優(yōu)手段詳解
性能調(diào)優(yōu)就是用更少的資源提供更好的服務(wù),成本利益最大化,下面這篇文章主要給大家介紹了關(guān)于nginx有哪些常規(guī)調(diào)優(yōu)手段的相關(guān)資料,需要的朋友可以參考下2023-01-01在Nginx中配置image filter模塊來實現(xiàn)動態(tài)生成縮略圖
這篇文章主要介紹了在Nginx中配置image filter模塊來實現(xiàn)動態(tài)生成縮略圖的方法,包括縮略圖尺寸的設(shè)置等方面的介紹,需要的朋友可以參考下2015-12-12比較完整的Nginx配置文件nginx.conf常用參數(shù)中文詳解
這篇文章主要介紹了比較完整的Nginx配置文件nginx.conf常用參數(shù)中文詳解,需要的朋友可以參考下2015-07-07解決Nginx + PHP(FastCGI)遇到的502 Bad Gateway錯誤
昨日,有朋友問我,他將Web服務(wù)器換成Nginx 0.6.31 + PHP 4.4.7(FastCGI)后,有時候訪問會出現(xiàn)“502 Bad Gateway”錯誤,如何解決。2009-10-10