docker中使用nginx,实现文件下载功能

Docker容器部署 Nginx服务

首先需要先在自己的Docker中,下载Nginx镜像。

docker pull nginx

配置Nginx

第一步,生成宿主机的文件夹以及宿主机的配置文件

mkdir -p /home/nginx /home/nginx/sharefiles && vim /home/nginx/nginx.conf

第二步,修改nginx.conf 配置文件

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/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;
        server_name  _;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        limit_rate 50k;# 限制下载速度
        location / {
            root    /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 {
        }
    }
}

第三步、启动Nginx的Docker容器

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

参数说明

  • –name:指定容器的名称,容器名称不能重复
  • -v:挂载数据卷,宿主机路径:容器路径
  • -p:指定映射端口,宿主机端口:容器端口
  • -d:指定容器后台运行
    运行成功后,通过脚本,查看自己的容器是否启动。
docker ps # 查看运行中的容器
或者
docker ps -a # 查看所有的容器

启动成功后,只要输入对应的ip:80 即可
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值