随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

nginx + tomcat配置负载均衡

目标:Nginx做为HttpServer,连接多个tomcat应用实例,进行负载均衡。

注:本例程以一台机器为例子,即同一台机器上装一个nginx和2个Tomcat且安装了JDK1.7。

1、安装Nginx
安装Nginx教程

2、配置两个Tomcat
在本机上配置两个Tomcat,分别为tomcat7-8081、tomcat7-8082。
tomcat7-8081访问地址:http://localhost:8081,浏览显示内容:this is 8081 port
tomcat7-8082访问地址:http://localhost:8082,浏览显示内容:this is 8082 port

D:\div\tomcat7-8081\webapps\ROOT\index.jsp文件内容为:

<!DOCTYPE html>

<html lang="en">
    <head>this is 8081 port</head>
</html>

D:\div\tomcat7-8082\webapps\ROOT\index.jsp文件内容为:

<!DOCTYPE html>

<html lang="en">
    <head>this is 8082 port</head>
</html>

在同一台服务器上配置多个Tomcat教程

这样我们就成功的搭建了一个nginx服务,成功的配置了两个tomcat应用实例。

3、Nginx+Tomcat负载均衡配置
这里只需要修改Nginx的配置,让它通过tomcat来转发。
a、nginx.conf配置文件

复制代码
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  10;
    include extra/upstream01.conf;
}
复制代码

 

b、extra/upstream01.conf文件,负载均衡配置信息

复制代码
upstream mysite {
    server localhost:8081 weight=5;
    server localhost:8082 weight=5;
}
 
server {
    listen 80;
    server_name localhost;
  
    location / {
    proxy_pass http://mysite;
    }
}
复制代码

当有请求到localhost时,请求会被分发到对应的upstream设置的服务器列表上,每一次请求分发的服务器都是随机的。

接着在运行一次start nginx,当你不断刷新http://localhost的时候,浏览器上就会来回切换"this is 8081 port"和"this is 8082 port"。

这样说明负载均衡配置成功了!!!!!!

posted on   Ruthless  阅读(3964)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2011-05-13 六、把文件存放在SDCard
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示