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负载均衡已完成。


posted @   Reasonzzy  阅读(627)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示
主题色彩