Nginx動靜分離-tomcat

1、動靜分離

一、經過中間件將動態請求和靜態請求分離。javascript

二、爲何?php

分離資源,減小沒必要要的請求消耗,減小請求延時。html

34961171

三、場景java

34979453

還能夠利用php,fastcgi,python 等方式 處理動態請求python

#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動態請求

 

 

[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 的配置jquery

[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

[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

測試web

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

aa234b89-eb4b-4185-a143-0e0a20d661d9

相關文章
相關標籤/搜索