欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实现Nginx+Tomcat负载均衡

程序员文章站 2022-07-13 09:31:51
...

1、环境:

1、操作系统:Centos7.6

2、服务器配置如下:

服务器 软件
182.92.220.145 Nginx
123.206.63.55 Tomcat
132.232.125.97 Tomcat

2、安装Nginx

2.1 下载

`进入到/usr/local/src目录下`
[aaa@qq.com ~]# cd /usr/local/src

`下载nginx`
[aaa@qq.com src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

`解压缩`
[aaa@qq.com src]# tar -zxvf nginx-1.16.1.tar.gz


2.2安装

`进入到/usr/local/src/nginx-1.16.1目录中`
[aaa@qq.com nginx-1.16.1]# cd /usr/local/src/nginx-1.16.1
[aaa@qq.com nginx-1.16.1]# ./configure --prefix=/usr/local/nginx

`编译安装`
[aaa@qq.com nginx-1.16.1]# make && make install

编译过程中可能会出现如下常见问题:

1、缺少PCRE

`-------错误信息---------------`
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
  
`----- ---解决方案--------------------`
  yum -y install pcre-devel

2、缺少zlib

`--------错误信息---------------`
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

`-------解决方案--------------------`
yum -y install zlib-devel

2.3 启动nginx

`启动nginx服务`
[aaa@qq.com nginx]# cd /usr/local/nginx/sbin
[aaa@qq.com sbin]# ./nginx
`查看nginx启动情况`
[aaa@qq.com sbin]# ps -ef|grep nginx
root     16374     1  0 23:12 ?        00:00:00 nginx: master process ./nginx
nobody   16375 16374  0 23:12 ?        00:00:00 nginx: worker process
root     16377 11301  0 23:12 pts/0    00:00:00 grep --color=auto nginx

2.4 防火墙

为方便测试,我直接关闭了防火墙,在实际应用中可以根据需要开启防火墙的端口,此外还要设置服务器的安全策略,我的是阿里云的服务器,就在阿里云服务器控制台设置了安全策略,开放了需要的端口。

  • 关闭防火墙:
`关闭防火墙`
[aaa@qq.com sbin]# systemctl stop firewalld

`查看防火墙状态`
[aaa@qq.com sbin]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

Mar 07 22:39:08 henry003 systemd[1]: Starting firewalld - dynamic firewall daemon...
Mar 07 22:39:08 henry003 systemd[1]: Started firewalld - dynamic firewall daemon.
Mar 07 22:39:23 henry003 systemd[1]: Stopping firewalld - dynamic firewall daemon...
Mar 07 22:39:24 henry003 systemd[1]: Stopped firewalld - dynamic firewall daemon.

2.5 访问

访问后可以看到nginx熟悉的界面。
实现Nginx+Tomcat负载均衡

3 、Tomcat

tomcat安装就不在此介绍了,我已在另外两台服务器分别安装了tomcat。接下来就进行nginx的配置,来实现负载均衡。

4、配置nginx

4.1nginx配置文件

`进入到nginx配置目录下`
[aaa@qq.com conf]# cd /usr/local/nginx/conf
`编辑nginx.conf`
[aaa@qq.com conf]# vim nginx.conf

nginx.conf文件如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

	#引入文件
    include ../extra/*.conf;   
    gzip on;
    gzip_min_length 5k;
    gzip_comp_level 3;
    gzip_types application/javascript image/jpeg image/svg+xml image/png;
    gzip_buffers 4 32k;
    gzip_vary on;

}

4.2 外链一个配置文件

创建extra文件夹,并创建文件tomcat.conf

[aaa@qq.com nginx]# mkdir /usr/local/nginx/extra
[aaa@qq.com nginx]# cd extra/
[aaa@qq.com nginx]# vim tomcat.conf

tomcat.conf文件如下:

upstream tomcat {
    server 123.206.63.55:8090 max_fails=2 fail_timeout=60s;
    server 132.232.125.97:8090;
}
server {
    listen 80;
    server_name localhost;
    location / {
        proxy_pass http://tomcat;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout http_500 http_503;
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET,POST,DELETE';
        add_header 'Aceess-Control-Allow-Header' 'Content-Type,*';
	}
	#location ~ .*\.(js|css|png|svg|ico|jpg)$ {
		#防盗链
    #    valid_referers none blocked 123.206.63.55;
    #    if ($invalid_referer) {
    #    return 404;
    #    }
    #    root static-resource;
    #    expires 1d;
	#}
}

5、访问

为了区分两个tomcat,修改了其中一个tomcat的首页:

实现Nginx+Tomcat负载均衡

  • 访问Nginx地址

实现Nginx+Tomcat负载均衡
再次刷新页面,可以访问到另外一个Tomcat:

实现Nginx+Tomcat负载均衡

结语:

到此nginx的配置就已经完成了,要想了解LVS+KeepAlived+Nginx+Tomcat高可用解决方案,可以访问以下地址。
LVS+KeepAlived+Nginx+Tomcat高可用解决方案https://blog.csdn.net/qq_33996921/article/details/105000294