Docker构建nginx并挂载外部配置文件

1.首先pull下载nginx镜像包

        docker pull nginx 

2.查看nginx镜像里面配置文件、日志等文件的具体位置,便于最后覆盖这些路径

        docker run -i -t nginx /bin/bash 进入nginx容器 

        找到镜像中nginx.conf配置文件路径

        nginx.conf :/etc/nginx/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;
}

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

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index 1.html;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

        log文件夹:/var/log/nginx

        

<html>

<head>

<title>Mynginx</title>

</head>

<body>

<h1>

my nginx!

</h1>

</body>

</html>

3.创建本地挂载文件,并创建文件放入对应文件夹,详细配置根据上一步文件修改

mkdir -p /home/app/nginx/conf    nginx.conf存放位置

mkdir -p /home/app/nginx/conf.d  default.conf存放位置

mkdir -p /home/app/nginx/html         html存放位置

mkdir -p /home/app/nginx/logs           log文件存放位置(自动生成log文件只需创建文件夹即可)

4.创建容器并且挂载文件和文件夹

docker run --name mynginx -d -p 8501:80 -v /home/app/nginx/html:/usr/share/nginx/html -v /home/app/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/app/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /home/app/nginx/logs:/var/log/nginx nginx

docker run  --name mynginx -d -p 8501:80

-v /home/app/nginx/html:/usr/share/nginx/html

-v /home/app/nginx/conf/nginx.conf:/etc/nginx/nginx.conf

-v /home/app/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf

-v /home/app/nginx/logs:/var/log/nginx nginx

挂载的目标目录或者文件路径要与第二步里面已经找出来的镜像中的路径保持一致

5.docker ps 查看有没有启动成功

6.http://IP:端口  打开网页查看

=========================================================================

nginx常见配置:

负载均衡:

1.在http部分添加

upstream myserver{        //upstream是关键字  myserver是自定义的需要负载均衡服务的名称
    server ip1:端口1;
    server ip2:端口2;
    }

2.在server部分添加

 location /fz/ {
        proxy_pass http://myserver;
    }

 反向代理:

在server部分添加一个location即可:下图是代理了天猫首页实例(若在路径上配置了正则就不能代理具体的url地址,会引起nginx启动错误

location /tm/ {
        proxy_pass https://www.tianmao.com/;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值