systemctl stop firewalld
setenforce 0
systemctl disable firewalld
cd /opt
rpm -qpl jdk-8u201-linux-x64.rpm
rpm -ivh jdk-8u201-linux-x64.rpm
java -version
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile
java -version
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mkdir /usr/local/tomcat
mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2 #创建两个文件
vim /etc/profile.d/tomcat.sh
#tomcat1
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
#tomcat2
export CATALINA_HOME2=/usr/local/tomcat/tomcat2
export CATALINA_BASE2=/usr/local/tomcat/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat/tomcat2
source /etc/profile.d/tomcat.sh
vim /usr/local/tomcat/tomcat2/conf/server.xml
#22行,修改Server port,默认为8005 -> 修改为8006
修改为8081
#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
vim /usr/local/tomcat/tomcat1/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat2/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
vim /usr/local/tomcat/tomcat2/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
/usr/local/tomcat/tomcat1/bin/startup.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
(1)Nginx+Tomcat负载均衡、动静分离
(2)Nginx作为负载均衡器,Tomcat作为应用服务器
(3)Nginx 服务器:192.168.142.5
(4)Tomcat多实例服务器1:192.168.142.4:8080 192.168.142.4:8081
(5)Tomcat单实例服务器2:192.168.142.6
systemctl stop firewalld
setenforce 0
#安装所需开发包和编译环境、编译器
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
#创建程序用户
useradd -M -s /sbin/nologin nginx
#解压安装包
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/
#指定安装路径、指定用户名、组名、启用模块以支持统计状态
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \ #启用文件修改支持
--with-http_stub_status_module \ #启用状态统计
--with-http_gzip_static_module \ #启用 gzip静态压缩
--with-http_flv_module \ #启用 flv模块,提供对 flv 视频的伪流支持
--with-http_ssl_module #启用 SSL模块,提供SSL加密功能
--with-stream #启用stream模块,提供四层调度
#编译及安装
make && make install
#软链接便于系统识别nginx操作命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#添加nginx系统服务
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#赋权及开启服务、开启开机自启
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
#安装JDK
cd /opt
tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
#设置JDK环境变量
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JAVA_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile
#安装启动Tomcat
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
就是第一段的内容
(1)多实例动态分离配置(192.168.142.4)
实例一:
mkdir /usr/local/tomcat/tomcat1/webapps/test #创建动态测试文件
vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp #动态页面显示内容
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
JSP test1 page #指定为test1页面
<% out.println("test1 test1 test1 ");%>
vim /usr/local/tomcat/tomcat1/conf/server.xml #删除前面的HOST配置
/usr/local/tomcat/tomcat1/bin/shutdown.sh
/usr/local/tomcat/tomcat1/bin/startup.sh
实例二:
mkdir /usr/local/tomcat/tomcat2/webapps/test #创建动态测试文件
vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp #动态页面显示内容
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
JSP test2 page #指定为test2页面
<% out.println("test2 test2 test2");%>
vim /usr/local/tomcat/tomcat2/conf/server.xml #删除前面的HOST配置
/usr/local/tomcat/tomcat2/bin/shutdown.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
(2)单实例Tomcat的server配置(192.168.142.6)
mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
JSP test3 page #指定为test3页面
<% out.println("test3 test3 test3");%>
vim /usr/local/tomcat/conf/server.xml #删除前面的HOST配置
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
(1)准备静态页面和静态图片
echo '111222333
' > /usr/local/nginx/html/index.html #准备静态网页
mkdir /usr/local/nginx/html/img
cp /root/1.png /usr/local/nginx/html/img #准备图片文件
(2)Nginx配置文件修改
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
#gzip on;
upstream tomcat_server { #配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
server 192.168.80.12:8080 weight=1;
server 192.168.80.12:8081 weight=1;
server 192.168.80.15:8080 weight=1;
}
server {
listen 80;
server_name www.123.com;
charset utf-8;
#access_log logs/host.access.log main;
location ~ .*\.jsp$ { #配置Nginx处理动态页面请求,将 .jsp文件请求转发到Tomcat 服务器处理
proxy_pass http://tomcat_server;
proxy_set_header HOST $host; #设置后端的Web服务器可以获取远程客户端的真实IP;设定后端的Web服务器接收到的请求访问的主机名(域名或IP、端口),默认host的值为proxy_pass指令设置的主机名
proxy_set_header X-Real-IP $remote_addr; #把$remote_addr赋值给X-Real-IP,来获取源IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/img; #配置Nginx处理静态图片请求
expires 1d;
}
location / {
root html;
index index.html index.htm;
}
......
}
......
}
systemctl restart nginx.service
(3)浏览器验证
vim /usr/local/nginx/conf/nginx.conf
stream { #与http同等级,一般在http上面设置
upstream appserver {
server 192.168.80.11:8080 weight=1;
server 192.168.80.11:8081 weight=1;
server 192.168.80.13:8080 weight=1;
}
server {
listen 8080;
proxy_pass appserver;
}
}
server {
listen 80;
server_name www.gxd.com;
charset utf-8;
#access_log logs/host.access.log main;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/img; #配置Nginx处理静态图片请求
expires 1d;
}
systemctl restart nginx
代理方式 | 区别 |
---|---|
七层反向代理 | 是基于http/https/mail等代理转发 |
四层反向代理 | 是基于IP+PROT(TCP/UDP)转发 |
负载均衡模式 | 说明 |
---|---|
rr负载均衡模式 | 每个请求按时间顺序逐一分配到不同的后端服务器,如果超过了最大失败次数后(max_fails,默认1),在失效时间内(fail_timeout,默认10秒),该节点失效权重变为0,超过失效时间后,则恢复正常,或者全部节点都为down后,那么将所有节点都恢复为有效继续探测,一般来说rr可以根据权重来进行均匀分配。 |
least_conn最少连接 | 优先将客户端请求调度到当前连接最少的服务器。 |
ip_hash负载均衡模式 | 每个请求按访问ip的hash结果分配,这样每个访客固定一个后端服务器,可以解决session的问题,但是ip_hash会造成负载不均,有的服务请求接收多,有的服务请求接收少,所以不建议采用ip_hash模式,session共享问题可用后端服务的session共享代替nginx的ip_hash。 |
fair(第三方)负载均衡模式 | 按后端服务器的响应时间来分配请求,响应时间短的优先分配。 |
url_hash(第三方)负载均衡模式 | 和ip_hash算法类似,是对每个请求按ur来的hash结果匹配,使每个url定向到同一个后端服务器,但是也会造成分配不均的问题,这种模式后端服务器为缓存时比较好。 |