nginx认证访问

avatar 2020年3月11日18:19:26 评论 1,032 次浏览

我自己创建了一个nginx服务,不过我不想把我的nginx服务对外服务。原因这里涉及到一些个人隐私,所以我想设置一下认证,只有认证通过才可以浏览。这里使用的是htpasswd来做用户认证,先安装htpasswd:

[root@www.wulaoer.org ~]# yum -y install httpd-tools

htpasswd的相关参数:

-c 创建passwdfile.如果passwdfile 已经存在,那么它会重新写入并删去原有内容.
-n 不更新passwordfile,直接显示密码
-m 使用MD5加密(默认)
-d 使用CRYPT加密(默认)
-p 使用普通文本格式的密码
-s 使用SHA加密
-b 命令行中一并输入用户名和密码而不是根据提示输入密码,可以看见明文,不需要交互
-D 删除指定的用户

工具安装成功,下面开始创建用户和密码,这里我把用户和密码创建到home目录下的passwd文件中。

[root@www.wulaoer.org ~]# htpasswd -c /home/passwd wulaoer
New password: 
Re-type new password: 
Adding password for user wulaoer

输入两次密码,用户创建成功,我们看一下创建的密码是什么样的。

[root@www.wulaoer.org ~]# cat /home/passwd 
wulaoer:$apr1$.GQE3WCv$HZJf3gjWr0olBFv4VZMFL.

把创建的用户和密码加到需要认证的nginx中,打开需要认证的nginx配置文件

server
	{
		listen 80 default_server reuseport;
		#listen [::]:80 default_server ipv6only=on;
		server_name _;
		index index.html index.htm index.php;
		default_type 'text/html';
		charset utf-8;
		auth_basic "Welcome to www.wulaoer.org";    #这里是验证时的提示信息
		auth_basic_user_file /home/passwd;
..............................................

下面验证一下,是否需要输入用户名和密码,把刚刚创建的用户名和密码输入进去,是否能够访问站点的内容。

输入用户和密码后,我这里显示的验证成功,如果验证不通过肯定是用户和密码错误了,或者nginx配置的用户名和密码的地址错误。

avatar
  • 未经博主允许,禁止转载本博客任何内容,如需转载请留言,谢谢合作!!!
  • 转载注明:nginx认证访问 | 吴老二

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: