Nginx静态资源或者路径鉴权方式

 更新时间:2024年06月19日 10:55:06   作者:longxiaobai_WJ  
这篇文章主要介绍了Nginx静态资源或者路径鉴权方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

1. http_auth_request_module

# 版本说明 nginx 1.20.2, 默认自带 http_auth_request_module
# 模块查看验证 nginx -V (nginx -V| grep 'http_auth_request_module')

2. 鉴权接口准备

本文后端接口是通过Egg.js来实现的

实现代码取决于项目中使用何种后端语言

'use strict';

const Controller = require('egg').Controller;

class AuthController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, AuthController';
  }

  async login() {
    const { ctx, service } = this;
    const secrect = Date.now();
    const jwt_token = await service.actionToken.apply(secrect);

    ctx.helper.success();
    // ctx.app.jwt.verify(jwt_token, this.app.config.jwt.secret);

    ctx.body = {
      status: 200,
      secret: jwt_token,
    };
  }

  async authorize() {
    // 获取 POST 传递的参数
    console.log(this.ctx.params);
    // 获取 GET 传递的参数
    console.log(this.ctx.query);
    // 获取通过 cookie 传递的参数
    const isAuthToken = this.ctx.cookies.get('authorize', {
      signed: false,
    });
    console.log('isAuthToken log', isAuthToken);
    // 该代码为兼容 4-2 配置代码
    console.log('x-original-uri', this.ctx.headers['x-original-uri']);

    if (isAuthToken) {
      this.ctx.body = {
        status: 200,
        secret: 'jwt_token',
      };
    } else {
      this.ctx.status = 403;
    }
  }
}

module.exports = AuthController;

3. 修改Nginx配置

server {
    listen 8080;
    server_name localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;
    location = / {
        root index;
        index index.html index.htm;
    }

    location /eggjs {
        if ($http_cookie) {
            set $code_cookie $http_cookie;
        }
        if ($arg_code) {
            set $code_cookie "authorize=$arg_code; Path=/";
        }
        # 指定auth_request
        auth_request /auth;
        proxy_pass http://127.0.0.1:3000/static/index.html;
    }

    # 验证配置
    location /jwt {
        proxy_pass http://localhost:3000/authorize;
    }

    location /scripts {
        proxy_pass http://127.0.0.1:3000/static/scripts/;
    }

    location = /auth {
        internal;
        # $http_cookie
        proxy_set_header Cookie "$code_cookie";

        # 鉴权服务器的地址
        proxy_pass http://localhost:3000/authorize;

        # proxy_pass_request_body off;
        # proxy_set_header Content-Length "";
        # proxy_set_header X-Original-URI $query_string;
        # proxy_set_header X-Original-URI $request_uri;
    }

    #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 html;
    }
}

4. 配置说明

指定auth_request

auth_request /auth;

传递完整的原始请求URI

  • 由于身份验证子请求将丢弃请求体,可以通过以下配置传递信息(本文未使用该方式,故皆已注释);
# proxy_pass_request_body off;
# proxy_set_header Content-Length "";
# proxy_set_header X-Original-URI $query_string;
# proxy_set_header X-Original-URI $request_uri;

传递校验信息

# URL: http://localhost:8080/eggjs?code=202304
# 设置变量 $code_cookie,以便后续使用,此处配置兼容 cookie 与 query 两种写法
if ($http_cookie) {
    set $code_cookie $http_cookie;
}
if ($arg_code) {
    # $arg_code 与 URL上的参数code相对应
    # 即 secrect=202304 应对应 $arg_secrect
    set $code_cookie "authorize=$arg_code; Path=/";
}

# 在此处使用变量 $code_cookie
proxy_set_header Cookie "$code_cookie";

Extra Config Notes

以下配置,本文尚未验证;

location /eggjs {
    auth_request /auth;
    auth_request_set $auth_status $upstream_status;
    if ($auth_status = "403") {
        return 403;
    }
    proxy_pass http://127.0.0.1:3000/static/index.html;
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Nginx主机域名配置实现

    Nginx主机域名配置实现

    本文主要介绍了Nginx主机域名配置实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • 详解如何部署H5游戏到nginx服务器

    详解如何部署H5游戏到nginx服务器

    这篇文章主要介绍了详解如何部署H5游戏到nginx服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 服务器报错nginx 502 Bad Gateway的原因及如何解决详解

    服务器报错nginx 502 Bad Gateway的原因及如何解决详解

    项目启动时莫名其妙网站访问不了,502 Bad Gateway,下面这篇文章主要给大家介绍了关于服务器报错nginx 502 Bad Gateway的原因及如何解决的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-06-06
  • Nginx配置SSL自签名证书的方法

    Nginx配置SSL自签名证书的方法

    这篇文章主要介绍了Nginx配置SSL自签名证书的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • nginx配置静态资源的访问的实现

    nginx配置静态资源的访问的实现

    本文主要介绍了nginx配置静态资源的访问的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-05-05
  • Windows安装nginx1.10.1反向代理访问IIS网站

    Windows安装nginx1.10.1反向代理访问IIS网站

    这篇文章主要为大家详细介绍了Windows安装nginx1.10.1反向代理访问IIS网站的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Nginx使用mirror指令实现接口复制

    Nginx使用mirror指令实现接口复制

    Nginx中使用mirro指令可以方便地实现接口请求的复制,这个功能非常适合用于流量监控、数据收集或负载均衡,下面我们就来看看具体的用法吧
    2024-10-10
  • Nginx日志打印自定义请求头的实战

    Nginx日志打印自定义请求头的实战

    nginx的日志可以打印很多内容,但是有时候自定义的请求头该怎么打印呢,本文就来介绍一下,感兴趣的可以了解一下
    2024-12-12
  • 查看nginx日志的实现

    查看nginx日志的实现

    在 Nginx 中,查看日志是诊断问题和监控 Nginx 服务状态的重要手段,本文主要介绍了查看nginx日志的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • windows下Nginx多域名简单配置教程

    windows下Nginx多域名简单配置教程

    这篇文章主要为大家详细介绍了windows下Nginx多域名简单配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07

最新评论