nginx安装
Centos安装
yum -y install nginx
Ubuntu安装
apt -y install nginx
配置
创建文件存储目录download,在download目录下添加提供下载的文件
mkdir /etc/nginx/download
cp ~/nginx_install.sh /etc/nginx/download/ #上传一个测试文件
配置nginx配置文件
vi /etc/nginx/nginx.conf
user www-data;
worker_processes 2;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=512m;
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
#include /etc/nginx/conf.d/*.conf;
server {
listen 18907; #指定端口,访问的时候需要,推荐五位端口
root /usr/share/nginx/html ;
proxy_cache_key $host$uri;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
root /etc/nginx/download; # 指定实际目录绝对路径
autoindex on; # 开启目录浏览功能
autoindex_exact_size off; # 关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b
autoindex_localtime on; # 开启以服务器本地时区显示文件修改日期
charset utf-8,gbk; # 显示中文
#limit_conn one 8; # 并发数配置
#limit_rate 100k; # 单线程最大下载速度kb
}
}
}
检测语法并重启nginx
nginx -t && systemctl restart nginx
浏览器请求测试
ip add #查看本机的ip
浏览器打开192.168.10.36:18907
这里值得说的是,nginx下载服务支持 wget 下载,支持断点续传
普通用户的需求基本可以满足了,如果需要提升安全性,继续往下看
增加用户名密码验证
使用htpasswd命令生成用户名密码
Centos安装
yum -y install httpd
Ubuntu在没有安装的情况下,会给出相应的提示
apt install apache2-utils
htpasswd -c /etc/nginx/passwd test
/etc/nginx/passwd是生成密码文件的路径,然后test是用户名,也可以根据需要设置其它用户名。运行命令后,输入两次密码,就会提示为用户test添加密码。
nginx认证配置
配置文件中的server加上认证配置auth_basic 和 auth_basic_user_file即可
server {
listen 18907;
......
# 需要添加的内容
auth_basic "Please input password"; # 验证时的提示信息
auth_basic_user_file /etc/nginx/passwd; # 认证文件
......
}
测试效果
浏览器打开192.168.10.36:18907就会弹窗让输入用户名密码登录才能正常访问
使用wget下载测试,需要使用–user和–password参数
wget http://192.168.10.36:18907/nginx_install.sh --user=test --password=123456