亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

深入了解Nginx auth_request

 更新時間:2024年12月26日 11:06:07   作者:zzhongcy  
描述:nginx-auth-request-module模塊用于實現(xiàn)權(quán)限控制攔截,通過配置可以實現(xiàn)多個站點之間的統(tǒng)一權(quán)限控制,下面就來介紹一下,感興趣的可以了解一下

模塊

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)文章

最新評論