nginx配置网页密码

环境:

nginx 1.16
httpd Apache/2.4.6

yum安装方式:(yum没有此软件时,需要配置yum源)

yum install nginx -y   
yum install httpd -y

启动nginx  、httpd

systemctl start nginx
systemctl  start httpd

创建密文

[root@243-ceshi download]# htpasswd -cb /dwz/htpasswd kaifa2  vrgv25.      
Adding password for user kaifa2 [root@243-ceshi download]# cat /dwz/htpasswd kaifa2:$apr1$UJrSJ10s$asQLF/ImBymWnB1zk5U6S0

注:/dwz目录需要存在,要不会报错

nginx配置文件:


[root@243-ceshi download]#vim /etc/nginx/nginx.conf.d/download.conf
server {
        listen       30000;                                          #端口号自定义
        root         /usr/share/nginx/html;                           
        auth_basic "This is input password";                         #网页输入密码提示语
        auth_basic_user_file /dwz/htpasswd;                          #密文存放位置
        # Load configuration files for the default server block.

location /download {                                                  #下面是自定义下载引导位置,当访问ip:30000/download后会转到服务器的/download/目录下,提供下载
    charset utf-8;                                                    #支持中文
    alias /download/;                                                 #转的下载目录
    index index.html;
    autoindex on;
    autoindex_exact_size off;
}
}

注:download.conf配置文件是自定义创建的,并被nginx管理

重新启动nginx

systemctl restart nginx

打开网址:

完成

嗨~如果有帮助,请帮忙点个赞吧,谢谢 -致敬每一个正在努力的人
原文地址:https://www.cnblogs.com/dongweizhen/p/13895814.html