Nginx的Basic认证

参考 -> Restricting Access with HTTP Basic Authentication


敏感服务不想对外暴露, 简单加个Basic Authentication

AUR 是个好东西, htpasswd也有了, apache-tools还可以利用 ab 进行压力测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  ~ proxychains yaourt htpasswd
[proxychains] config file found: /etc/proxychains.conf
[proxychains] preloading /usr/lib/libproxychains4.so
1 archlinuxcn/apache-tools 2.4.25-1 [installed]
Useful Apache tools - htdigest, htpasswd, ab, htdbm
2 aur/apache-tools 2.4.39-1 [installed: 2.4.25-1] (115) (1.01)
Useful Apache tools - htdigest, htpasswd, ab, htdbm
3 aur/python-htpasswd-git .r31.ef677a5-1 (0) (0.00)
Python Library for working with htpasswd user (only basic authorization) and group files
4 aur/python2-htpasswd-git .r31.ef677a5-1 (5) (0.05)
Python 2 Library for working with htpasswd user (only basic authorization) and group files
==> Enter n° of packages to be installed (e.g., 1 2 3 or 1-3)
==> ---------------------------------------------------------
==>

生成密码

1
htpasswd -c 文件名 用户名

挂载进 nginx 容器, 在需要的位置配置

1
2
auth_basic "Auth";
auth_basic_user_file `认证文件路径`;

注意文件权限问题

curl 测试一下是否生效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< HTTP/1.1 401 Unauthorized
< Server: nginx/1.15.12
< Date: Sat, 20 Apr 2019 12:53:31 GMT
< Content-Type: text/html
< Content-Length: 180
< Connection: keep-alive
< WWW-Authenticate: Basic realm="Auth"
<
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.15.12</center>
</body>
</html>

正常使用