前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用auth_request模块实现nginx端鉴权控制

使用auth_request模块实现nginx端鉴权控制

作者头像
用户1141560
发布2018-03-30 10:47:23
11.2K0
发布2018-03-30 10:47:23
举报
文章被收录于专栏:西安-晁州西安-晁州

使用auth_request模块实现nginx端鉴权控制

nginx-auth-request-module

该模块是nginx一个安装模块,使用配置都比较简单,只要作用是实现权限控制拦截作用。默认高版本nginx(比如1.12)已经默认安装该模块,下面介绍下使用该模块实现多个站点之间的统一权限控制。

这里用一个例子来说明下,如下例子是包含site1(对应web1)、site2(对应web2)、auth(20.131:7001)在内的三个应用项目,auth项目主要做权限拦截,比如jwt校验等,site1、site2分别为两个受保护的资源站点,只有auth授权通过后才能访问该站点。

实现上述要求nginx配置详情如下(nginx地址为20.198):

代码语言:javascript
复制
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对应的路由返回401 or 403时,会拦截请求直接nginx返回前台401 or 403信息; 2、auth_request对应的路由返回2xx状态码时,不会拦截请求,而是构建一个subrequest请求再去请求真实受保护资源的接口;

所以,基于此,auth模块只需要校验然后返回相应的状态码即可实现权限拦截操作,简单测试如下:

auth代码:

代码语言:javascript
复制
// 授权认证接口
  async auth() {
    console.log(Date.now());
    this.ctx.status = 200;
  }

  // 失败后的登录页面
  async login() {
    console.log('失败了........');
    this.ctx.body = {
      msg: '授权失败',
      code: 10001
    }
  }

这里的auth授权接口我们直接返回200,login是上述auth项目下配置的路由,用于授权失败后302至登录页面用的。

site1和site2代码相同,只罗列一个如下:

代码语言:javascript
复制
/* /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接口如下:

代码语言:javascript
复制
// 授权认证接口
  async auth() {
    console.log(Date.now());
    this.ctx.status = 401;
  }

  // 失败后的登录页面
  async login() {
    console.log('失败了........');
    this.ctx.body = {
      msg: '授权失败',
      code: 10001
    }
  }

这里将状态码改为了401,再次访问:http://192.168.20.198/api/web1/users,输出

这里可以看到,浏览器直接进行了302跳转,因为鉴权失败,直接重定向到登录页面了。

以上就是关于nginx-auth-request-module模块的基本操作及配置,多个项目下部署统一的权限接口时还是相当有用的。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-02-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用auth_request模块实现nginx端鉴权控制
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档