What is the method of separating and configuring Nginx dynamically and statically?

PHPz
Release: 2023-05-11 22:25:04
forward
1204 people have browsed it

1.Nginx dynamic and static separation concept

Dynamic and static separation uses middleware to separate dynamic requests and static requests, separate resources, reduce unnecessary request consumption, and reduce request delays.

Benefits: After dynamic and static separation, even if dynamic services are unavailable, static resources will not be affected

Dynamic requests and static requests can be separated through middleware

What is the method of separating and configuring Nginx dynamically and statically?

2. Nginx static and dynamic separation application cases

What is the method of separating and configuring Nginx dynamically and statically?

##2.1. Environment planning

SystemServiceServiceAddress##centos7.5centos7.5##centos7.5Dynamic resourcesTomcat server 192.168.81.2302.2. Configure static resources
1.创建动静分离配置文件
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim ds.conf
#动静分离
server {
	listen 80;
	server_name ds.com;
	
	location / {
		root /web;
		index index.html;
	}
	
	location ~* .*\.(png|jpg|gif)$ {
		root /web/images;
	}
}

2.重载Nginx
[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# systemctl reload nginx

3.准备图片
[root@localhost conf.d]# mkdir /web/images
[root@localhost conf.d]# wget -O /web/images/nginx.png http://nginx.org/nginx.png
Copy after login
Load balancing Nginx proxy 192.168.81.210
Static resources Nginx static 192.168.81.220

2.3. Configure dynamic resourcesWhat is the method of separating and configuring Nginx dynamically and statically?

1.编译安装tomcat
[root@localhost soft]# tar xf apache-tomcat-7.0.92.tar.gz  -C /application/

2.写入动态文件
[root@localhost soft]# cd /application/
[root@localhost application]# vim apache-tomcat-7.0.92/webapps/ROOT/java_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
    <HEAD>
        <TITLE>JSP Test Page</TITLE>
    </HEAD>
    <BODY>
      <%
        Random rand = new Random();
        out.println("<h2>Random number:</h2>");
        out.println(rand.nextInt(99)+100);
      %>
    </BODY>
</HTML>

3.启动服务
[root@localhost application]# cd apache-tomcat-7.0.92/
[root@localhost apache-tomcat-7.0.92]# ./bin/startup.sh
Copy after login

2.4. Integrate dynamic and static separation

2.4.1. Configure dynamic and static separation load balancing

[root@localhost conf.d]# vim lb_ds.conf
#整合动静分离
upstream static_photo {
        server 172.16.1.20:80;
}

upstream java {
        server 172.16.1.30:8080;
}

server {
        listen 80;
        server_name ds.com;
        access_log /nginx_log/lb_ds_access.log main;

        location / {
                root /web/ds;
                index index.html;
        }

        location ~* .*\.(jpg|png|gif)$ {
                proxy_pass http://static_photo;
                proxy_set_header HOST $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ~* .jsp$ {
                proxy_pass http://java;
                proxy_set_header HOST $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}
Copy after login

2.4.2. Write integrated dynamic and static separation code
[root@localhost conf.d]# vim /web/ds/index.html
<html lang="en">
<head>
        <meta charset="UTF-8" />
        <title>测试动静分离</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://ds.com/java_test.jsp",
        success: function(data) {
                $("#get_data").html(data)
        },
        error: function() {
                alert("fail!!,请刷新再试");
        }
        });
});
</script>
        <body>
                <h2>测试动静分离</h2>
                <h2>上面为静态图片,下面为动态页面</h2>
                <img  src="http://ds.com/nginx.png" alt="What is the method of separating and configuring Nginx dynamically and statically?" >
                <div id="get_data"></div>
        </body>
</html>
Copy after login
2.5. Effect
It looks like one page, but in fact different machines are processing it

The above is the detailed content of What is the method of separating and configuring Nginx dynamically and statically?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template