docker-nginx负载均衡

1 用docker在同一台服务器上搭建nginx负载均衡

(1)两个php服务器分别处理俩个php请求分别为php和php1

(2)三个nginx,一个作为分发nginx,两个处理请求nginx1,和nginx2

保证服务器已经安装docker和docker-compose

创建文件

sudo  mkdir -p /home/log/nginx && mkdir /home/log/nginx/nginx1 && mkdir /home/log/nginx/nginx2 &&
 mkdir -p /home/conf/nginx  && mkdir /home/conf/nginx/conf.d  && mkdir /home/conf/nginx/conf1.d  &&
  mkdir /home/conf/nginx/conf2.d  && mkdir -p /home/www/project1 && mkdir /home/www/project2

nginx配置文件内容

upstream web-nginx {
    server nginx1:8080;
    server nginx2:8081;
}

server {
    listen 80;
    server_name  localhost;

    location / {
        proxy_pass         http://web-nginx;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

nginx1配置文件内容

server {
    listen       8080;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html/public;
        index  index.html index.htm;
    }
    autoindex  off;
   if (!-e $request_filename) {
           rewrite ^(.*)$ /index.php?s=/$1 last;
            break;
   }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/public;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

nginx2配置文件内容

server {
    listen       8081;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html/public;
        index  index.html index.htm;
    }
    autoindex  off;
   if (!-e $request_filename) {
           rewrite ^(.*)$ /index.php?s=/$1 last;
            break;
   }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/public;
    }

    location ~ \.php$ {
        fastcgi_pass   php1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

构建docker-compose.yml

version: "1.0.0"
networks:  #添加网络
   web-net:
    driver: bridge
services:
  php:
      image: php-test:7.4-fpm
      container_name: php-fpm-1
      privileged: true #设置容器权限为root
      volumes:
        - /home/www/project1:/var/www/html
      networks:
        - web-net
  php1:
      image: php-test:7.4-fpm
      container_name: php-fpm-2
      privileged: true #设置容器权限为root
      volumes:
        - /home/www/project2:/var/www/html
      networks:
        - web-net
  nginx:
      image: nginx:1.15
      container_name: php-nginx-1
      privileged: true
      ports:
        - "80:80"
      volumes:
        - /home/log/nginx:/var/log/nginx
        - /home/conf/nginx/conf.d:/etc/nginx/conf.d
      networks:
        - web-net
      restart: always
  nginx1:
      image: nginx:1.15
      container_name: php-nginx-2
      privileged: true
      ports:
        - "8080:8080"
      volumes:
        - /home/www/project1:/usr/share/nginx/html
        - /home/log/nginx/nginx-1:/var/log/nginx
        - /home/conf/nginx/conf1.d:/etc/nginx/conf.d
      networks:
        - web-net
      restart: always
      links:
        - php-fpm-1:php1
  nginx2:
      image: nginx:1.15
      container_name: php-nginx-3
      privileged: true
      ports:
        - "8081:8081"
      volumes:
        - /home/www/project2:/usr/share/nginx/html
        - /home/log/nginx/nginx-2:/var/log/nginx
        - /home/conf/nginx/conf2.d:/etc/nginx/conf.d
      networks:
        - web-net
      restart: always
      links:
        - php-fpm-2:php2
  mysql:
      image: mysql:5.7
      container_name: php-mysql-1
      privileged: true
      ports:
        - "3307:3306"
      volumes:
        - /home/data/mysql:/var/lib/mysql
        - /home/conf/mysql:/etc/mysql
        - /home/log/mysql:/var/log
      environment:
        - MYSQL_ROOT_PASSWORD=123456
      networks:
        - web-net

都完成后运行docker-compose up -d 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值