主机 | 操作系统 | IP地址 | 所需软件 |
---|---|---|---|
Nginx Server | CentOS7 | 192.168.109.7 | nginx-1.12.0.tar.gz |
Tomcat Server1 | CentOS7 | 192.168.109.22 | apache-tomcat-9.0.16.tar.gz、jdk-8u201-linux-x64.rpm |
Tomcat Server2 | CentOS7 | 192.168.109.23 | apache-tomcat-9.0.16.tar.gz、jdk-8u201-linux-x64.rpm |
#!/bin/bash #脚本说明:编译安装nginx服务 #注意:使用前请将nginx-1.12.0.tar.gz放入/opt目录下 #关闭防火墙 systemctl stop firewalld systemctl disable 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/ #编译安装nginx;指定安装路径、指定用户名、组名、启用模块以支持统计状态 cd /opt/nginx-1.12.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-file-aio --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module make && make install #软链接便于系统识别nginx操作命令 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #添加nginx系统服务 echo '[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' > /lib/systemd/system/nginx.service #赋权及开启服务、开启开机自启 chmod 754 /lib/systemd/system/nginx.service systemctl restart nginx.service systemctl enable nginx.service
2、部署两台 Tomcat 应用服务器
#!/bin/bash #安装Tomcat服务 #关闭防火墙 systemctl stop firewalld systemctl disable firewalld setenforce 0 #安装JDK cd /opt rpm -ivh jdk-8u201-linux-x64.rpm #设置JDK环境变量 echo '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' > /etc/profile.d/java.sh 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/startup.sh
3、动静分离配置
请看tomcat
1)Tomcat1 server 配置
mkdir /usr/local/tomcat/webapps/test #配置动态页面 vim /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("动态页面 1:www.test1.com");%> </body> </html>
#修改配置文件 vim /usr/local/tomcat/conf/server.xml <Host name="192" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" /> </Host> /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/startup.sh
2)Tomcat2 server 配置
mkdir /usr/local/tomcat/webapps/test vim /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test2 page</title> </head> <body> <% out.println("动态页面 1:www.test2.com");%> </body> </html>
#修改配置文件 vim /usr/local/tomcat/conf/server.xml <Host name="192" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" /> </Host> /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/startup.sh
3)Nginx server 配置
#准备静态页面和静态图片 echo '<html><body><h1>this is static</h1></body></html>' > /usr/local/nginx/html/index.html mkdir /usr/local/nginx/html/long cd /usr/local/nginx/html/long
vim /usr/local/nginx/conf/nginx.conf ...... http { ...... #gzip on; upstream tomcat_server { server 192.168.109.22:8080 weight=1; server 192.168.109.23:8080 weight=1; } server { listen 80; server_name www.long.com; #charset koi8-r; #access_log logs/host.access.log main; #配置Nginx处理动态页面请求,将 .jsp 文件请求转发到Tomcat 服务器处理 location ~ .*\.jsp$ { proxy_pass http://tomcat_server; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #配置Nginx处理静态图片请求 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { root /usr/local/nginx/html/img/; expires 10d; } location / { root html; index index.html index.htm; } systemctl restart nginx.service
4、访问测试
测试静态页面效果
浏览器访问 http://192.168.109.7/
浏览器访问 http://192.168.109.7/long.jpg
测试负载均衡效果,不断刷新浏览器测试
浏览器访问 http://192.168.109.7/test/index.jsp
到此这篇关于Nginx+Tomcat实现负载均衡、动静分离的原理解析的文章就介绍到这了,更多相关Nginx负载均衡、动静分离内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!