Docker Nginx 负载均衡配置

nginx容器启动命令:

docker run --name nginx89 -d -p 89:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx

修改了配置,重启容器命令:

docker container restart nginx89 

配置nginx宿主机使用目录,将nginx使用的配置文件,和日志输出到宿主机

[root@localhost nginx]# pwd
/data/nginx
[root@localhost nginx]# ll
总用量 0
drwxr-xr-x. 2 root root 24 3月  27 23:13 conf
drwxr-xr-x. 2 root root 26 3月  27 23:25 conf.d
drwxr-xr-x. 2 root root  6 3月  27 17:43 html
drwxr-xr-x. 2 root root 41 3月  27 22:31 logs
[root@localhost nginx]#

在conf目录下面,建立nginx入口配置文件nginx.conf,配置内容如下



    upstream pic {
        server 172.17.0.2:8080;
        server 172.17.0.3:8080;
    }

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


在conf.d目录下面建立各个模块的配置文件 ,内容如下: 

server{
        listen  80;
        server_name localhost ;
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-Nginx-Proxy true;
                proxy_pass http://pic;
                proxy_redirect off;
        } 

}


在宿主机上面,启动了两个容器,容器中是对应的Tomcat服务 ,容器启动之后有自己对应的ip,可以使用对应的ip访问服务 

分别为:172.17.0.2:8080,172.17.0.3:8080 

发布了95 篇原创文章 · 获赞 50 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/jc0803kevin/article/details/88847159