Nginx reverse proxy multiple tomcat, load balancing

One, forward proxy and reverse proxynginx

  1. Forward proxy

正向proxy


  1. Back to proxy nginx
客户端发送请求到服务器(客户端认为是原始服务器,
实际上是一台反向代理服务器),
反向代理服务器接收请求并将请求
发送给内部网络中的多台集群服务器,
并将响应的数据返回给客户端。
一般用于服务器集群,分布式等,
实现负载均衡,提高处理和响应速度,
保证内网安全,隐藏服务器信息,防止Web攻击。

According to a specific strategy: Distribute requests to designated server ports. Nginx returns the data to the client.

1. 虚拟主机:把一台物理服务器划分为多个虚拟服务器,每个虚拟主机对应一个Web站点
2. 负载均衡,同一个ip,映射多个端口,每个端口放一样的程序。

Two, nginx installation

# 添加 Nginx 源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

# 安装 Nginx
sudo yum install -y nginx

# 启动 Nginx
sudo systemctl start nginx.service

# 设置开机自启 Nginx
sudo systemctl enable nginx.service

Three, nginx load balancing

The same ip maps multiple ports, and each port puts the same program.

Scenes

A cat can only support 1000 concurrency, at a certain point in time, only 1000 people can visit, otherwise the server will hang.

Add tomcat, but each cat will occupy a port. If so, a website only maps one port. The user will not type the port to use your po software...

If you use nginx to distribute the user's request to a port, such as the port 80 below (80 is hidden).

http://java.com

Through a certain configuration, nginx monitors port 80, distributes requests to a certain tomcat, and responds to the content you want.

If there are more users, continue to add servers to form a server cluster, each server can have multiple tomcats.


Now that the server is set up, what if nginx goes down? Heartbeat mechanism, using keepalive, high availability.
Used to monitor the nginx server.

Keepalived is a service software that guarantees the high availability of the cluster in cluster management to prevent single points of failure.
The function of Keepalived is to detect the status of the web server. If a web server crashes or malfunctions, Keepalived will detect it and remove the faulty web server from the system. When the web server works normally, Keepalived will automatically remove the web server. Join the server group, all these tasks are completed automatically, no manual intervention is required, and the only thing that needs to be done manually is to repair the faulty web server

Fourth, configure nginx to access multiple tomcat agents

Only two ports are proxy here.
Reverse proxy strategy: reflected in the configuration.

描述:
轮询:你分配一下,我分配一下
随机:看nginx心情
权重:设置优先级
就近:服务器离用户比较近

你需要:
服务器上有两个tomcat,一个不改变,
第二个猫复制,改变端口就行了,保证所有端口和第一个不一样。并且能正常启动。
保证tomcat的首页不一样,上传不同的项目到tomcat测试。
安装nginx。

Configuration:

1. 进入nginx目录
cd /etc/nginx/config.d
2. 新建tomcat.conf,以.conf结尾的文件
vim tomcat.conf

Copy the following code into it, esc and press p

# 两个端口,会被代理
upstream  abc.cn {
    
     # abc.cn 可以随便取,但是要同下面location-pass 的一样。

    server    127.0.0.1:8081;  #weight=10;
    server    127.0.0.1:8082; # weight=3;

}


# 这里是监听8080端口
server {
    
    
    listen        8080;
    server_name   localhost;
    location / {
    
    
         proxy_pass http://abc.cn; # 同上
         proxy_redirect default;
    }

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
    
    
        root   html;
    }

}

3. 按esc,然后输入:wq保存退出
4. nginx -t 检查语法
5. nginx -s reload 重启nginx

Test: The
above configuration, visit localhost:8080. If you did not upload the project to tomcat, all you see is the home page of tomcat.

Guess you like

Origin blog.csdn.net/qq_44783283/article/details/109952755