nginx+tomcat实现简单的负载均衡

Posted 苦咖啡~~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+tomcat实现简单的负载均衡相关的知识,希望对你有一定的参考价值。

host1:10.0.0.10(部署nginx和tomcat)

host2:10.0.0.11(部署tomcat)

平台环境(2主机一样)

[root@smp ~]# uname -r
3.10.0-862.el7.x86_64
[root@smp ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core)

host1及host2上操作:

安装tomcat

https://www.cnblogs.com/liliyang/p/9742284.html

按照上面的博文将将两台机器上的tomcat均部署好,

然后访问 

 

host1上安装nginx

yum install  wget -y

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum install  nginx  -y

cat  /etc/nginx/nginx.conf
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 2048; use epoll; } 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; include /etc/nginx/conf.d/*.conf; upstream tomcatservers {     #这里注意tomcatserver为池的名称,这里的名称可任意取,但是名称内不能有下划线(有的书中使用了下划线)!!!!!!! server 10.0.0.10:8080 weight=1; server 10.0.0.11:8080 weight=1; } server { listen 80 default_server; listen [::]:80 default_server; server_name 10.0.0.10; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://tomcatservers;    #这里的名称必须是服务池的名称 proxy_redirect default; }                          #除了红色部分其余均为系统默认配置 error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }

[root@smp nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@smp nginx]# systemctl start nginx

访问测试:

 

以上是关于nginx+tomcat实现简单的负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

Nginx+Tomcat实现负载均衡

nginx+tomcat负载均衡实验

Nginx+Tomcat实现负载均衡小demo

Nginx+Tomcat实现动静分离和负载均衡

通过Nginx+tomcat+redis实现反向代理 负载均衡及session同步

Linux下Nginx+多Tomcat负载均衡实现详解