nginx设置HTTP基本身份验证凭据,文件下载服务器
版权声明:
                        
                    
                                本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
                    更新时间:
                    
                2023-05-09 22:13:24
                温馨提示:
                    
            学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
                先生密码文件
账号:admin
密码: 12345678
特别注意密码最长为8位,长了会截断
printf "admin:$(openssl passwd -crypt 12345678)\n" > /root/dnmp/services/openresty/conf.d/.htpasswd
nginx配置
server {
     listen    80;
     server_name  download.xxx.com;
     root   /www/download;
     index  index.php index.html; 
     auth_basic "authentication";
     auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
     location / {
        if ($request_filename ~* ^.*?\.(html|doc|pdf|zip|docx|txt)$) {
            add_header Content-Disposition attachment;
            add_header Content-Type application/octet-stream;
        }
            sendfile on;   # 开启高效文件传输模式
            autoindex on;  # 开启目录文件列表
            autoindex_exact_size on;  # 显示出文件的确切大小,单位是bytes
            autoindex_localtime on;  # 显示的文件时间为文件的服务器时间
            charset utf-8,gbk;  # 避免中文乱码
      }
}