运维之道 | Nginx+Tomcat 动静分离实战

Nginx + Tomcat动静分离实战Nginx

动静分离简单来说是把动态跟静态请求分开,但不能理解成只是单纯地把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用 Nginx 处理静态页面Tomcat、Resin、PHP、ASP 处理动态页面

动静分离从实现角度来讲大致分为两种:一种是纯粹地把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;另外一种方法就是动态跟静态文件混合在一起发布,通过 Nginx 来分开。

Nginx动静分离场景实践

在这里插入图片描述

  • 环境准备
系统 服务 服务 IP
CentOS7.5 负载均衡 Nginx Proxy 192.168.146.132
CentOS7.5 静态资源 Nginx Static 192.168.146.133
CentOS7.5 动态资源 Tomcat Server 192.168.146.134

1、在192.168.146.133服务器上配置静态资源

  • 将静态图片导入发布目录中
[root@localhost html]# rz 
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring nginx.jpg...
nginx.jpg was skipped

[root@localhost html]# ls
nginx.jpg
  • 修改静态配置文件
worker_processes  1;
events {
    worker_connections  1024;
}
#############################################
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
#############################################
    server {
        listen       80;
        server_name  localhost;
#############################################
    location ~*.*\.(png|jpg|gif)$ {
            root   html;
            index  index.html index.htm;
        }
#############################################
    error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
        }
    }
}
  • 检查配置文件是否正确,并重载数据
[root@#localhost ~]# /usr/local/nginx/sbin/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
[root@#localhost ~]# /usr/local/nginx/sbin/nginx -s reload
  • 测试访问静态资源

在这里插入图片描述


2、在192.168.146.134服务器上配置动态资源

  • 将java.jsp文件导入发布目录中
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
    <HEAD>
        <TITLE>JSP Test Page</TITLE>
    </HEAD>
    <BODY>
      <%
        Random rand = new Random();
        out.println("<h1>Random number:</h1>");
	    out.println("<h1>刷新数字即可变化</h1>");
        out.println(rand.nextInt(99)+100);
      %>
    </BODY>
</HTML>
  • 重启Tomcat服务
[root@localhost ROOT]# /usr/local/tomcat/bin/startup.sh 
  • 测试访问动态资源

在这里插入图片描述


3、在负载均衡192.168.146.132上配置调度, 实现访问jsp和png

  • 未进行动静分离配置访问状态

在这里插入图片描述
在这里插入图片描述


  • 配置域名
[root@localhost ~]# vi /etc/hosts
192.168.146.132  www.zwl.com
  • 动静分离配置
[root@localhost nginx]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}
################################################
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
################# `upstream` ###################
upstream static {
        server 192.168.146.133:80;
}

upstream java {
        server 192.168.146.134:8080;
}
################################################
    server {
        listen       80;
        server_name  www.zwl.com;
       
        location / {
            root   html;
            index  index.html index.htm;
################# `proxy_pass` ##################
        location ~* .*\.(png|jpg|gif)$ {
            proxy_pass http://static;
        }

        location ~ .*\.jsp$ {
            proxy_pass http://java;
        }
        }
#################################################
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
  • 检查配置文件是否正确,并刷新数据
[root@#localhost ~]# /usr/local/nginx/sbin/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
[root@#localhost ~]# /usr/local/nginx/sbin/nginx -s reload
  • 测试负载访问静态资源

在这里插入图片描述

  • 通过负载测试访问动态资源

在这里插入图片描述

发布了97 篇原创文章 · 获赞 10 · 访问量 3411

猜你喜欢

转载自blog.csdn.net/VillianTsang/article/details/103473341
今日推荐