1. 程式人生 > >搭建動態靜態伺服器

搭建動態靜態伺服器

Nginx做靜態資源伺服器優於Tomcat

區分靜態資源,動態資源請求

使用域名區分!

 

如果是動態資源請求  反向代理到 Tomcat 

如果 是靜態資源請求  直接走本地Nginx

配置:

###靜態資源
server {
listen 80;
server_name static.toov5.com;
location /static/img {
root /home;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}

 

 

 

女神出現了

 

動態:

properties:

server.port: 8080
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp  

 controller

package com.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @SpringBootApplication @Controller public class IndexController { @RequestMapping("/") public String index() { System.out.println("index"); return "index"; }
public static void main(String[] args) { SpringApplication.run(IndexController.class, args); } }

jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basepath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert title here</title>
</head>
<body>
  <h1>
   look:
     <img alt="" src="http://static.toov5.com/static/img/p.jpg"> 
  </h1>
</body>
</html>

pom:

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.itmayiedu</groupId>
	<artifactId>servet_demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <!-- SpringBoot 核心元件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>
</project>

  

我的訪問url與 網頁內嵌的不一樣哦

 訪問時候是www    圖片是static  完全分離了哦 哈哈哈

 

 請求一次: