Centos7环境下搭建Nginx反向代理多个Tomcat的负载均衡集群

我要、发光_2332019-03-22 23:25:34

分类专栏:大学作业
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
版权

本文原文链接: https://blog.csdn.net/java_kider/article/details/81675666

配合上一篇博文体验更佳:https://mp.csdn.net/postedit/88750139

1.Tomcat基础配置 :

#安装JDK1.8#安装tomcat7.0.93cd /usr/local/src#rz上传apache-tomcat-7.0.93.tar.gztar -zxvf apache-tomcat-7.0.93.tar.gz -C /usr/local/cd /usr/localcp -r apache-tomcat-7.0.93 ./tomcat70cp -r apache-tomcat-7.0.93 ./tomcat71cp -r apache-tomcat-7.0.93 ./tomcat72
vim /etc/profile  # 在文件末尾增加如下内容export CATALINA70_BASE=/usr/local/tomcat70export CATALINA70_HOME=/usr/local/tomcat70export TOMCAT70_HOME=/usr/local/tomcat70export CATALINA71_BASE=/usr/local/tomcat71export CATALINA71_HOME=/usr/local/tomcat71export TOMCAT71_HOME=/usr/local/tomcat71export CATALINA72_BASE=/usr/local/tomcat72export CATALINA72_HOME=/usr/local/tomcat72export TOMCAT72_HOME=/usr/local/tomcat72source /etc/profile  # 使配置文件生效
cd /usr/local/tomcat70/bin/vim catalina.sh  # 找到如下那行注释,在该注释下,增加两行配置# OS specific support.  $var _must_ be set to either true or false.export CATALINA_BASE=$CATALINA70_BASEexport CATALINA_HOME=$CATALINA70_HOMEcd /usr/local/tomcat71/bin/vim catalina.sh  # 找到如下那行注释,在该注释下,增加两行配置# OS specific support.  $var _must_ be set to either true or false.export CATALINA_BASE=$CATALINA71_BASEexport CATALINA_HOME=$CATALINA71_HOMEcd /usr/local/tomcat72/bin/vim catalina.sh  # 找到如下那行注释,在该注释下,增加两行配置# OS specific support.  $var _must_ be set to either true or false.export CATALINA_BASE=$CATALINA72_BASEexport CATALINA_HOME=$CATALINA72_HOME
vim /usr/local/tomcat70/conf/server.xml# 第一个端口,Server port节点端口<Server port="7005" shutdown="SHUTDOWN"># 第二个端口,Connector port节点端口,也即是Tomcat访问端口<Connector port="7080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" /># 第三个端口,Connector port节点端口<Connector port="7009" protocol="AJP/1.3" redirectPort="8443" />vim /usr/local/tomcat71/conf/server.xml# 第一个端口,Server port节点端口<Server port="7105" shutdown="SHUTDOWN"># 第二个端口,Connector port节点端口,也即是Tomcat访问端口<Connector port="7180" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" /># 第三个端口,Connector port节点端口<Connector port="7109" protocol="AJP/1.3" redirectPort="8443" />vim /usr/local/tomcat72/conf/server.xml# 第一个端口,Server port节点端口<Server port="7205" shutdown="SHUTDOWN"># 第二个端口,Connector port节点端口,也即是Tomcat访问端口<Connector port="7280" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" /># 第三个端口,Connector port节点端口<Connector port="7209" protocol="AJP/1.3" redirectPort="8443" />

2.Ngnix基础配置:

yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel#下载ngnixcd /usr/local/srcwget http://nginx.org/download/nginx-1.14.2.tar.gz#解压安装ngnixtar -zxvf nginx-1.14.2.tar.gzcd nginx-1.14.2./configure --prefix=/usr/local/nginxmake && make installcd /usr/local/nginxls[echo]conf  html  logs  sbin
#修改配置文件mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bakvim /usr/local/nginx/conf/nginx.conf
#nginx.conf详细内容user nobody nobody;worker_processes 2;error_log /usr/local/nginx/logs/nginx_error.log crit;pid /usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 51200;events{use epoll;worker_connections 6000;}http{include mime.types;default_type application/octet-stream;server_names_hash_bucket_size 3526;server_names_hash_max_size 4096;log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'' $host "$request_uri" $status'' "$http_referer" "$http_user_agent"';sendfile on;tcp_nopush on;keepalive_timeout 30;client_header_timeout 3m;client_body_timeout 3m;send_timeout 3m;connection_pool_size 256;client_header_buffer_size 1k;large_client_header_buffers 8 4k;request_pool_size 4k;output_buffers 4 32k;postpone_output 1460;client_max_body_size 10m;client_body_buffer_size 256k;client_body_temp_path /usr/local/nginx/client_body_temp;proxy_temp_path /usr/local/nginx/proxy_temp;fastcgi_temp_path /usr/local/nginx/fastcgi_temp;fastcgi_intercept_errors on;tcp_nodelay on;gzip on;gzip_min_length 1k;gzip_buffers 4 8k;gzip_comp_level 5;gzip_http_version 1.1;gzip_types text/plain application/x-javascript text/css text/htmapplication/xml;add_header Access-Control-Allow-Origin *;include vhost/*.conf;}
mkdir /usr/local/nginx/conf/vhost # 创建虚拟主机配置文件的存放目录cd /usr/local/nginx/conf/vhostvim www.xxx.com.conf  # 创建虚拟主机配置文件,内容如下
upstream 192.168.18.128 {#虚拟机相对于宿主机的IP地址# 需要负载的server列表,可以直接使用ipserver 192.168.18.128:7080 weight=1;server 192.168.18.128:7180 weight=1;server 192.168.18.128:7280 weight=1;# server www.xxx.com:8080 weight=1;# server www.xxx.com:9080 weight=3;}server{listen 80;autoindex on;server_name 192.168.18;access_log /usr/local/nginx/logs/access.log combined;index index.html index.htm index.jsp;location / {proxy_pass http://192.168.18.128;add_header Access-Control-Allow-Origin *;}}
#启动nginx[root@localhost local]# cd /usr/local/nginx/sbin[root@localhost sbin]# ./nginx -t  # 检查nginx配置文件nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf  # 启动nginx服务[root@localhost sbin]# netstat -lntp | grep nginx  # 检查端口是否已监听tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      19623/nginx: master[root@localhost sbin]# ps aux |grep nginx  # 检查nginx进程是否正常root      19623  0.0  0.0  20536   632 ?        Ss   21:38   0:00 nginx: master process ./nginx -c /usr/local/nginx/conf/nginx.confnobody    19624  0.7  0.0  22980  3212 ?        S    21:38   0:00 nginx: worker processnobody    19625  0.7  0.0  22980  3212 ?        S    21:38   0:00 nginx: worker processroot      19629  0.0  0.0 112708   980 pts/0    S+   21:38   0:00 grep --color=auto nginx#启动tomcat/usr/local/tomcat70/bin/startup.sh #启动tomcat70/usr/local/tomcat71/bin/startup.sh #启动tomcat71/usr/local/tomcat72/bin/startup.sh #启动tomcat72

3.修改tomcat初始界面方便检测效果

vim /usr/local/tomcat70/webapps/ROOT/index.jsp<div id="congrats" class="curved container"><h2>I'm Tomcat70</h2></div>
vim /usr/local/tomcat71/webapps/ROOT/index.jsp<div id="congrats" class="curved container"><h2>I'm Tomcat71</h2></div>
vim /usr/local/tomcat72/webapps/ROOT/index.jsp<div id="congrats" class="curved container"><h2>I'm Tomcat72</h2></div>

4.设置防火墙规则,开放80端口:

firewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --reload

5.宿主机端口转发访问虚拟机

开放各需要的端口:

firewall-cmd --zone=public --add-port=8080/tcp --permanentfirewall-cmd --zone=public --add-port=8089/tcp --permanentfirewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --zone=public --add-port=22/tcp --permanentfirewall-cmd --reload

5.验证实验是否成功:输入网址后不断刷新

至此负载集群搭建完毕

(0)

相关推荐