ngx_http_auth_basic_module模块实现让访问者只有输入正确的用户密码才允许访问web内容。web上的一些内容不想被其他人知道,但是又想让部分人看到。nginx的http auth模块以及Apache http auth都是很好的解决方案。 默认情况下nginx已经安装了ngx_http_auth_basic_module模块,如果不需要这个模块,可以加上 --without-http_auth_basic_module 。 nginx basic auth指令 语法: auth_basic string | off; 默认值: auth_basic off; 配置段: http, server, location, limit_except 默认表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示。 语法: auth_basic_user_file file; 默认值: — 配置段: http, server, location, limit_except 用户密码文件,文件内容类似如下: testuser1:password1 testuser2:password2:comment nginx认证配置实例 server{ server_name www.xiaoten.com xiaoten.com; index index.html index.php; root /data/site/www.xiaoten.com; location / { auth_basic "nginx basic http test for xiaoten.com"; auth_basic_user_file conf/htpasswd; autoindex on; } } 备注:一定要注意auth_basic_user_file路径,否则会不厌其烦的出现403。 生成密码 可以使用htpasswd,或者使用openssl # printf "test:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd # cat conf/htpasswd test:xyJkVhXGAZ8tM 账号:test 密码:123456 reload nginx # /usr/local/nginx-1.5.2/sbin/nginx -s reload 完。 附:LNMP下为Nginx目录设置访问验证的用户名密码 博主的云服务器的网站运行环境使用的是lnmp,于是发现lnmp也有相关的权限设置的教程,也有针对于权限认证文件生成写成的脚本文件,下面是教程内容,转自vps侦探: 有时候需要象Apache那样为指定的目录添加访问验证,一般在Apache下使用htpasswd来添加,而htpasswd是包含在apache2-utils里,一般LNMP一键安装包或自己编译安装LNMP都不会安装apache2-utils。 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 fastcgi.conf; } auth_basic "Authorized users only"; auth_basic_user_file 这里写前面脚本返回的文件路径; } 本教程适合LNMP一键安装包或自己安装的LNMP,只不过目录和配置文件可能位置不一样。 设置完执行:/usr/local/nginx/sbin/nginx -t 测试配置是否有错误。 再执行:/usr/local/nginx/sbin/nginx -s reload 载入配置文件。 Loading... ngx_http_auth_basic_module模块实现让访问者只有输入正确的用户密码才允许访问web内容。web上的一些内容不想被其他人知道,但是又想让部分人看到。nginx的http auth模块以及Apache http auth都是很好的解决方案。 默认情况下nginx已经安装了ngx_http_auth_basic_module模块,如果不需要这个模块,可以加上 --without-http_auth_basic_module 。<!--more--> <h3>nginx basic auth指令</h3> 语法: auth_basic string | off; 默认值: auth_basic off; 配置段: http, server, location, limit_except 默认表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示。 语法: auth_basic_user_file file; 默认值: — 配置段: http, server, location, limit_except 用户密码文件,文件内容类似如下: <pre class="lang:default decode:true">testuser1:password1 testuser2:password2:comment</pre> <h3>nginx认证配置实例</h3> <pre class="lang:default decode:true">server{ server_name www.xiaoten.com xiaoten.com; index index.html index.php; root /data/site/www.xiaoten.com; location / { auth_basic "nginx basic http test for xiaoten.com"; auth_basic_user_file conf/htpasswd; autoindex on; } }</pre> 备注:一定要注意auth_basic_user_file路径,否则会不厌其烦的出现403。 生成密码 可以使用htpasswd,或者使用openssl <pre class="lang:default decode:true "># printf "test:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd # cat conf/htpasswd test:xyJkVhXGAZ8tM</pre> 账号:test 密码:123456 reload nginx <pre class="lang:default decode:true"># /usr/local/nginx-1.5.2/sbin/nginx -s reload</pre> 完。 <h3>附:LNMP下为Nginx目录设置访问验证的用户名密码</h3> 博主的云服务器的网站运行环境使用的是lnmp,于是发现lnmp也有相关的权限设置的教程,也有针对于权限认证文件生成写成的脚本文件,下面是教程内容,转自vps侦探: 有时候需要象Apache那样为指定的目录添加访问验证,一般在Apache下使用htpasswd来添加,而htpasswd是包含在apache2-utils里,一般LNMP一键安装包或自己编译安装LNMP都不会安装apache2-utils。 1、创建类htpasswd文件 执行:<code>wget -c soft.vpser.net/lnmp/ext/htpasswd.sh;bash htpasswd.sh</code> 按提示输入用户名、密码、及认证文件名。脚本会自动生成认证文件。记录下脚本返回的文件路径。如:<code>/usr/local/nginx/conf/vpser.net.auth</code>。 2、为Nginx添加auth认证配置 下面是以某域名下面的soft目录为例,在域名的server段里加上如下代码: <pre class="lang:default decode:true">location ^~ /soft/ { auth_basic "Authorized users only"; auth_basic_user_file 这里写前面脚本返回的文件路径; }</pre> Authorized users only为提示信息,可以修改成自己想让他提示的信息;auth_basic_user_file 后面需要填htpasswd.sh脚本返回的人家文件的路径。按上面的提示修改好配置后,重启nginx,访问http://yourdomainname/soft/ 就会提示输入用户名和密码。 注意,加上认证之后该目录下的PHP将不会被解析,会出现下载提示,如果想可以解析PHP可以将上面的配置改为: <pre class="lang:default decode:true">location ^~ /soft/ { location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } auth_basic "Authorized users only"; auth_basic_user_file 这里写前面脚本返回的文件路径; }</pre> 本教程适合LNMP一键安装包或自己安装的LNMP,只不过目录和配置文件可能位置不一样。 设置完执行:<code>/usr/local/nginx/sbin/nginx -t</code> 测试配置是否有错误。 再执行:<code>/usr/local/nginx/sbin/nginx -s reload</code> 载入配置文件。 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏