1.1 介绍

ngx_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_excep

1.2 安装服务htpasswd

如果服务器上面没有htpasswd 可以使用yum install -y httpd 进行安装

[root@linux-node1 ~]# yum install httpd -y

1.3 添加用户生成密码文件

方法一:

[root@linux-node1 conf]# printf "test1:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd
[root@linux-node1 conf]# pwd
/etc/nginx/conf
[root@linux-node1 conf]# ll
total
-rw-r--r--  root root  Dec  : htpasswd
[root@linux-node1 conf]# cat htpasswd
syavingc:uu7RndQCc/s.Q
test1:myirmIogiWWeQ

方法二:

[root@linux-node1 ~]# htpasswd -c /etc/nginx/passwd.db syavingc ##syavingc 为用户名
New password: ##设置密码
Re-type new password: ##再次输入密码
Adding password for user syavingc

1.4 修改nginx配置文件

[root@linux-node1 vhost]# cat abc.conf
server {
listen ;
server_name abc..com;
location / {
root html/abc;
index index.php index.html index.htm;
auth_basic "nginx basic http test for syaving.com";
auth_basic_user_file /etc/nginx/passwd.db;
autoindex on;
}
access_log logs/access_abc.log main gzip buffer=32k flush=5s;
}

备注:一定要注意auth_basic_user_file路径,否则会不厌其烦的出现403。
生成密码
可以使用htpasswd,或者使用openssl

1.5 检测语法,重启nginx

[root@linux-node1 vhost]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux-node1 vhost]# nginx -s reload

1.6 输出网址 用户名 密码

用户名 syavingc
密码 1q2w3e$R

1.7 登陆之后的页面

1.8 添加一个用户

可以按照下面方式添加新用户,也可以使用上面的方法进行添加新用户

[root@linux-node1 nginx]# echo -n 'test2:' >> /etc/nginx/conf/htpasswd 

1.9 设置加密密码

[root@linux-node1 nginx]# openssl  >>/etc/nginx/conf/htpasswd

1.10 查看

[root@linux-node1 nginx]# cat /etc/nginx/conf/htpasswd
syavingc:uu7RndQCc/s.Q
test1:myirmIogiWWeQ
test2:5q4cr0mSI5VWU
[root@linux-node1 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux-node1 nginx]# nginx -s reload

最新文章

  1. IntelliJ IDEA 中集成使用git(2015年06月10日)
  2. jQuery 第一阶段 学习使用总结
  3. java开发webservice
  4. IntelliJ IDEA导出Java 可执行Jar包
  5. Hash散列算法 Time33算法
  6. 《javascript设计模式与开发实践》阅读笔记(10)—— 组合模式
  7. JSP Debug日志
  8. Java基础2:基本数据类型与常量池
  9. 安卓ListView行详细内容展示页编写和下拉刷新实现
  10. Ubuntu 18.04 根目录为啥只有 4G 大小
  11. Java并发编程(六)-- 同步块
  12. MyBatis第一个案例-----永远的HelloWorld 含所有代码
  13. js监听指定元素的css动画属性
  14. (转)Springboot邮件服务
  15. es5与es6继承思考
  16. php初进一个项目组,使用几个函数帮忙熟悉流程
  17. 样本服从正态分布,证明样本容量n乘样本方差与总体方差之比服从卡方分布x^2(n)
  18. IntelliJ - idea15.0.2 破解方法
  19. yii2 高级版新建一个应用(api应用为例子)
  20. Python——Django学习笔记

热门文章

  1. Flexbox兼容性语法汇总
  2. Scala 将BigDecimal转换为Long
  3. [转]2年SQL Server DBA调优方面总结
  4. CSS/JavaScript hacks,browserhacks使用
  5. 攻克了Cocoapods Undefined symbols for architecture _OBJC_CLASS_xxxx的问题,辛苦死我了,记录下之后有空在研究
  6. C-printf/sprintf/snprintf中的类型转换详解
  7. svn your working copy appears to be locked run cleanup to amend the situation
  8. tony_nginx的安装和配置
  9. Webpack 的 HtmlWebpackPlugin 如何控制某个 chunks 的 inject 位置?
  10. eclipse git拉取完代码后怎么maven构建