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

Nginx + Tomcat动静分离实战Nginx

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

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

Nginx动静分离场景实践

在这里插入图片描述

  • 环境准备
系统服务服务IP
CentOS7.5负载均衡Nginx Proxy192.168.146.132
CentOS7.5静态资源Nginx Static192.168.146.133
CentOS7.5动态资源Tomcat Server192.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
  • 测试负载访问静态资源

在这里插入图片描述

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

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值