微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Nginx动静分离-tomcat

一、动静分离

1、通过中间件将动态请求和静态请求分离。

2、为什么?

分离资源,减少不必要的请求消耗,减少请求延时。

34961171

3、场景

34979453

还可以利用PHP,fastcgi,python 等方式 处理动态请求


[root@web-01 ~]# cat ngixn.conf
user Nginx;
worker_processes auto;
error_log /var/log/Nginx/error.log;
pid /run/Nginx.pid;
include /usr/share/Nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
​
    include             /etc/Nginx/mime.types;
    default_type        application/octet-stream;

​
    include /etc/Nginx/conf.d/cp4/*.conf;
}
#主配置文件

server conf 的配置

[root@web-01 ~]# cat test_mysite.conf
​
upstream java_api{
    server 127.0.0.1:8080;
}
server {
    listen       80;
    server_name  web01.fadewalk.com;
​
    access_log  /var/log/Nginx/host.access.log  main;
    root /opt/app/code/cp4/code;
​
    location ~ \.jsp$ {
        proxy_pass http://java_api;
        index  index.html index.htm;
    }
​
    location ~ \.(jpg|png|gif)$ {
        expires 1h;
        gzip on;
    }
}

Tomcat 部署jsp页面

[root@web-01 ROOT]# tomcat version
Server version: Apache Tomcat/7.0.76
Server built:   Mar 12 2019 10:11:36 UTC
Server number:  7.0.76.0
OS Name:        Linux
OS Version:     3.10.0-957.21.2.el7.x86_64
Architecture:   amd64
JVM Version:    1.8.0_212-b04
JVM vendor:     Oracle Corporation
​
[root@web-01 ~]# cd /usr/share/tomcat/webapps
[root@web-01 webapps]# mkdir ROOT
[root@web-01 webapps]# cd ROOT/
[root@web-01 ROOT]# pwd
/usr/share/tomcat/webapps/ROOT              #/usr/share/tomcat/webapps 所有页面目录,没有ROOT目录时,需要自己新建,ROOT目录为认的网站页面目录 ,项目目录必须大写,对应配置
[root@web-01 ROOT]# ll
total 4
-rw-r--r--. 1 root root 343 Jun 17 02:14 java_test.jsp
view
​访问页面

<html lang="en">
<head>
<Meta charset="UTF-8" />
<title>测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "http://jeson.t.imooc.io/java_test.jsp",
        success: function(data) {
            $("#get_data").html(data)
        },
        error: function() {
            alert("fail!!!,请刷新再试!");
        }
    });
});
</script>
<body>
    <h1>测试动静分离</h1>
    <img src="http://jeson.t.imooc.io/img/Nginx.png"/>
    <div id="get_data"><div>
</body>
</html>
test_mysite.html
​处理动态页面请求

<%@ 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(rand.nextInt(99)+100);
        %>
    </BODY>
</HTML>
java_test.jsp

测试

6853abf2-0724-4b12-aa30-25e296f345a9

aa234b89-eb4b-4185-a143-0e0a20d661d9


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐