VM的虚拟机中装的ubuntu,使用Docker-compose创建了三个容器:
Nginx-services 访问端口映射为宿主机器的9000
apache01 访问端口映射为宿主机器的9001
apache02 访问端口映射为宿主机器的9002
单独访问以上三个服务器,都可以返回根目录的index.html(我在每一个服务器根目录放了一个静态文件,内容都不一样)
但是无法实现负载均衡, 访问9000 的时候,请求没有转发到9001 和9002 上面。
我的配置文件如下:
nginx.conf
http {
upstream webpools {
server php-apache01:80 weight=1;
server php-apache02:80 weight=1;
}
server {
listen 9000;
index index.php index.html;
server_name localhost;
location / {
# root ./opt/webapps;
# index index.html;
proxy_pass http://webpools/ ;
}
}
}
~
docker-compoes.yml
version: '3'
services:
#Nginx-services
nginx-service:
image: nginx
links:
- php-apache01
- php-apache02
volumes:
- ./webapps/nginxapps/:/usr/share/nginx/html/
# - ./nginx/logs:/usr/local/nginx/logs
# - ./etc/localtime:/etc/localtime
- ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
- ./nginx/conf.d/:/usr/local/nginx/conf.d/
ports:
- 9000:80
- 443:443
restart: always
#php-apache
php-apache01:
image: php:apache
volumes:
- ./webapps/01:/var/www/html
ports:
- 9001:80
php-apache02:
image: php:apache
volumes:
- ./webapps/02:/var/www/html
ports:
- 9002:80
nginx 配置中 listen 9000;
改为 listen 80;