Docker中nginx+tomcat实现负载均衡
拉取tomcat镜像
docker pull tomcat
运行两个tomcat容器
docker run -d -p 8088:8080 --name tomcat8088 tomcat
docker run -d -p 8089:8080 --name tomcat8089 tomcat
修改下index.jsp把两个tomcat区别开来
docker ps
docker exec -it 01c626690608 /bin/bash cd webapps/ROOT/ echo "<h1>Apache Tomcat 8088</h1>" >index.jsp
cat index.jsp
exit
docker restart 01c626690608
echo "test" >index.jsp 替换index.jsp的内容
echo "test" >>index.jsp 在文件末尾追加内容
另外一个也是一样的
获取nginx镜像
docker pull nginx
创建目录
mkdir -p /data/nginx/{conf,conf.d,html,logs}
创建配置文件nginx.conf
vim /data/nginx/conf/nginx.conf
编辑内容
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name 192.168.1.4; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://pic; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } upstream pic{ server 192.168.1.4:8088 weight=5; server 192.168.1.4:8089 weight=5; } }
创建nginx容器
docker run --name mynginx -d -p 80:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -d docker.io/nginx
访问页面

--

至此nginx负载均衡已完成。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律