
正文
基于nginx的配置网站密码认证
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
在nginx配置服务中,创建访问网站密码认证。
1)需要ngx_http_auth_basic_module模块
语法:
Syntax: auth_basic string | off;
Default:
auth_basic off;
Context: http, server, location, limit_except
默认是关闭的,使用位置在http,server,location标签。
2)例子:
location / {
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}
3)首先配置出保存用户和密码的文件htpasswd
使用htpasswd命令进行创建登录用户和密码
参数:
-c:创建一个加密文件;
-n:不更新加密文件,只将加密后的用户名密码显示在屏幕上;
-m:默认采用MD5算法对密码进行加密;
-d:采用CRYPT算法对密码进行加密;
-p:不对密码进行进行加密,即明文密码;
-s:采用SHA算法对密码进行加密;
-b:在命令行中一并输入用户名和密码而不是根据提示输入密码;
-D:删除指定的用户。
a、查看系统是否安装了htpasswd命令
root@oldboy nginx]# which htpasswd
/usr/bin/which: no htpasswd in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
b、安装htpasswd命令
[root@oldboy nginx]# yum install http -y
c、使用htpasswd命令创建密码文件htpasswd
[root@oldboy nginx]# htpasswd -cb /application/nginx/conf/htpasswd taili01
Adding password for user taili01
d、在原文件htpasswd中增加新的用户
[root@oldboy nginx]# htpasswd -b ./htpasswd taili02
Adding password for user taili02
e、查看htpasswd文件内容
[root@oldboy nginx]# cat ./htpasswd
taili01:ZCN8EXnjt3OYY
taili02:lDJrLzZuwxh/g
4)配置nginx.conf文件
[root@oldboy conf]# vim nginx.conf
worker_processes ;
events {
worker_connections ;
}
error_log logs/error.log;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
keepalive_timeout ;
server {
listen ;
server_name www.sandy.com;
location / {
root html/www;
index index.html index.htm;
auth_basic "This is input password";
auth_basic_user_file conf/htpasswd;
}
access_log logs/host.access.log main;
error_page /50x.html;
location = /50x.html {
root html;
}
}
}
5)访问网站








