使用docker搭建nginx文件下载服务器

本文详细介绍了如何使用Docker部署一个Nginx文件下载服务器,包括配置nginx.conf文件,设置download目录,以及启动Nginx容器。重点讲解了root和alias在配置中的区别,并提供了启动容器的命令。
摘要由CSDN通过智能技术生成

一、概述

这篇文章介绍使用docker搭建nginx文件下载服务器,主要有两个步骤:

在宿主机配置nginx.conf文件
挂载宿主机的nginx.conf文件和下载路径到容器的对应路径

二、配置步骤

1、在宿主机配置nginx.conf文件

创建文件夹 /data/docker/nginx (作为挂载nginx需要的文件)

包含 1.nginx.conf 配置文件

2.download 目录作为存储下载源文件

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 2048;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location /view {
       # root    /usr/share/nginx/html/download;
	    alias    /usr/share/nginx/html/download;
        autoindex on;    #开启索引功能
        autoindex_exact_size off;  #关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
        autoindex_localtime on;   #显示本机时间而非 GMT 时间
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

部署中存在问题 因为这里配置的路径加了个 /view 如果直接使用root是访问不到的

总结:
root响应的路径:配置的路径(root指向的路径)+完整访问路径(location的路径)+静态文件
alias响应的路径:配置路径+静态文件(去除location中配置的路径)

一般情况下,在location /中配置root,在location /other中配置alias

2、启动nginx的docker容器

docker run --name nginx -v /data/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /data/docker/nginx/download/:/usr/share/nginx/html/download -p 80:80 -d  nginx

参数说明:

–name:指定容器的名称,容器名称不能重复
-v:挂载数据卷,宿主机路径:容器路径
-p:指定映射端口,宿主机端口:容器端口
-d:指定容器后台运行

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值