nginx用户认证配置(Basic HTTP authentication)
发表于:2019-08-15 18:26:57浏览:53次
gx_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`
用户密码文件,文件内容类似如下:
```
user1:password1
user2:password2:comment
```
#### nginx认证配置实例
```
server{
server_name xxx;
index index.html index.php;
root /data/site/xxx;
location /
{
auth_basic "nginx basic http test for ttlsa.com";
auth_basic_user_file htpasswd;
autoindex on;
}
}
```
可以使用htpasswd,或者使用openssl
工具连接: [http://tool.oschina.net/htpasswd](http://tool.oschina.net/htpasswd "http://tool.oschina.net/htpasswd")
```
# printf "ubuntu:$(openssl passwd -crypt 123456)\n" >>htpasswd
# cat conf/htpasswd
ttlsa:xyJkVhXGAZ8tM
```
```
sudo service nginx reload
```
账号:ubuntu
密码:123456

