nginx基于用户的访问控制

ngx_http_auth_basic_module模块

 

实现基于用户的访问控制,使用basic机制进行用户认证
  auth_basic string | off;
  auth_basic_user_file file;

  Default: —
  Context: http, server, location, limit_except


  location /admin/ {
    auth_basic "Admin Area";
    auth_basic_user_file /etc/nginx/.ngxpasswd;
  }
用户口令文件:
1、明文文本:格式name:password:comment
2、加密文本:由htpasswd命令实现,httpd-tools包所提供

示例:

修改配置:
[root@centos7.6 conf.d]# vi /etc/nginx/con.d/test.conf
server {
        listen 80;
        server_name www.magedu.tech;
        root /opt/testdir/;
        limit_rate 2k;

        location / {
            limit_except GET {
                allow 10.0.0.126;
                deny all;
            }
        }

        location /admin {
                auth_basic "admin area";
                auth_basic_user_file /etc/nginx/conf.d/.nginxpasswd;
        }
        location /image {
        alias /opt/image/;
        try_files $uri $uri/tree.jpg =404;
        }
        error_page 404 =200 /404.html;
        location = /test.html {
        }
}
创建默认index
[root@centos7.6 conf.d]# mkdir /opt/testdir/admin
[root@centos7.6 conf.d]# echo /opt/testdir/admin/index.html >/opt/testdir/admin/index.html
htpasswd创建安全访问用户:
[root@centos7.6 conf.d]# htpasswd -c .nginxpasswd bob                     #-c  第一次创建
New password: 
Re-type new password: 
Adding password for user bob
[root@centos7.6 conf.d]# htpasswd -b .nginxpasswd alice centos            #-b  追加
Adding password for user alice

验证:

 

 

 

 

posted @ 2024-08-29 09:03  起点·漫步前行  阅读(21)  评论(0)    收藏  举报