欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入了解Nginx auth_request

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

模塊

nginx-auth-request-module

該模塊是nginx一個(gè)安裝模塊,使用配置都比較簡(jiǎn)單,只要作用是實(shí)現(xiàn)權(quán)限控制攔截作用。默認(rèn)高版本nginx(比如1.12)已經(jīng)默認(rèn)安裝該模塊,下面介紹下使用該模塊實(shí)現(xiàn)多個(gè)站點(diǎn)之間的統(tǒng)一權(quán)限控制。

例子1

這里用一個(gè)例子來說明下,如下例子是包含site1(對(duì)應(yīng)web1)、site2(對(duì)應(yīng)web2)、auth(20.131:7001)在內(nèi)的三個(gè)應(yīng)用項(xiàng)目,auth項(xiàng)目主要做權(quán)限攔截,比如jwt校驗(yàn)等,site1、site2分別為兩個(gè)受保護(hù)的資源站點(diǎn),只有auth授權(quán)通過后才能訪問該站點(diǎn)。

實(shí)現(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;
    }
}

配置好之后,要明白一點(diǎn),那就是nginx-auth-request-module模塊基本使用原理就是:

1、auth_request對(duì)應(yīng)的路由返回401 or 403時(shí),會(huì)攔截請(qǐng)求直接nginx返回前臺(tái)401 or 403信息;

2、auth_request對(duì)應(yīng)的路由返回2xx狀態(tài)碼時(shí),不會(huì)攔截請(qǐng)求,而是構(gòu)建一個(gè)subrequest請(qǐng)求再去請(qǐng)求真實(shí)受保護(hù)資源的接口;

所以,基于此,auth模塊只需要校驗(yàn)然后返回相應(yīng)的狀態(tài)碼即可實(shí)現(xiàn)權(quán)限攔截操作,簡(jiǎn)單測(cè)試如下:

auth代碼:

// 授權(quán)認(rè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項(xiàng)目下配置的路由,用于授權(quán)失敗后302至登錄頁面用的。

site1和site2代碼相同,只羅列一個(gè)如下:

/* /api/web1/users,如果是web2則為/api/web2/users */
router.all('/', function(req, res, next) {
  res.send('respond with a resource from web1');
});

這里只是簡(jiǎn)單渲染輸出一個(gè)字符串而已,測(cè)試如下:

瀏覽器訪問:http://192.168.20.198/api/web1/users,輸出:

改變auth接口如下:

// 授權(quán)認(rè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,輸出:

這里可以看到,瀏覽器直接進(jìn)行了302跳轉(zhuǎn),因?yàn)殍b權(quán)失敗,直接重定向到登錄頁面了。

以上就是關(guān)于nginx-auth-request-module模塊的基本操作及配置,多個(gè)項(xiàng)目下部署統(tǒng)一的權(quán)限接口時(shí)還是相當(dāng)有用的。

例子2

首先,確保Nginx已經(jīng)安裝并啟用了auth_request模塊。然后,編輯Nginx配置文件(通常是nginx.conf或某個(gè)虛擬主機(jī)配置文件)。

通過–with-http_auth_request_module添加auth_request模塊

http {
  # 定義認(rèn)證服務(wù)的邏輯
  server {
    listen 127.0.0.1:8080;
    location /auth {
      # 此處為簡(jiǎn)單示例,實(shí)際應(yīng)用中應(yīng)調(diào)用外部認(rèn)證服務(wù)
      if ($http_authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") {  # 假設(shè)認(rèn)證使用Basic Auth
        return 200;
      }
      return 401;
    }
  }

  server {
    listen 80;
    server_name example.com;

    location / {
      # 使用 auth_request 調(diào)用認(rèn)證服務(wù)
      auth_request /auth;

      # 處理認(rèn)證服務(wù)的響應(yīng)結(jié)果
      error_page 401 = @error401;
      error_page 403 = @error403;

      # 正常處理請(qǐng)求
      proxy_pass http://backend;
    }

    # 定義認(rèn)證失敗時(shí)的處理邏輯
    location @error401 {
      return 401 "Unauthorized";
    }

    location @error403 {
      return 403 "Forbidden";
    }

    # 認(rèn)證服務(wù)的代理設(shè)置
    location /auth {
      proxy_pass http://127.0.0.1:8080/auth;
      proxy_pass_request_body off; # 不代理請(qǐng)求體到認(rèn)證服務(wù)
      proxy_set_header Content-Length "";
      proxy_set_header X-Original-URI $request_uri;
    }
  }
}

配置說明

定義認(rèn)證服務(wù):

server {
  listen 127.0.0.1:8080;
  location /auth {
    if ($http_authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") {
      return 200;
    }
    return 401;
  }
}

這個(gè)server塊模擬了一個(gè)簡(jiǎn)單的認(rèn)證服務(wù),它監(jiān)聽127.0.0.1:8080,根據(jù)請(qǐng)求頭Authorization判斷用戶是否經(jīng)過認(rèn)證。在實(shí)際應(yīng)用中,這個(gè)應(yīng)該是一個(gè)調(diào)用外部服務(wù)的代理配置。

主站點(diǎn)配置:

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,在處理用戶請(qǐng)求前,先將請(qǐng)求發(fā)送到/auth進(jìn)行認(rèn)證。
  • error_page 401 = @error401;和error_page 403 = @error403;:定義認(rèn)證失敗時(shí)的處理邏輯,將401或403錯(cuò)誤重定向到相應(yīng)的處理塊。
  • proxy_pass http://backend;:成功認(rèn)證后,將請(qǐng)求代理到后端服務(wù)器。

認(rèn)證失敗處理:

location @error401 {
  return 401 "Unauthorized";
}

location @error403 {
  return 403 "Forbidden";
}

認(rèn)證失敗時(shí),根據(jù)實(shí)際情況返回401或403狀態(tài)碼,并附帶相應(yīng)的錯(cuò)誤信息。

測(cè)試與驗(yàn)證

啟動(dòng)Nginx,嘗試訪問http://example.com,并使用不同的Authorization頭部測(cè)試認(rèn)證行為。如果頭部包含正確的用戶名和密碼(在本例中為"Basic dXNlcm5hbWU6cGFzc3dvcmQ="),請(qǐng)求應(yīng)被允許訪問后端資源,否則返回相應(yīng)的錯(cuò)誤狀態(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論