nginx映射本地文件_Docker的日常记录——nginx

首先,拉去nginx最新镜像

docker pull nginx:latest

使用以下命令查看是否安装成功

docker images

59600315fe17113870e6eaf2567b09e4.png

运行容器,映射到本地80端口

docker run --name nginx -p 80:80 -d nginx

访问

a03c359c351341546acfd9a7ee50ba5d.png

nginx 配置

因为我需要使用nginx做静态资源服务器,所以还需要添加相应配置

首先copy出nginx 配置

mkdir ~nginx/conf ~nginx/logs
docker cp nginx:/etc/nginx/nginx.conf ~/nginx/conf

docker默认的nginx.conf配置如下

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  65;

    #gzip  on;

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

可以看到配置文件中include /etc/nginx/conf.d/*.conf,这就是我将要映射配置的路径

因为我打算还是用nginx中的默认配置文件,所以就把刚刚copy下来的nginx.conf给删掉了

rm ~/nginx/conf/nginx.conf

在刚刚创建好的~/nginx/conf路径下,新增配置

vim ~/nginx/conf/video.conf

添加server配置

server{
    server_name localhost;
    listen 80;
    location ^~ /video/{
        root /Users/yanyi/data/;
    }
}

关闭删除刚刚启动的容器

docker stop nginx
docker rm nginx

重新启动新的容器

docker run --name nginx -p 80:80  
-v ~/nginx/conf:/etc/nginx/conf.d 
-v ~/nginx/logs:/var/log/nginx 
-v ~/data:/User/yanyi/data/ 
-d nginx

访问资源

9de98d7f97c4d862ac7215948b522ba6.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值