Nginx 为静态文件添加访问权限

    为了方便看资料,如技术文章、音频、视频,在自己的华为云上用 Docker 搭了一个 Nginx 静态资源服务器,朋友同事知道的人越来越多后,觉得有必要做一些安全限制。

  1. 安装软件
apt install apache2-utils
  1. 创建目录
mkdir /etc/apache
  1. 创建账号
htpasswd -c /etc/apache/htpasswd admin

接下来根据提示设置密码

New password: 
Re-type new password: 
  1. 修改 Nginx 配置
server{
    
    
        listen 80;
        server_name *.*.*.*;
        root /usr/share/nginx/html;

        location /doc {
    
    
                auth_basic "账号登录";      # 开启认证
                auth_basic_user_file /etc/apache/htpasswd;    # 上面指定的密码文件
                alias /usr/share/nginx/html;
                autoindex on;
                autoindex_exact_size on;
                autoindex_localtime on;
                charset utf-8,gbk;
        }
}
  1. 重启 Nginx 容器
docker restart nginx

猜你喜欢

转载自blog.csdn.net/ChinaLiaoTian/article/details/125190827