在日常运维中,通常会为一些服务设置权限,如为静态原型文件配置密码访问、web服务配置密码访问svnSkyWalking等服务。
那么按照以下操作就能轻松实现
nginx默认提供了【ngx_http_auth_basic_module】模块,该模块可以让用户只有输入正确的账号密码才能访问web。
使用到htpasswd,若未安装,则按以下指令安装
生成密码yum -y install httpd #安装httpd
htpasswd -cb /etc/nginx/htpasswd test 123456 #生成密码文件,test为用户名,123456为密码
修改Nginx配置server{
listen 80;
server_name 域名地址;
location / {
auth_basic "Please input password"; # 提示信息
auth_basic_user_file /etc/nginx/htpasswd; # 存放密码文件的路径
proxy_redirect off;
proxy_pass http://127.0.0.1:8080;
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; #上传文件大小限制
}
}
重启Ngxin
访问异常情况nginx -s reload #重启
若访问出现500、404等异常,基本为权限问题,用户资源与nginx启动用户不一致等情况
Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Thu Apr 28 00:29:21 CST 2022
[c9b2fd76-1] There was an unexpected error (type=Not Found, status=404).
Nginx重启关闭-c 创建一个加密文件
-n 不更新加密文件,只将htpasswd命令加密后的用户名密码显示在屏幕上
-m 默认htpassswd命令采用MD5算法对密码进行加密
-d htpassswd命令采用CRYPT算法对密码进行加密
-p htpassswd命令不对密码进行进行加密,即明文密码
-s htpassswd命令采用SHA算法对密码进行加密
-b htpassswd命令行中一并输入用户名和密码而不是根据提示输入密码
-D 删除指定的用户
nginx -s reload #重启
nginx -s stop #停止
nginx -c /etc/nginx/nginx.conf #启动
欲买桂花同载酒,终不似,少年游



