获取nginx镜像
docker pull nginx
编写nginx配置文件
hello.conf,这里方便搞成一个文件,多个站点最好拆分成多个,这里配置两个站点的负载均衡
upstream hello { server 192.168.0.108:8888; server 192.168.0.108:8889; } server { listen 8899; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; proxy_set_header Host $host; proxy_pass http://hello; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } upstream haha { server 192.168.0.108:8888; server 192.168.0.108:8889; } server { listen 8877; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; proxy_set_header Host $host; proxy_pass http://haha; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
运行nginx 容器
我们需要将端口映射出来
docker run -d --name nginx-gate -p 8899:8001 -p 8877:8002 nginx
拷贝 配置文件到容器内,或者直接将配置文件夹映射到磁盘
docker cp hello.conf nginx-gate:/etc/nginx/conf.d/
重启容器:
docker restart nginx-gate