您现在的位置是:首页 > 学无止境 > nginx

nginx实现负载均衡

贾继涛 2019-03-05 2058人围观
nginx 

注意:前提需要安装docker及docker-compose

docker安装卸载及配置镜像加速器

 docker Compose安装与卸载

  1. 在虚拟机利用docker-compose创建tomcat俩个

version: '3'
services:
  tomcat1:
    image: tomcat:9.0.16-jre8
    container_name: tomcat1
    ports:
    - 9090:8080
  tomcat2:
    image: tomcat:9.0.16-jre8
    container_name: tomcat2
    ports:
    - 9091:8080


2.docker-compose.yaml启动nginx

version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
    - 81:80
    volumes:
    - ./conf/nginx.conf:/etc/nginx/nginx.conf
    - ./wwwroot:/usr/share/nginx/wwwroot


3.修改 /usr/local/docker/nginx/conf 目录下的 nginx.conf 配置文件:

user  nginx;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;	
	upstream mytom {
		server 192.168.102.130:9090 weight=10;
		server 192.168.102.130:9091 weight=10;
	}
	server {
		listen 80;
		server_name 192.168.102.130;
		location / {
			proxy_pass http://mytom;
			index index.jsp index.html index.htm;
		}
	}
}


4.修改index.jsp页面

docker exec -it tomcat1 bash
echo "9090" >> /usr/local/tomcat/webapps/ROOT/index.jsp
exit
docker exec -it tomcat2  bash
echo "9091" >> /usr/local/tomcat/webapps/ROOT/index.jsp
exit


5.访问如下地址 查看页面底部9090和9091变化

http://192.168.102.130:81/




您的支持