天天看點

Nginx動靜分離-tomcat

一、動靜分離

1、通過中間件将動态請求和靜态請求分離。

2、為什麼?

分離資源,減少不必要的請求消耗,減少請求延時。

Nginx動靜分離-tomcat

3、場景

Nginx動靜分離-tomcat

還可以利用php,fastcgi,python 等方式 處理動态請求

Nginx動靜分離-tomcat
Nginx動靜分離-tomcat
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}      

處理php動态請求

Nginx動靜分離-tomcat
Nginx動靜分離-tomcat
[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頁面

Nginx動靜分離-tomcat
Nginx動靜分離-tomcat
[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

​通路頁面      
Nginx動靜分離-tomcat
Nginx動靜分離-tomcat
<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

​處理動态頁面請求      
Nginx動靜分離-tomcat
Nginx動靜分離-tomcat
<%@ 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