美文网首页
nginx auth_basic

nginx auth_basic

作者: DeanWang | 来源:发表于2022-03-08 22:53 被阅读0次

    nginx的简单鉴权
    使用nginx搭建的web服务,需要限制访问,但是构建用户系统又过于繁琐时,可考虑使用nginx的auth_basic进行简单鉴权

    安装htpasswd

    Debian系统一般安装apache后已经带入,如果命令不存在尝试apt-get install apache2-utils命令安装

    htpasswd 创建用户

    # 新建password文件
    htpasswd -c /etc/nginx/db/passwd.db user1 
    # 新增用户
    htpasswd -b /etc/nginx/db/passwd.db user2 passwd2
    # 查看文件
    cat /etc/nginx/db/passwd.db
    # 会输出以下文本:
    user1:$apr1$o.nBjU0AVW/q1E/v6W02rC7wzMDl
    user2:$apr1$SzZrLnWM$hD2rC7wzMDlP3hnkb1
    
    # 删除用户
    htpasswd -D /etc/nginx/db/passwd.db user2
    

    nginx 配置

    location / { 
        auth_basic "auth-user";
        auth_basic_user_file /etc/nginx/db/passwd.db;
        # 其他配置
    }
    

    这个可以给swagger等页面做简单的鉴权。防止接口直接公开外露

    相关文章

      网友评论

          本文标题:nginx auth_basic

          本文链接:https://www.haomeiwen.com/subject/sxtarrtx.html