1.安装nginx

参考地址:https://www.runoob.com/docker/docker-install-nginx.html
1.1拉取镜像

[root@localhost ~]# docker pull nginx

1.2查看镜像

[root@localhost ~]# docker images

如何修改docker中nginx 配置文件 docker nginx 配置_nginx


1.3使用 NGINX 默认的配置来启动一个 Nginx 容器实例:

[root@localhost ~]#docker run --name runoob-nginx-test -p 8090:80 -d nginx

说明:
runoob-nginx-test 容器名称。
the -d设置容器在在后台一直运行。
the -p 端口进行映射,将本地 8090 端口映射到容器内部的 80 端口。

如何修改docker中nginx 配置文件 docker nginx 配置_nginx_02

访问nginx:地址看您当前的主机ip ip:8090

如何修改docker中nginx 配置文件 docker nginx 配置_html_03

2.部署反向代理的nginx

2.1创建目录

[root@localhost ~]# mkdir -p /var/docker-nginx

2.2创建反向代理的nginx

[root@localhost docker-nginx]# cd /var/docker-nginx
[root@localhost docker-nginx]# mkdir -p ./nginx-profix/www ./nginx-profix/logs ./nginx-profix/conf

2.3拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录,容器 ID 可以查看 docker ps 命令输入中的第一列:

[root@localhost ]# docker ps -a

如何修改docker中nginx 配置文件 docker nginx 配置_docker_04

复制nginx.conf

[root@localhost docker-nginx]# docker cp b1273727b1a0:/etc/nginx/nginx.conf ./nginx-profix/conf/

进入nginx-profix目录

[root@localhost docker-nginx]# cd nginx-profix

2.4在nginx-profix目录下执行容器创建命令:

[root@localhost nginx-profix]# docker run -d -p 8091:80 --name nginx-second -v $PWD/www:/usr/share/nginx/html -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/var/log/nginx nginx

命令说明:

-p 8082:80: 将容器的 80 端口映射到主机的 8091 端口。

--name runoob-nginx-test-web:将容器命名为 runoob-nginx-test-web。

-v ~/nginx/www:/usr/share/nginx/html:将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html。

-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。

-v ~/nginx/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。

查看创建好的容器:

[root@localhost nginx-profix]# docker ps -a

如何修改docker中nginx 配置文件 docker nginx 配置_html_05


访问nginx:地址看您当前的主机ip ip:8091【自行查看】

3.1部署服务nginx[需要部署两台以上的nginx服务]

3.1创建服务nginx-one

[root@localhost nginx-profix]# cd ..
[root@localhost docker-nginx]# mkdir -p nginx-one/conf/ nginx-one/www/ nginx-one/logs/

3.2复制nginx.conf

[root@localhost docker-nginx]# docker cp b1273727b1a0:/etc/nginx/nginx.conf ./nginx-one/conf/

3.3进入nginx-one目录执行容器创建

[root@localhost docker-nginx]# cd nginx-one/
[root@localhost nginx-one]# docker run -d -p 8092:80 --name nginx-one -v $PWD/www:/usr/share/nginx/html -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/var/log/nginx nginx

查看创建好的容器:

[root@localhost nginx-one]# docker ps -a

如何修改docker中nginx 配置文件 docker nginx 配置_nginx_06

3.4进入www目录创建文件:

[root@localhost nginx-one]# cd www/

创建 index.html 文件,内容如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>8092</title>
</head>
<body>
    <h6>我的第一个标题</h6>
    <p>nginx-one 8092</p>
</body>
</html>

访问nginx:地址看您当前的主机ip ip:8092【自行查看】

3.5创建服务nginx-second

[root@localhost docker-nginx]# mkdir -p nginx-second/conf/ nginx-second/www/ nginx-second/logs/

3.6复制nginx.conf

[root@localhost docker-nginx]# docker cp b1273727b1a0:/etc/nginx/nginx.conf ./nginx-second/conf/

3.7进入nginx-second目录执行容器创建

[root@localhost nginx-second]#docker run -d -p 8093:80 --name nginx-second -v $PWD/www:/usr/share/nginx/html -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/var/log/nginx nginx

如何修改docker中nginx 配置文件 docker nginx 配置_docker_07

3.8进入www目录创建文件:

[root@localhost nginx-second]# cd www/

创建 index.html 文件,内容如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>8092</title>
</head>
<body>
    <h6>我的第一个标题</h6>
    <p>nginx-one 8093</p>
</body>
</html>

访问nginx:地址看您当前的主机ip ip:8093【自行查看】

3.9重复步骤就能创建多台服务
3.1->3.2->3.3->3.4 [注意要修改文件目录,容器名称,端口]

4.配置反向代理的nginx

4.1查看服务nginx的ip地址【查看容器的信息】
命令参考:https://www.jianshu.com/p/7c9e2247cfbd

[root@localhost docker-nginx]# docker inspect nginx-one

如何修改docker中nginx 配置文件 docker nginx 配置_docker_08

[root@localhost docker-nginx]# docker inspect nginx-second/

如何修改docker中nginx 配置文件 docker nginx 配置_html_09


4.2配置反向代理nginx的配置文件

[root@localhost nginx-profix]# vim conf/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;

#添加反向代理的配置
 upstream backserver {
    server 172.17.0.4:80;
    server 172.17.0.5:80;
 }

    server {
    listen       80;
    server_name  localhost;

    location / {
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
        proxy_pass  http://backserver;
        index  index.html index.htm;
        proxy_connect_timeout 1;
        proxy_send_timeout 1;
        proxy_read_timeout 1;
    }

    }
}

截图说明:

如何修改docker中nginx 配置文件 docker nginx 配置_docker_10


4.3重启反向代理的nginx

[root@localhost nginx-profix]# docker restart nginx-profix

或者:

[root@localhost nginx-profix]# docker restart 2f743964e761

如何修改docker中nginx 配置文件 docker nginx 配置_docker_11

4.4访问 IP:8091 [访问方向代理的机器]

如何修改docker中nginx 配置文件 docker nginx 配置_html_12


反复刷新访问

如何修改docker中nginx 配置文件 docker nginx 配置_html_13


如果有多台nginx服务会根据你配置的算法跳转到对应的服务