nginx用户认证配置

10 篇文章 0 订阅

nginx_http_auth_basic_module模块实现让访问着,只有输入正确的用户密码才允许访问web内容。web上的一些内容不想被其他人知道,但是又想让部分人看到。nginx的http auth模块以及Apache http auth都是很好的解决方案。
默认情况下nginx已经安装了ngx_http_auth_basic_module模块,如果不需要这个模块,可以加上 --without-http_auth_basic_module 。

nginx basic auth指令

语法: auth_basic string | off;
默认值: auth_basic off;
配置段: http, server, location, limit_except
默认表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示。
语法: auth_basic_user_file file;
默认值: —
配置段: http, server, location, limit_except
用户密码文件,文件内容类似如下:

ttlsauser1:password1
ttlsauser2:password2:comment 
nginx认证配置实例
server{
      server_name www.test.com;
 
       index index.html index.php;
       root/data/site/test;      
 
       location/
       {
          auth_basic"nginx basic http test for ttlsa.com";
          auth_basic_user_fileconf/htpasswd;
       }
}

注意:一定要注意auth_basic_user_file路径,否则会出现403。

生成密码
可以使用htpasswd,或者使用openssl

# printf "test_user:$(openssl passwd -crypt test123)\n" >>conf/htpasswd
# cat conf/htpasswd

或者用php代码生成

// 密码明文
$password = 'test123';
// 对密码进行加密
$password = crypt($password, base64_encode($password));
// 获得加密后的密码
echo $password;

test_user:dGz.qyLUvqZDc

最后重启nginx。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值