首页 > 代码库 > httpd或Nginx负载均衡tomcat
httpd或Nginx负载均衡tomcat
实验环境:CentOS7
#两台tomcat的基本配置如下: [root@webapps localhost]#setenforce 0 [root@webapps localhost]#iptables -F [root@webapps localhost]#yum -y install java-1.8.0-openjdk-devel tomcat tomcat-webapps tomcat-admin-webapps [root@webapps localhost]#chown tomcat.tomcat /usr/share/tomcat/webapps/test/* [root@webapps localhost]#chown tomcat.tomcat test [root@webapps localhost]#vim /etc/tomcat/server.xml <Connector port="8081" protocol="HTTP/1.1" maxThreads="200" enableLookups="False" connectionTimeout="60000" address="172.16.252.142" redirectPort="8443" /> [root@webapps localhost]#vim /usr/share/tomcat/webapps/test/index.jsp <%@ page language="java" %> <%@ page import="java.util.*" %> <html> <head><title>tomcat1 mini</title></head> <body> <% out.println("tomcat1 mini"); %> </body> </htmL> [root@webapps localhost]#vim /usr/share/tomcat/conf/tomcat-users.xml <role rolename="manager-gui"/> <user username="tomcat" password="123456" roles="manager-gui"/>
#第三台主机安装Nginx:172.16.253.177
#ngixn做负载均衡:
[root@~ localhost]#setenforce 0 [root@~ localhost]#iptables -F [root@~ localhost]#yum -y install nginx-1.10.2-1.el7.ngx.x86_64.rpm [root@~ localhost]#vim /etc/nginx/nginx.conf http { #定义:集群tomcatsr upstream tomcatsr { server 172.16.252.142:8081; server 172.16.254.47:8081; } include /etc/nginx/conf.d/*.conf; } #引用 [root@~ localhost]#vim /etc/nginx/conf.d/default.conf server { location / { proxy_pass http://tomcatsr; root /usr/share/nginx/html; index index.html index.htm; }
#httpd做负载均衡:
[root@~ localhost]#yum -y install httpd
[root@~ localhost]#httpd -M
proxy_balancer_module (shared)#负载均衡模块
[root@~ localhost]#vi /etc/httpd/conf.d/tomcat-httpd.conf <proxy balancer://tomcatsr> BalancerMember http://172.16.252.142:8081 BalancerMember http://172.16.254.47:8081 ProxySet lbmethod=byrequests </Proxy> <VirtualHost *:80> ServerName linux.io ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass / balancer://tomcatsr/ ProxyPassReverse / balancer://tomcatsr/ <Location /> Require all granted </Location> </VirtualHost>
#测试:
[root@~ localhost]#for i in {1..10};do curl http://172.16.253.177/test/;done <html> <head><title>tomcat2 mini1</title></head> <body> tomcat2 mini1 </body> </htmL> <html> <head><title>tomcat1 mini</title></head> <body> tomcat1 mini </body> </htmL> <html> <head><title>tomcat2 mini1</title></head> <body> tomcat2 mini1 </body> </htmL> <html> <head><title>tomcat1 mini</title></head> <body> tomcat1 mini </body> </htmL> <html> <head><title>tomcat2 mini1</title></head> <body> tomcat2 mini1 </body> </htmL> <html> <head><title>tomcat1 mini</title></head> <body> tomcat1 mini </body> </htmL> <html> <head><title>tomcat2 mini1</title></head> <body> tomcat2 mini1 </body> </htmL> <html> <head><title>tomcat1 mini</title></head> <body> tomcat1 mini </body> </htmL> <html> <head><title>tomcat2 mini1</title></head> <body> tomcat2 mini1 </body> </htmL> <html> <head><title>tomcat1 mini</title></head> <body> tomcat1 mini </body> </htmL>
httpd或Nginx负载均衡tomcat
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。