Nginx输入用户名密码访问

前期操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#查看运行容器的ID
docker ps

#进入nginx容器
docker exec -it 容器ID /bin/bash

#容器内部操作
#更新软件源
apt-get update

#安装apache2-utils
apt-get install apache2-utils

#创建用户名
htpasswd -c /etc/nginx/passwd.db 用户名

#输入密码(自动弹出)
New password:
Re-type new password:

#查看用户和密码
cat /etc/nginx/passwd.db

#退出容器
exit

nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
server {
listen 80;
server_name 域名;

#charset koi8-r;
access_log /var/log/nginx/logging.access.log main;

location / {
auth_basic "Please input password"; #这里是验证时的提示信息
auth_basic_user_file /etc/nginx/passwd.db;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://IP:8088;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

评论