栏目头部广告

Nginx文件下载服务器搭建

一、Nginx相关参数解读

root和alias区别

Sets the root directory for requests. For example, with the following configuration 
location /i/ { 
    root /data/w3;
}

Defines a replacement for the specified location. For example, with the following configuration 
location /i/ {
    alias /data/w3/images/;
}

当访问/i/top.gif时,root是去/data/w3/i/top.gif请求文件,alias是去/data/w3/images/top.gif请求

(1)root响应的路径:配置的路径+完整访问路径(完整的location配置路径+静态文件)

(2)alias响应的路径:配置的路径+静态文件(去除location中配置的路径)

官网参考文档:https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/

二、Nginx文件下载服务器搭建

2.1 修改配置文件

[root@10-27-0-224 ~]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

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"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;

        include /etc/nginx/default.d/*.conf;
        location / {
            alias /data/;
            index  index.html index.htm;
            autoindex on;                 # 开启nginx目录浏览功能
            autoindex_exact_size off;     # 默认为on,显示出文件的确切大小,单位是bytes。
                                          # 改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
            autoindex_localtime on;       # 显示文件修改时间为服务器本地时间
            charset utf-8,gbk;            # 显示中文
            limit_rate 100k;              # 单个线程最大下载速度,单位KB/s        
        }
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

2.2 访问测试效果

Nginx文件下载服务器搭建(图1)

作者:UStarGao
链接:https://www.starcto.com/service_operations/173.html
来源:STARCTO
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

UCloud云平台推荐


UCloud新用户专属注册连接

UCloud CDN超值特惠专场

UCloud全球云主机(UHost/VPS)大促页面

UCloud快杰云主机大促页面

加载中~
文章详情页广告

随便看看

底部广告
`