In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要介绍了Nginx服务器中为网站或目录添加认证密码的配置详解,使用到了Apache的htpasswd工具,需要的朋友可以参考下
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.pluser:password
把生成的用户名密码粘贴到/usr/local/nginx/conf/vhost/http://www.bbqmw.net/qm_yeqm//nginx_passwd文件中即可
如果是为了给网站加上认证,可以直接将认证语句写在nginx的配置server段中。
如果是为了给目录加上认证,就需要写成目录形式了。同时,还要在目录中加上php的执行,否则php就会被下载而不执行了。
例如:基于整个网站的认证,auth_basic在php解释之前。
server { listen 80; server_name www.iis7.com 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.iis7.com 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/.* 意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.