`
eyesmore
  • 浏览: 363896 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

HTTP Basic Auth 备忘

阅读更多

http://wiki.nginx.org/HttpAuthBasicModule

http://wiki.nginx.org/Faq#How_do_I_generate_an_htpasswd_file_without_having_Apache_tools_installed.3F

 

 

location  /  {

  auth_basic            "Restricted";

  auth_basic_user_file  htpasswd;

}

 

命令1:auth_basic  表示使用HTTP Basic Auth,不是其他的Auth形式。

auth_basic "Restricted" 字符串仅仅是提示,作为HTTP头,可以随便写。

 

命令2:auth_basic_user_file  表示HTTP Basic Auth的密码文件存储地址。

htpasswd表示文件是跟conf目录下(也就是nginx.conf锁在的目录)。

需要注意的是:密码文件本身是加密存储的,用的是crypt(3)算法。

Nginx官方建议用apache的工具生成加密文件。但是如果你没有Apache,也有办法:

printf "myuser:$(openssl passwd -crypt mypwd)\n" >> htpasswd

表示向密码文件htpasswd追加账号名为myuser,密码名mypwd的账号。

还有注意的是:密码不能超过8个字节,否则会按8字节处理。

Warning: truncating password to 8 characters

 

 

curl http://www.example.com/protected.html -I

curl http://www.example.com/protected.html -I --basic -u myuser:mypwd

 

 

[@tc_52_122 conf]# curl http://www.example.com/protected.html -I

HTTP/1.1 401 Unauthorized

Server: nginx/1.0.5

Date: Fri, 14 Dec 2012 07:37:14 GMT

Content-Type: text/html

Content-Length: 194

Connection: close

WWW-Authenticate: Basic realm="Secure Area"

 

 

[@tc_52_122 conf]# curl http://www.example.com/protected.html -I --basic -u myuser:mypwd

HTTP/1.1 200 OK

Server: nginx/1.0.5

Date: Fri, 14 Dec 2012 07:37:29 GMT

Content-Type: text/html

Content-Length: 23

Last-Modified: Fri, 14 Dec 2012 06:54:02 GMT

Connection: close

Accept-Ranges: bytes

 

[@tc_52_122 conf]# 


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics