小知识:Nginx服务器中为网站或目录添加认证密码的配置详解

nginx可以为网站或目录甚至特定的文件设置密码认证。密码必须是crypt加密的。可以用apache的htpasswd来创建密码。

格式为:

?
1
htpasswd -b -c site_pass username password

site_pass为密码文件。放在同nginx配置文件同一目录下,当然你也可以放在其它目录下,那在nginx的配置文件中就要写明绝对地址或相对当前目录的地址。

如果你输入htpasswd命令提示没有找到命令时,你需要安装httpd。如果是centos可以执行如下来安装,

?
1
yum install httpd

如果你不想安装httpd的话,可以使用perl脚本来实现(代码如下:)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#! /usr/bin/perl -w 
#filename: add_ftp_user.pl 
use strict; 
print “#example: user:passwd\n”; 
while (<STDIN>) { 
exit if ($_ =~/^\n/); 
chomp; 
(my $user, my $pass) = split /:/, $_, 2; 
my $crypt = crypt $pass, $1$ . gensalt(8); 
print “$user:$crypt\n”; 
sub gensalt { 
my $count = shift; 
my @salt = (., /, 0 .. 9, A .. Z, a .. z); 
my $s; 
$s .= $salt[rand @salt] for (1 .. $count); 
return $s; 
}
为脚本赋予可执行权限:
?
1
chmod o+x add_user.pl

脚本使用方法:

?
1
2
./add_user.pl
user:password

把生成的用户名密码粘贴到/usr/local/nginx/conf/vhost/nginx_passwd文件中即可

如果是为了给网站加上认证,可以直接将认证语句写在nginx的配置server段中。

如果是为了给目录加上认证,就需要写成目录形式了。同时,还要在目录中加上php的执行,否则php就会被下载而不执行了。

例如:基于整个网站的认证,auth_basic在php解释之前。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server 
listen 80; 
server_name www.tuohang.net tuohang.net; 
root /www/tuohang.net; 
index index.html index.htm index.php; 
auth_basic “input you user name and password”; 
auth_basic_user_file /usr/local/nginx/conf/vhost/nginx_passwd; 
location ~ .php$ 
fastcgi_pass 127.0.0.1:9000; 
fastcgi_index index.php; 
include fastcgi_params; 
location ~ /\.ht 
deny all; 
access_log /logs/tuohang.net_access.log main; 
}

针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。auth_basic在嵌套的location之后。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
server 
listen 80; 
server_name www.tuohang.net tuohang.net; 
root /www/tuohang.net; 
index index.html index.htm index.php; 
location ~ ^/admin/.* 
location ~ \.php$ 
fastcgi_pass 127.0.0.1:9000; 
fastcgi_index index.php; 
include fastcgi_params; 
auth_basic “auth”; 
auth_basic_user_file /usr/local/nginx/conf/vhost/auth/admin.pass; 
location ~ .php$ 
fastcgi_pass 127.0.0.1:9000; 
fastcgi_index index.php; 
include fastcgi_params; 
location ~ /\.ht 
deny all; 
access_log /logs/tuohang.net_access.log main; 
}

这里有一个细节,就是location ~ ^/admin/.* {…} 保护admin目录下的所有文件。如果你只设了/admin/ 那么直接输入/admin/index.php还是可以访问并且运行的。 ^/admin/.* 意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。

htpasswd

既然是使用到了htpasswd,那么就顺带着介绍一下其基本使用。

htpasswd参数

-c 创建passwdfile.如果passwdfile 已经存在,那么它会重新写入并删去原有内容.

-n 不更新passwordfile,直接显示密码

-m 使用MD5加密(默认)

-d 使用CRYPT加密(默认)

-p 使用普通文本格式的密码

-s 使用SHA加密

-b 命令行中一并输入用户名和密码而不是根据提示输入密码,可以看见明文,不需要交互

-D 删除指定的用户

实例 1. 如何利用htpasswd命令添加用户?
?
1
2
3
4
# /usr/local/apache/bin/htpasswd -bc linuxeye_pd linuxeye_user linuxeye_password
Adding password for user linuxeye_user
# cat linuxeye_pd
linuxeye_user:$apr1$Mugpp3FE$zGsi7/JfQIhFXPlgqo/Wx/

生成当前目录下生成一个linuxeye_pd文件,用户名linuxeye_user,密码:linuxeye_password,默认采用MD5加密方式

2. 如何在原有密码文件中增加下一个用户?

?
1
2
3
4
5
# /usr/local/apache/bin/htpasswd -b linuxeye_pd linuxeye.com linuxeye.com
Adding password for user linuxeye.com
# cat linuxeye_pd
linuxeye_user:$apr1$Mugpp3FE$zGsi7/JfQIhFXPlgqo/Wx/
linuxeye.com:$apr1$/8EUOPYI$4MBxYpzotrSDcTTDZvTeT0

一定要去掉-c选项,否则覆盖密码文件再创建

3. 如何不更新密码文件,只显示加密后的用户名和密码?

?
1
2
3
4
5
6
7
# /usr/local/apache/bin/htpasswd -n linuxeye
New password:
Re-type new password:
linuxeye:$apr1$bZ6Gclc4$zKRap.0BADzZIxLoxpDNv0
# /usr/local/apache/bin/htpasswd -nb linuxeye linuxeye_password
linuxeye:$apr1$yvngdKGV$QrnlriJ.MxIu52Vmo.ROE1

4. 如何利用htpasswd命令删除用户名和密码?

?
1
2
3
4
# /usr/local/apache/bin/htpasswd -D linuxeye_pd linuxeye_user
Deleting password for user linuxeye_user
# cat linuxeye_pd
linuxeye.com:$apr1$/8EUOPYI$4MBxYpzotrSDcTTDZvTeT0

5. 如何利用htpasswd命令修改密码?

?
1
2
3
4
5
6
# /usr/local/apache/bin/htpasswd -D linuxeye_pd linuxeye.com
Deleting password for user linuxeye.com
# /usr/local/apache/bin/htpasswd -b linuxeye_pd linuxeye.com linuxeye_passwd
Adding password for user linuxeye.com
# cat linuxeye_pd
linuxeye.com:$apr1$74ZvB1vC$/b7ETmg8xhDPieYj0b0cE.
声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:把你的树莓派变成一个 HiFi 音乐系统

2023-5-2 3:01:53

建站知识

小知识:Linux VPS下SSH常用命令整理

2023-5-2 3:10:16

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索