栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx为服务配置访问密码

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx为服务配置访问密码

背景

在日常运维中,通常会为一些服务设置权限,如为静态原型文件配置密码访问、web服务配置密码访问svnSkyWalking等服务。
那么按照以下操作就能轻松实现

安装httpd

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).

扩展 htpasswd命令选项参数说明:

-c 创建一个加密文件
-n 不更新加密文件,只将htpasswd命令加密后的用户名密码显示在屏幕上
-m 默认htpassswd命令采用MD5算法对密码进行加密
-d htpassswd命令采用CRYPT算法对密码进行加密
-p htpassswd命令不对密码进行进行加密,即明文密码
-s htpassswd命令采用SHA算法对密码进行加密
-b htpassswd命令行中一并输入用户名和密码而不是根据提示输入密码
-D 删除指定的用户

Nginx重启关闭

nginx -s reload #重启
nginx -s stop #停止
nginx -c /etc/nginx/nginx.conf #启动

欲买桂花同载酒,终不似,少年游

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/839935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号