Nginx configuration of static and dynamic separation

                                  Nginx of static and dynamic separation

    nginx reverse proxy, I think we should understand, tomcat nginx proxy rear end of the front, but in dealing with static resources (picture etc.) tomcat is not dominant. So static and dynamic separation use nginx's location matches the static resources to deal with their own, or handed over to other server processing, dynamic resource to tomcat process. Such benefits are to speed up the access speed of the site, reducing the back-end pressure, and in the background when tomcat is down, static resources are not affected.


Environment: three hosts

192.168.0.18 proxy nginx proxy host

192.168.0.25 resolve dynamic resource tomcat

192.168.0.102 parse static resources nginx


First, dynamic resource configuration tomcat

tomcat installation does not go into details, the main change to access the page

Path: / usr / local / tomcat8 / webapps / ROOT

vim index.jsp # code that follows, can be copied directly.

<%@ 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>随机数:</h1>");
        out.println(rand.nextInt(1000)+100);
      %>
      %    </BODY>
      %    </HTML

Visit the following page content

Random number .png



Second, the static resource solution analysis nginx configuration

Install nginx not go into details, the main change nginx.conf

1, add the following location in the default server, the matching static resources

        location ~ .*\.(gpg|png|css|jpg) {

            root   /usr/local/nginx/static;

        }

[$Y873AREV2_PHVZMSIILU7.png


2, create the corresponding directory

mkdir /usr/local/nginx/static

In this directory you can put a test image, images suffix to match the configuration file,


3, restart nginx, page visits

V_CC(6G@RTNA_5QWO@17(R5.png


三、proxy代理nginx配置

搭建nginx不在赘述,主要修改主配置文件

这样访问192.168.0.18/test.jpg 和192.168.0.18/index.jsp时会代理不同的后端主机进行解析。

vim /usr/local/nginx/conf/nginx.conf

    gzip  on;
    #增加upstream模块
    upstream tomcat_server {
      server 192.168.0.25:8080;
      }
    upstream static_server {
      server 192.168.0.102;
     }
    #修改默认server的中配置
    server {
        listen       80;
        server_name  localhost;
        location / {
        root html;
        index index.html;
}
            #匹配到http://ip/*.jpg或者*.png或者*.css时,交由静态资源服务器102处理
        location ~ .*\.(jpg|png|css) {
          proxy_pass http://static_server;
          proxy_set_header X-Real-IP $remote_addr;
}
           #匹配到http://ip/*.jsp时,交由后台tomcat处理动态资源
       location ~ .*\.jsp$ {
         proxy_pass http://tomcat_server;
         proxy_set_header X-Real-IP $remote_addr;
}
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

BM`V1KMR~1NPIJ$G79VU8FI.png

I3%850[~S(_)SS_5QE$%(T3.png


四、整合动态和静态资源(proxy代理nginx)

vim /usr/local/nginx/html/index.html

<html>
<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",
        #添加proxy主机代理的动态资源
        url: "http://192.168.0.18/index.jsp",
        success: function(data) {
                $("#get_data").html(data)
        },
        error: function() {
                alert("fail!!,请刷新再试!");
        }
        });
});
</script>
        <body>
                <h1> static and dynamic separation test </ h1> 
                # Add a static resource proxy host agent 
                <img src = "http://192.168.0.18/test.jpg"> 
                <div the above mentioned id = "get_Data"> </ div> 
        </ body> 
</ HTML>

Access effect is as follows:

AWVL$83YFNW4QMXA)900C5K.png


Summary: static and dynamic separation principle is actually using the location of the nginx regular matches, so even if the dynamic resource downtime, does not affect static resource use, and increase access efficiency.

Guess you like

Origin blog.51cto.com/13760226/2422167