CentOS7+Nginx配置Tomcat负载均衡环境

本文涉及的产品
网络型负载均衡 NLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
传统型负载均衡 CLB,每月750个小时 15LCU
简介: 1.准备两个Tomcat 配置两个Tomcat一个端口是8080另外一个端口是8081,分别在webapps下面添加一个测试用的web项目,修改index.jsp文件,8080端口的index.jsp页面加入: This page is from 8080 port 8081的端口的index.

1.准备两个Tomcat

配置两个Tomcat一个端口是8080另外一个端口是8081,分别在webapps下面添加一个测试用的web项目,修改index.jsp文件,8080端口的index.jsp页面加入:

<h2>This page is from  8080 port </h2>

8081的端口的index.jsp加入:

<h2>This page is from  8081 port </h2>

之后启动两个Tomcat,8080端口跟8081端口都要开放

开放端口:
firewall-cmd --zone=public --add-port=8080/tcp --permanent

查看开放的端口:
firewall-cmd --list-ports

  

2.配置Nginx

使用whereis nginx命令找到nginx所在的目录并进入目录内的conf文件夹找到ngnix.conf配置文件,在conf文件夹下创建一个vhosts文件夹并在里面创建一个webapp.conf文件内容如下:

upstream tomcat {  #这里的名称要跟proxy_pass内的名称一致
	server 127.0.0.1:8080 weight=1;
	server 127.0.0.1:8081 weight=1; 
}  
  
server {  
	listen 80;  
	server_name bbs.goodapp.net;  
	access_log logs/bbs.access.log;  
	error_log logs/bbs.error.log;  
	#root html;  
	#index index.html index.htm index.jsp index.php;  
  
	location / {  
		proxy_pass http://tomcat/App.Web/;
  
		#Proxy Settings  
		proxy_redirect off;  
		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 invalid_header http_500 http_502 http_503 http_504;  
		proxy_max_temp_file_size 0;  
		proxy_connect_timeout 90;  
		proxy_send_timeout 90;  
		proxy_read_timeout 90;  
		proxy_buffer_size 4k;  
		proxy_buffers 4 32k;  
		proxy_busy_buffers_size 64k;  
		proxy_temp_file_write_size 64k;  
	}  
}  

接着将创建的vhosts/webapps.conf文件include到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;

    #gzip  on;

    include vhosts/webapp.conf;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           root   html;
            index  index.html index.htm;
        }
	

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

重新加载Nginx:

./nginx -s reload

在地址栏输入地址看下效果:

把8080端口的Tomcat关闭后再访问:

3.随机启动

通过vi /lib/systemd/system/nginx.service来添加nginx.service文件,并输入如下内容:

[Unit]  
Description=nginx 1.13.7  
After=network.target  
  
[Service]  
Type=forking  
ExecStart=/usr/local/nginx/sbin/nginx  
ExecReload=/usr/local/nginx/sbin/nginx -s reload  
ExecStop=/usr/local/nginx/sbin/nginx -s quit  
PrivateTmp=true  
  
[Install]  
WantedBy=multi-user.target  

注意:如果不是安装在/usr/local/nginx/目录下,请根据实际安装路径修改ExecStart、ExecReload、ExecStop中的值。

更改nginx.service为可执行:

chmod 755 /lib/systemd/system/nginx.service  

设置开机自启动

systemctl enable nginx.service  

 

相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
5天前
|
存储 应用服务中间件 Linux
nginx配置证书和私钥进行SSL通信验证
nginx配置证书和私钥进行SSL通信验证
20 4
|
15天前
|
网络协议 Java 应用服务中间件
centos7环境下tomcat8的安装与配置
本文介绍了在Linux环境下安装和配置Tomcat 8的详细步骤。首先,通过无网络条件下的文件交互软件(如Xftp 6或MobaXterm)下载并解压Tomcat安装包至指定路径,启动Tomcat服务并测试访问。接着,修改Tomcat端口号以避免冲突,并部署Java Web应用项目至Tomcat服务器。最后,调整Linux防火墙规则,确保外部可以正常访问部署的应用。关键步骤包括关闭或配置防火墙、添加必要的端口规则,确保Tomcat服务稳定运行。
|
3月前
|
负载均衡 监控 应用服务中间件
配置Nginx反向代理时如何指定后端服务器的权重?
配置Nginx反向代理时如何指定后端服务器的权重?
195 61
|
2月前
|
负载均衡 Ubuntu 应用服务中间件
nginx修改网站默认根目录及发布(linux、centos、ubuntu)openEuler软件源repo站点
通过合理配置 Nginx,我们可以高效地管理和发布软件源,为用户提供稳定可靠的服务。
147 13
|
2月前
|
存储 应用服务中间件 nginx
nginx反向代理bucket目录配置
该配置实现通过Nginx代理访问阿里云OSS存储桶中的图片资源。当用户访问代理域名下的图片URL(如 `http://代理域名/123.png`)时,Nginx会将请求转发到指定的OSS存储桶地址,并重写路径为 `/prod/files/2024/12/12/123.png`。
83 5
|
2月前
|
负载均衡 前端开发 应用服务中间件
负载均衡指南:Nginx与HAProxy的配置与优化
负载均衡指南:Nginx与HAProxy的配置与优化
123 3
|
2月前
|
负载均衡 网络协议 算法
Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式
本文探讨了Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式,以及软件负载均衡器、云服务负载均衡、容器编排工具等实现手段,强调两者结合的重要性及面临挑战的应对措施。
108 3
|
3月前
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
141 3
|
3月前
|
安全 应用服务中间件 网络安全
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
213 3
|
网络协议 应用服务中间件 nginx