Nginx下配置Http Basic Auth

     nginx basic auth指令php

第一条语句:python

语法:     auth_basic string | off;
默认值:     auth_basic off;
配置段:     http, server, location, limit_exceptnginx

默认表示不开启认证,后面若是跟上字符,这些字符会在弹窗中显示。浏览器

第二条语句:curl

语法:     auth_basic_user_file file;
默认值:     —
配置段:     http, server, location, limit_except加密

 

1. 下载这个Python文件:http://trac.edgewall.org/export/10770/trunk/contrib/htpasswd.py (nginx wiki里推荐的)url

chmod 777 htpasswd.py  #给文件加上权限
./htpasswd.py -c -b htpasswd username password
#-c为生成文件 htpasswd为文件名
-d 是以 crypt 加密
username为登陆的名字 password为登陆密码

2.可使用htpasswd,或者使用openssl建立密码spa

# printf "ttlsa:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd
# cat conf/htpasswd 
ttlsa:xyJkVhXGAZ8tM

3.把htppasswd文件放到nginx/conf目录下面,编辑nginx.conf文件.net

在location里面加两行:
auth_basic     "password please";
auth_basic_user_file  conf/htpasswd;

4.重启Nginx服务rest

service nginx restart

5.注意错误提醒:说明43行有错

6.可使用curl -v http://localhost 查看http返回的状态码:401未经受权表示成功

 

7.在浏览器里默认的访问地址是http://localhost

相关文章
相关标签/搜索