Nginx密码认证基础与密码文件准备
Nginx支持对网站、目录乃至特定文件实施密码认证保护,且密码必须采用crypt加密格式。用户可利用Apache提供的htpasswd工具来生成符合要求的密码文件。
配置的基本格式如下:
htpasswd -b -c site_pass username password
其中site_pass代表密码文件。该文件通常放置在Nginx配置文件所在的同一目录下;若选择其他路径,则需在Nginx配置文件中明确指定其绝对路径或相对于当前目录的路径。
若在系统中执行htpasswd命令提示未找到,说明需要安装httpd服务。对于CentOS系统,可通过以下命令进行安装:
yum install httpd
如果希望避免安装httpd,也可以使用Perl脚本来实现密码生成功能(代码如下:)
#! /usr/bin/perl -w
#filename: add_ftp_user.pl
use strict;
#
print "#example: user:passwd\n";
while () {
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;
}
首先需为生成的脚本赋予可执行权限:
chmod o+x add_user.pl
脚本的具体使用方法如下:
./add_user.pl
user:password
将脚本运行后生成的用户名和密码粘贴到/usr/local/nginx/conf/vhost/nginx_passwd文件中即可。
网站与目录认证配置差异
若需为整个网站添加认证,可直接将认证语句写入Nginx配置的server段中。
若需为特定目录添加认证,则需以目录形式编写配置。此外,还需确保在该目录中启用PHP执行功能,否则PHP文件将被直接下载而无法正常运行。
例如,针对整个网站的认证配置,auth_basic指令位于PHP解释器之前生效。
server
{
listen 80;
server_name www.jb51.net jb51.net;
root /www/jb51.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/jb51.net_access.log main;
}
针对目录的认证配置需置于单独的location块中,并在该location内部嵌套一个用于解析PHP的location块,否则PHP文件不会执行而是被下载。此时,auth_basic指令应放在嵌套的location之后。
server
{
listen 80;
server_name www.jb51.net jb51.net;
root /www/jb51.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/jb51.net_access.log main;
}
这里有一个关键细节:使用location ~ ^/admin/.* {…} 可以保护admin目录下的所有文件。如果仅设置/admin/,直接访问/admin/index.php仍可运行。^/admin/.* 表示保护该目录下所有文件。需要注意的是,只需进行一次认证,后续请求无需重复验证。
htpasswd命令参数详解与实例
htpasswd
既然使用了htpasswd,下面简要介绍其基本用法。 htpasswd参数说明
-c 创建passwdfile。如果passwdfile已存在,则会重新写入并删除原有内容。 -n 不更新passwordfile,直接在终端显示密码。 -m 使用MD5加密(默认选项)。 -d 使用CRYPT加密(默认选项)。 -p 使用普通文本格式的密码。 -s 使用SHA加密。 -b 在命令行中一并输入用户名和密码,而非交互式提示,可见明文,无需交互。 -D 删除指定的用户。 实例演示 1. 如何利用htpasswd命令添加新用户?
# /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. 如何在原有的密码文件中增加下一个用户?
# /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. 如何不更新密码文件,仅显示加密后的用户名和密码?
# /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命令删除用户名和密码?
# /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命令修改现有用户的密码?
# /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.