1、创建类htpasswd文件
执行:
代码如下 wget -c soft.vpser.net/lnmp/ext/htpasswd.sh;bash htpasswd.sh按提示输入用户名、密码、及认证文件名。脚本会自动生成认证文件。记录下脚本返回的文件路径。如:/usr/local/nginx/conf/vpser.net.auth。
2、为Nginx添加auth认证配置
下面是以某域名下面的soft目录为例,在域名的server段里加上如下代码:
代码如下location ^~ /soft/
{
auth_basic "Authorized users only";
auth_basic_user_file 这里写前面脚本返回的文件路径;
}
Authorized users only为提示信息,可以修改成自己想让他提示的信息;auth_basic_user_file 后面需要填htpasswd.sh脚本返回的人家文件的路径。按上面的提示修改好配置后,重启nginx,访问http://yourdomainname/soft/ 就会提示输入用户名和密码。
注意,加上认证之后该目录下的PHP将不会被解析,会出现下载提示,如果想可以解析PHP可以将上面的配置改为:
代码如下location ^~ /soft/ {
location ~ .*.(php|php5)?$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
auth_basic "Authorized users only";
auth_basic_user_file 这里写前面脚本返回的文件路径;
}
方法二
先建立以下脚本,命名为crypt.pl,放在root目录下面。
#!/usr/bin/perl
usestrict;
my
$passWord=$ARGV[0];
print
crypt($passWord,$passWord)."n";
然后运行:
chmod +x crypt.pl
./crypt.pl password
将上面用crypt.pl 生成的加密后的密码按照用户名:密码的格式写到htpasswd文件中,htpasswd放在/usr/local/nginx/conf目录下面。
最后在/usr/local/nginx/conf/nginx.conf里面,添加以下内容即可,放在server里面。
代码如下location /phpmyadmin {
auth_basic "Nginx Status";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
以上即对phpmyadmin目录进行加密。
http://m.bbyears.com/caozuoxitong/47518.html
推荐访问:nginx负载均衡配置