docker容器 搭建nginx实现简单的nginx负载均衡

docker容器下的nginx服务的负载均衡

1:首先创建四个nginx容器服务

	docker run -d --name masters -p 80:80  ecd67fe340f9  #运行一个80端口的Master
	docker run -d --name client1 -p 90:80  ecd67fe340f9  #运行第一个90端口服务器
	docker run -d --name client2 -p 100:80 ecd67fe340f9  #运行第二个100端口服务器
	docker run -d --name client3 -p 110:80 ecd67fe340f9  #运行第三个110端口服务器

2:docker ps 查看在运行的nginx服务器

[root@docker_compose ~]# docker ps
	CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
	df67c47d8f13        ecd67fe340f9        "/docker-entrypoin..."   6 minutes ago       Up 6 minutes        0.0.0.0:110->80/tcp   client3
	d114ca2d70a8        ecd                 "/docker-entrypoin..."   About an hour ago   Up About an hour    0.0.0.0:100->80/tcp   client2
	8920b72aae43        ecd                 "/docker-entrypoin..."   About an hour ago   Up About an hour    0.0.0.0:90->80/tcp    client1
	751a81817350        ecd                 "/docker-entrypoin..."   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp    master

3:进入docker中的名字为master的nginx容器

	docker exec -it ecd67fe340f9 /bin/sh					#进入容器

编辑/etc/ngonx/conf.d

	添加前五行
	upstream mynginx {
        server 192.168.78.100:90;
        server 192.168.78.100:100;
		server 192.168.78.100:110;
	}
	server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

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

    location / {
        proxy_pass http://mynginx;
        index  index.html index.htm;
    }

    #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;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
                                                                   
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #                                                                   
    #location ~ \.php$ {                                                
    #    root           html;                                           
    #    fastcgi_pass   127.0.0.1:9000;                                 
    #    fastcgi_index  index.php;                                      
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
    #    include        fastcgi_params;                                 
    #}                                                                  
                                                                        
    # deny access to .htaccess files, if Apache's document root         
    # concurs with nginx's one                                          
    #                                                                   
    #location ~ /\.ht {                                                 
    #    deny  all;                                                     
    #}                                                                  
}      
	配置完毕后,就可以使用了,需要重启master容器的nginx服务,其他的不需要重启

测试结果

在这里插入图片描述

测试结束
(qq:九七二四三九三二九(972439329),有哪里出错欢迎指正,大家一起学习交流)

End

猜你喜欢

转载自blog.csdn.net/weixin_45005209/article/details/107490316