Nginx之搭建需要身份验证的文件下载服务器

您所在的位置:网站首页 nginx权限认证 Nginx之搭建需要身份验证的文件下载服务器

Nginx之搭建需要身份验证的文件下载服务器

2023-11-29 14:36| 来源: 网络整理| 查看: 265

本文主要介绍使用nginx和openssl结合,搭建一个简单的文件下载服务器,通过用户名和密码完成身份证验证。

1、nginx安装

nginx安装可以使用源码安装或者YUM安装,源码安装方式参照博文 https://blog.csdn.net/carefree2005/article/details/110874473 或者使用 yum install nginx安装,详细安装步骤见博文:https://blog.csdn.net/carefree2005/article/details/110919052

2、openssl安装

#yum install -y openssl

3、创建用户名密码验证文件

生成 /etc/nginx/passwd openssl passwd 你的密码 >> /etc/nginx/passwd [root@test1 ~]#openssl passwd 123321 >> /etc/nginx/passwd [root@test1 ~]# cat /etc/nginx/passwd gnqkRk4ynK0bE 编辑/etc/nginx/paswd 在密文前面加上用户名和冒号 [root@test1 ~]# vim /etc/nginx/passwd test:gnqkRk4ynK0bE 保存退出

4、配置nginx

进入nginx配置目录 #/etc/nginx/conf.d #vim download.conf 将如下配置添加到download.conf后保存退出 server{ listen 8080; server_name 192.168.0.124; access_log /var/log/nginx/download.access.log main; error_log /var/log/nginx/download.err.log warn; large_client_header_buffers 4 16k; location / { autoindex on;# 显示目录 autoindex_exact_size on;# 显示文件大小 autoindex_localtime on;# 显示文件时间 add_header rootpath /opt/download; auth_basic “authentication”; auth_basic_user_file /etc/nginx/passwd; root /opt/download; } }

5、启动nginx

检查并验证nginx配置文件 [root@test1 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 启动nginx [root@test1 conf.d]# service nginx start 查看nginx状态 [root@test1 conf.d]# service nginx status Redirecting to /bin/systemctl status nginx.service ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2020-12-17 06:03:46 PST; 19s ago Docs: http://nginx.org/en/docs/ Process: 73028 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 73029 (nginx) CGroup: /system.slice/nginx.service ├─73029 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf └─73030 nginx: worker process Dec 17 06:03:46 test1 systemd[1]: Starting nginx - high performance web server… Dec 17 06:03:46 test1 systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start. Dec 17 06:03:46 test1 systemd[1]: Started nginx - high performance web server.

6、创建下载目录和测试文件

#cd /opt #mkdir download [root@test1 download]# touch hi.txt [root@test1 download]# touch hello.txt [root@test1 opt]# cp /tmp/yum.log download/ [root@test1 opt]# chown -R nginx.nginx download [root@test1 opt]# ls download [root@test1 opt]# ll download/ total 4 -rw-r–r--. 1 nginx nginx 0 Dec 17 06:06 hello.txt -rw-r–r--. 1 nginx nginx 5 Dec 17 06:16 hi.txt -rw-------. 1 nginx nginx 0 Dec 17 06:17 yum.log

7、下载验证

打开下载服务器地址地址http://192.168.0.124:8080 在这里插入图片描述 输入用户名密码后登录下载服务器即可看到可下载文件 点击文件可以开始下载 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3