Docker 运行 nginx,修改配置,作为文件服务器

零、步骤

  1. 获取nginx镜像
  2. 创建nginx.conf,修改配置
  3. 覆盖nginx的默认配置,运行nginx

在进行尝试的时候,思路不是很清晰,中间搞混了好几次,主要是nginx服务监听的端口,和docker的端口搞混了,还有进行文件路径映射的时候,搞不太清楚nginx.conf中的root目录和docker运行时的-v操作,网上都叫做挂载,而我更喜欢称为映射

一、获取nginx镜像

docker pull nginx

二、创建nginx.conf,修改配置

我的目录

/home
	/nginx
		startNginx.sh
        stopNginx.sh
		nginx.conf
		default.conf
		/data
			index.html
			/images
				/another.jpg
			/audios
				/透明.mp3

可以在windows上编辑好上传到服务器,再进行修改,nginx.conf配置如下

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    # 表示具体的配置位置,相当于将配置分散,不同的serve在不同的conf文件中
    # 默认引入/etc/nginx/conf.d/ 目录下的所有.conf结尾的配置文件,默认有default.conf文件
    include /etc/nginx/conf.d/*.conf;
}

default.conf文件配置

此处虽然端口为8089,但不是客户端浏览器的访问端口,而是docker中nginx容器的内部端口,客户端浏览器访问应使用docker -p 80:8089 进行映射之后的80端口

server {
    # nginx服务端口
    listen       8089;
    # 域名,IP都可以
    server_name  www.another.ren;

    # 编码
    charset utf8;
    
    # 如果不使用docker的情况下
    # 浏览器访问
    # www.another.ren:8089
    # 访问返回资源
    # www.another.ren:8089/home/data/index.html
    location / {
        root /home/data;
        # 默认访问
        index index.html;
    }
    
    # 如果不使用docker的情况下
    # 浏览器访问
    # www.another.ren:8089/images/pictures
    # 访问返回资源
    # www.another.ren:8089/home/data/images/pictures
    location /images {
        root   /home/data;
    }
   
    # 如果不使用docker的情况下
    # 浏览器访问
    # www.another.ren:8089/audios/透明.mp3
    # 访问返回资源
    # www.another.ren:8089/home/data/audios/透明.mp3
    location /audios {
        root    /home/data;
    }
}

三、覆盖nginx的默认配置,运行nginx

运行nginx的命令startNginx.sh,写到了脚本中,不用每次都这么麻烦地输入这么多东西,sh startNginx.sh 执行

docker run --name another-nginx -d -p 80:8089 -v /home/nginx/log:/var/log/nginx -v /home/nginx/data:/home/data:ro -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -v /home/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro nginx
  1. –name 为nginx起别名
  2. -p 端口映射,80表示宿主机端口,8089表示docker容器的内部端口
  3. -v 文件映射,-v /home/nginx/nginx.conf:/etc/nginx/nginx.conf 表示 宿主机中的/home/nginx/nginx.conf 映射覆盖nginx容器中的/etc/nginx/nginx.conf 的配置

停止nginx的命令stopNginx.sh,执行脚本sh stopNginx.sh

# 停止容器,another-nginx为运行时起得别名
docker stop another-nginx
# 移除容器
docker rm another-nginx
# 列出所有容器
docker ps -a

ps

  1. 目录映射

    nginx 版本为1.17.8,其在docker容器中的配置文件的位置信息如下

    docker中nginx容器中的目录与宿主机的目录不是一回事

    docker中nginx容器默认的root目录为

    /usr/share/nginx/html

    docker中nginx容器默认的日志目录为

    /var/log/nginx

    docker中nginx容器默认的根配置目录为

    /etc/nginx/nginx.conf

    docker中nginx容器默认的子配置目录为

    /etc/nginx/conf.d/default.conf

    在运行docker的nginx容器时,可以使用-v命令来进行映射

    -v 宿主机目录:ningx容器中的目录

    -v 宿主机文件:nginx容器中的文件

  2. 端口映射

    1. 首先客户端浏览器中访问的端口与服务器(宿主机)保持一致

    2. 服务器(宿主机)端口与docker之间的映射,-p 80:8089 即服务器使用80端口,docker中的nginx容器内部使用8089端口

    3. docker中nginx容器的端口在nginx.conf中进行配置,我配置的为8089,与-p 80:8089 中的后者保持一致

      nginx.conf中配置nginx容器端口 —> docker容器的端口映射 —> 浏览器与服务器端口一致

  3. nginx资源访问路径问题

    其实我感觉尝试的过程中,应该有已经配置成功了,但是访问路径错误,一直404的情况!!!

    排除端口的问题之后,nginx的路径问题来了

    location中的root配置,并不是我所理解的根目录那样,在访问www.another.ren/时,确实是访问到www.another.ren/home/data/index.html,但不是我所理解的那般,/ 替换为/home/data/ ,而是在访问/home/data/时,在/ 前拼接上/home/data ,例如

    location /images {
        root   /home/data;
    }
    

    此时访问路径中匹配到/images时,则会在前面拼接上/home/data ,即/home/data/images/

    客户端浏览器访问http://www.another.ren

    返回服务器的/home/data/index.html

    在这里插入图片描述

    访问http://www.another.ren/images/another.jpg

    返回服务器的/home/data/images/another.jpg

    在这里插入图片描述

    访问 http://www.another.ren/audios/透明.mp3

    返回服务器的/home/data/audios/透明.mp3
    在这里插入图片描述

  4. 参考

    dockerhub nginx镜像
    nginx 静态文件服务器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值