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