Nginx reverse proxy, separation of dynamics and statics, build Nginx+Tomcat cluster load balancing deployment! !

1. Deploy Nginx+tomcat reverse proxy to separate dynamics and statics

Nginx is a very powerful static web server, Tomcat handle dynamic requests inefficient, and large general site
most of the content is static files (such as images, html, css, js, etc.), through the front Nginx reverse proxy to accelerate and Filtering;
the pressure on the back-end Tomcat to process requests can be greatly reduced, and only needs to be responsible for processing dynamic content. In the balance of performance and stability, the use of Nginx+Tomcat can make them function in their respective areas of expertise;

Second, prepare the environment

  • Nginx static web server realizes reverse proxy
  • tomcat dynamic web server, only processing dynamic web information, static information is handed over to nginx, to achieve dynamic separation
nginx server 192.168.100.25 centos7.6 Compile and install
tomcat server 1 192.168.100.26 centos7.6 Install JDK environment, start tomcat
tomcat server 2 192.168.100.27 centos7.6 Install JDK environment, start tomcat

2.1 Build tomcat server 1

 1.将JDK软件包放到/opt目录下
cd /opt
tar -zxf jdk-8u144-linux-x64.tar.gz                ##### 解压安装包
cp -rv jdk1.8.0_144/ /usr/local/java               #####复制文件到/usr/local/java  

2.定义变量环境

vi /etc/profile                                             #####编辑环境变量

export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib

source /etc/profile                                     #####让刚才编辑的环境变量生效

java -version                                              #####检查java版本


3、安装配置Tomcat

●解压 apache-tomcat-8.5.16.tar.gz 包

tar -zxf apache-tomcat-8.5.16.tar.gz

●解压后生成 apache-tomcat-8.5.16 文件夹,将该文件夹移动到/usr/local/下,并 改名为 tomcat8。
[root@localhost ~]# mv apache-tomcat-8.5.16/ /usr/local/tomcat8.5

做启动,关闭脚本软连接
ln -s /usr/local/tomcat8.5/bin/startup.sh /usr/bin/tomcatup                           
ln -s /usr/local/tomcat8.5/bin/shutdown.sh /usr/bin/tomcatdown   

tomcatup    启动
tomcatdown  关闭      



4.启动tomcat

tomcatup

Using CATALINA_BASE:   /usr/local/tomcat8.5
Using CATALINA_HOME:   /usr/local/tomcat8.5
Using CATALINA_TMPDIR: /usr/local/tomcat8.5/temp
Using JRE_HOME:        /usr/local/java/jre
Using CLASSPATH:       /usr/local/tomcat8.5/bin/bootstrap.jar:/usr/local/tomcat8.5/bin/tomcat-juli.jar
Tomcat started.      

netstat -anpt | grep 8080                                   #####检测是否启动,8080端口是否工作正常
tcp6       0      0 :::8080                 :::*                    LISTEN      68238/java  

5.建立 Java 的 Web 站点
在根目录下建立一个 web 目录,并在里面建立一个 web1 目录,用于存放网站文件。
 mkdir -p  /web/web1

目录下建立一个 index.jsp 的测试页面。

vi   /web/web1/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
        <head>
                <title>JSP test1 page</title>
        </head>
        <body>
                <% out.println("这是tomcat动态页面1!  访问的是index.jsp的!");%>
        </body>
        <body>
                <div>静态页面nginx的图片1</div><br><img src="nginx.jpg">
        </body>
</html>


6.在tomcat主配置文件下添加
 vi /usr/local/tomcat8/conf/server.xml     #在host name配置区域下,添加咱们刚才配置的jsp

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

      <Context docBase="/web/web1" path="" reloadable="false" >
      </Context>

###docBase:web 应用的文档基准目录 
###reloadable :设置监视"类"是否变化 
###path="" 设置默认"类"

7.关闭tomcat,再开启toncat

tomcatup    启动
tomcatdown  关闭   

2.2 Build tomcat server 2

Same deployment as tomcat server 1, except that the contents of directory 1, and the test page are changed to 2 for easy identification. I will not write it here, just refer to tomcat server 1.

2.3 Install Nginx server

Install Nginx on the Nginx server 192.168.100.21, reverse proxy to two Tomcat sites, and achieve load balancing
(1) Turn off the firewall.
(2) Install related software packages.


1.安装环境依赖关系
yum -y install  gcc-c++  gcc  make pcre-devel zlib-devel openssl-devel

2.创建程序账户
useradd -M -s /sbin/nologin  nginx

3.解压软件包,编译安装
软件包在/opt目录下

cd  /opt
tar xzvf nginx-1.15.9.tar.gz
cd nginx-1.15.9/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module



/--user=,--group= 
//--with-file-aio 
//--with-http_stub_status_module 
//--with-http_gzip_static_module 
//--with-http_flv_module 
//--with-http_ssl_module
#指定运行的用户和组
#启用文件修改支持
#启动状态统计
#启用gzip静态压缩
#启用flv模块,提供寻求内存使用基于时间的偏移量文件
#启用SSL模块


4.安装kiall命令  我是最小化安装的
yum -y install psmisc

5.开启nginx,关闭,测试
nginx                                ####启动
netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN7180/nginx: master

killall -3 nginx                                                     ###停止服务
6.添加 Nginx 系统服务,使用centos的systemctl命令

vi /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
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target


7.设置权限,启动nginx
chmod 754 /lib/systemd/system/nginx.service
systemctl  start nginx
systemctl enable nginx.




下面在 Nginx 上准备静态图片。

mkdir /usr/local/nginx/html/img 		###创建静态文件目录 
存放图片的目录,nginx.jpg



配置nginx.conf文件
把 Nginx 的默认站点通过 proxy_pass 方法代理到了设定好的 tomcat_server 负载均衡
服务器组上。配置完成的 nginx.conf 文件内容如下。
 …… //省略部分内容 

http {
    
     
…… 		###省略部分内容
#gzip on;
upstream tomcat_server {
    
    
	server 192.168.100.26:8080 weight=1;
	server 192.168.100.27:8080 weight=1;
	}

server {
    
    
	listen 80;
	server_name localhost;
	#charset koi8-r;
	#access_log logs/host.access.log main;
	location ~ .*.jsp$ {
    
    
		proxy_set_header HOST $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header Client-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://tomcat_server;   #反向代理,跳转到上面的upstream配置
		}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
    
    
	root /usr/local/nginx/html/img;
	expires 30d;
}

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



验证配置文件语法

nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


重启服务
systemctl  restart  nginx


2.4 verification

Enter 192.168.100.25/index.jsp;

The tomcat webpage, the static pictures on the nignx server,
through the nignx reverse proxy, realize tomcat load balancing.
Through nginx, the location segment will be configured, and the jsp and other program files after the separation of movement and static will be distributed to the tomcat cluster.

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47320286/article/details/108805631