笔记 —— Nginx Basic 认证设置
Nginx默认已经安装此模块
Nginx Basic auth 配置说明:
| 语法 | 默认值 | 配置段 |
|---|---|---|
| auth_basic string | off; | off | http, server, location, limit_except |
| auth_basic_user_file file; | http, server, location, limit_except |
auth_basic默认off 表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示。
auth_basic_user_file 指定密码文件的路径
生成密码:
openssl passwd -crypt 123456
输出>: vIYfhvUIgIXJ6
创建一个文件, 把用户名密码放进去就行
执行命令:
touch /etc/nginx/authpasswd
echo 'admin:vIYfhvUIgIXJ6' >> /etc/nginx/authpasswd
配置Nginx的conf:
按照Nginx的语法, 在 .conf 文件中设置好 basic auth 信息
auth_basic "Please input password"; #这个是提示信息 auth_basic_user_file /etc/nginx/authpasswd; #存放密码文件的路径
然后访问就需要输入用户名和密码了
输入上边设置的 admin 和 123456 即可
Nginx使用basic auth 的 conf 配置文件内容如下所示:
upstream skywalking_web {
server 127.0.0.1:8080;
}
server{
charset utf-8;
listen 80;
#server_name 192.168.75.106;
location / {
auth_basic "Please input password"; #这个是提示信息
auth_basic_user_file /etc/nginx/authpasswd; #存放密码文件的路径
proxy_redirect off;
proxy_pass http://skywalking_web;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Cookie $http_cookie;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 480;
proxy_send_timeout 360;
proxy_read_timeout 360;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
client_max_body_size 200m; #上传文件大小限制
}
}
