Nginx超级强大它可以单独为一个域名设置用户认证,方法也很简单我们只要生成用户认证的用户名和密码,然后再Nginx添加auth认证配置即可
Nginx可以为某一个域名单独加用户认证,具体做法如下:
1. 生成用户认证的用户名和密码:
#wget -c soft.vpser.net/lnmp/ext/htpasswd.sh;bash htpasswd.sh
根据提示输入:
用户名:
密码:
文件名:
脚本会自动生成认证文件,auth.conf内容如下:
/usr/local/nginx/conf/auth.conf
nginx可以为网站或目录甚至特定的文件设置密码认证。密码必须是crypt加密的。可以用apache的htpasswd来创建密码。
格式为:htpasswd -d -c site_pass username password
site_pass为密码文件。放在同nginx配置文件同一目录下,当然你也可以放在其它目录下,那在nginx的配置文件中就要写明绝对地址或相对当前目录的地址。
如果你输入htpasswd命令提示没有找到命令时,你需要安装httpd.如centos是yum install httpd
2. 为Nginx添加auth认证配置
下面以某域名下面的auth目录为例,在域名的server段里加上如下代码:
location ^~ /auth/ {
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 /usr/local/nginx/conf/auth.conf
}
auth_basic_user_file 为htpasswd文件的路径
3. 重启Nginx
访问http://yourdomainname/auth/ 就会提示输入用户名和密码。
如果我们只想为目录增加用户认证上面方法显示不行,下面我来介绍具体目录用户认证
为目录增加用户认证( auth basic)
nginx的auth_basic认证采用与apache兼容的密码文件,因此我们需要通过apache的htpasswd生成密码文件。
首先查找你系统上的htpasswd
find / –name htpasswd
一般CentOS都会装apache的,位置在:
/usr/bin/htpasswd
如果没找到那就自行安装
yum install apache
并找到htpasswd文件地址。
找到htpasswd文件后,我们来创建一个用户,比如这个用户叫:xiaoquan
/usr/bin/htpasswd –c /usr/local/ngnix/conf/authdb xiaoquan
上面的命令在nginx的配置文件目录创建了用户为xiaoquan的authdb密码文件,当然你也可以创建的在其他地方,此处nginx配置文件使用比较方便。
上面的命令输入回车后会得到提示输入密码的提示信息,输入两次,即可添加成功。
接着修改nginx的配置文件,在某个需要加auth_basic的server配置下添加如下内容
location /admin/ {
auth_basic “QuanLei Auth.”;
auth_basic_user_file /usr/local/ngnix/conf/authdb;
}
最后让nginx使用最新的配置:
/usr/local/ngnix/sbin/nginx -s reload
补充一下,如果你使用了集群环境,那么还需要加Proxy_Pass:
location /admin/ {
proxy_pass http://cluster/mgmt/;
auth_basic “QuanLei Auth.”;
auth_basic_user_file /usr/local/ngnix/conf/authdb;
}Nginx Http认证 实现访问网站或目录密码认证保护 | 使用 HttpAuthBasicModule 模块
location / {
auth_basic "Restricted";
auth_basic_user_file htpasswd;
}
解释:auth_basic指令包含一个具有测试用户名和密码的HTTP基本认证,指定的参数将用于认证域。如果将值设置为“off”则忽略下级指令继承的动作。auth_basic_user_file指令为验证域指定了密码文件,0.6.7版本以后这里指定的文件是nginx.conf所在目录的相对路径,而不是–prefix指定的路径。“Restricted” 单词将会出现在第一次访问Nginx站点的弹出框内。htpasswd是一个文件,位于conf目录下。注意如果你 设置的是 conf/htpasswd,这个htpasswd文件应该在conf/conf/目录下。或者避免麻烦,直接用绝对路径。如何生成密码文件( http://wiki.nginx.org/Faq#How_do_I_generate_an_.htpasswd_file_without_having_Apache_tools_installed.3F )1)使用apache htpasswd 生成:nginx 的 http auth basic 的密码是用 crypt(3) 加密的,而apache是md5加密。所以生成时:
/usr/local/apache2/bin/htpasswd -c -d pass_file user_name #回车输入密码,-c 表示生成文件,-d 是以 crypt 加密。
2)使用 htpasswd.py来生成(Nginx 官方 Wiki 中推荐 http://trac.edgewall.org/browser/trunk/contrib/htpasswd.py)
chmod 777 htpasswd.py ./htpasswd.py -c -b htpasswd username password
使用反向代理是避免重复提示认证 proxy_set_header Authorization “”;
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
proxy_pass http://xxx.xxx.xxx.xxx;
proxy_redirect off;
proxy_set_header Authorization ""; #避免代理
weblogic重复提示认证问题
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
}
对于实现访问网站或目录密码认证保护,nginx的HTTP基本认证模块(HTTP Auth Basic)可以实现.这个模块提供基于用户名与密码的验证来保护你的站点或站点的一部分。step1:打开conf/nginx.conf文件,添加下面的指令:location / { auth_basic ”Restricted”; auth_basic_user_file webpass;}解释:auth_basic指令包含一个具有测试用户名和密码的HTTP基本认证,指定的参数将用于认证域。如果将值设置为“off”则忽略下级指令继承的动作。auth_basic_user_file指令为验证域指定了密码文件,0.6.7版本以后这里指定的文件是nginx.conf所在目录的相对路径,而不是–prefix指定的路径。“Restricted” 单词将会出现在第一次访问Nginx站点的弹出框内。webpass是一个文件,位于conf目录下。注意如果你 设置的是 conf/webpass,这个webpass文件应该在conf/conf/目录下。或者避免麻烦,直接用绝对路径。step2:创建pwd文件。添加你的用户名和密码(明文)这个密码会在第三步被替换这个文件格式如下:user:770328user2:pass2user3:pass3step3:安装Apache2 工具。[root@REKFAN /]# apt-get install apache2-utilsstep4:使用Apache2工具修改密码。[root@REKFAN /]# htpasswd /usr/nginx/conf/webpass user你会被要求输入两次密码。现在pwd文件内容改变了:
[root@REKFAN /]# vim /usr/nginx/conf/webpassuser:$apr1$I2FIVtPG$I51oSU4eatH.tJdnmxG6K0———————————————————-
如果没有apache的htpasswd程序,可以使用pl程序生成:程序地址:http://trac.edgewall.org/export/10890/trunk/contrib/htpasswd.py使用方法: ./htpasswd.py -b -c webpass admin 123456 webpass为密码文件,admin是用户名,123456是密码.step5: 重新启动Nginx服务。[root@REKFAN /]# service nginx restartstep6: 登录你的站点。例子:如果是为了给网站加上认证,可以直接将认证语句写在nginx的配置server段中。如果是为了给目录加上认证,就需要写成目录形式了。同时,还要在目录中加上php的执行,否则php就会被下载而不执行了。例如:基于整个网站的认证,auth_basic在php解释之前。
server
{
listen 80;
server_name www.Rekfan.com Rekfan.com;
root /www/Rekfan.com;
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/Rekfan.com_access.log main;
}
针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。auth_basic在嵌套的location之后。
server
{
listen 80;
server_name www.rekfan.com Rekfan.com;
root /www/Rekfan.com;
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/Rekfan.com_access.log main;
}
这里有一个细节,就是location ~ ^/admin/.* {…} 保护admin目录下的所有文件。如果你只设了/admin/ 那么直接输入/admin/index.php还是可以访问并且运行的。 ^/admin/.* 意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。
location = / {
# matches the query / only.
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
[ configuration B ]
}
location /documents/ {
# matches any query beginning with /documents/ and continues
# searching, so regular expressions will be checked.
# This will be matched only if
# regular expressions don't find a match.
[ configuration C ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration D.
[ configuration E ]
}