Nginx简单快速搭建文件服务器

一、前言

nginx的配置这块和普通的一样就可以了,只要在nginx/html 目录新增文件即可。然后通过Nginx的IP加上文件的路径即可下载,比如在nginx/html目录创建一个test目录,然后在test目录在创建一个001.txt文件,最在浏览器输入 http://localhost:10007/01/001.txt 即可进行下载。

二、静态文件下载

上述的配置可以简单满足一些要求,但是有时候我们想通过nginx进行下载其他的格式的文件时候,比如下载一张图片,但是访问这个url浏览器会自动展现这张图片,那么这时我们就可以通过增加配置,并且让浏览器下载该图片。

例如,我们在访问test目录的静态文件,那么我们在nginx/conf中添加如下配置即可!

location / {
    add_header Content-Disposition "attachment;";
}

未加配置的时候:
在这里插入图片描述

添加配置的时候:
在这里插入图片描述

三、指定文件存放路径

Nginx的文件路径默认在安装的nginx/html 目录下,如果我们想改变这路径,可以将location 的root 路径进行更改,比如更改到/opt/soft/wno704/images目录下 :

location / {
    root   /opt/soft/wno704/images;
    index  index.html index.htm;
}

如果配置多个目录,路径指定用“alias”,具体如下:

location /file {
    alias   /opt/file;
    index  index.html index.htm;
}

location /image {
    alias   /opt/image;
    index  index.html index.htm;
}

四、nginx.conf 配置

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    server_tokens   off;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    client_body_timeout 100;
    client_header_timeout 100;
    send_timeout  100;
    limit_conn_zone $binary_remote_addr zone=conn_zone:100m;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    server {
        listen       10007;
        server_name  localhost;
        location / {
            root   /opt/soft/wno704/images;
            index  index.html index.htm;
            add_header Content-Disposition "attachment;";
        }
        location /file {
            alias   /opt/file;
            index  index.html index.htm;
            add_header Content-Disposition "attachment;";
        }
        location /image {
            alias   /opt/image;
            index  index.html index.htm;
            add_header Content-Disposition "attachment;";
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值