nginx用户认证配置( Basic HTTP authentication)

凉白开 Nginx4121,06811字数 1134阅读3分46秒阅读模式

ngx_http_auth_basic_module模块实现让访问着,只有输入正确的用户密码才允许访问web内容。web上的一些内容不想被其他人知道,但是又想让部分人看到。nginx的http auth模块以及Apache http auth都是很好的解决方案。

默认情况下nginx已经安装了ngx_http_auth_basic_module模块,如果不需要这个模块,可以加上 --without-http_auth_basic_module 。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

nginx basic auth指令

语法:     auth_basic string | off;
默认值:     auth_basic off;
配置段:     http, server, location, limit_except文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

默认表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

语法:     auth_basic_user_file file;
默认值:     —
配置段:     http, server, location, limit_except文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

用户密码文件,文件内容类似如下:文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

ttlsauser1:password1
ttlsauser2:password2:comment

nginx认证配置实例

server{
       server_name  www.ttlsa.com ttlsa.com;

        index index.html index.php;
        root /data/site/www.ttlsa.com;       

        location /
        {
                auth_basic "nginx basic http test for ttlsa.com";
                auth_basic_user_file conf/htpasswd; 
                autoindex on;
        }
}

备注:一定要注意auth_basic_user_file路径,否则会不厌其烦的出现403。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

生成密码文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

可以使用htpasswd,或者使用openssl文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

# printf "ttlsa:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd
# cat conf/htpasswd 
ttlsa:xyJkVhXGAZ8tM

账号:ttlsa
密码:123456文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

reload nginx文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

 # /usr/local/nginx-1.5.2/sbin/nginx -s reload

效果如下:文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

Apache

http_auth_basic_module

 文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

完成~文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/ 文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-basic-http-authentication/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 20/03/2015 01:00:31
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/nginx-basic-http-authentication/
评论  4  访客  4
    • kevin
      kevin 0

      printf “ttlsa:$(openssl passwd -crypt 123456)\n” >>conf/htpasswd 此命令生成的密码有随机加盐,每次生成的密码都不一样,请问博主是怎么解决的?

      • xxx
        xxx 9

        请教下nginx下限制操作系统语言的访问有没有办法
        不是限制浏览器语言

        • 卷毛就是我
          卷毛就是我 1

          basic auth终究不是个安全的方式

        评论已关闭!