温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nginx动静分离及配置的方法是什么

发布时间:2023-04-26 16:49:05 来源:亿速云 阅读:98 作者:iii 栏目:开发技术

这篇“Nginx动静分离及配置的方法是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Nginx动静分离及配置的方法是什么”文章吧。

1.Nginx动静分离概念

动静分离,通过中间件将动态请求和静态请求进行分离,分离资源,减少不必要的请求消耗,减少请求延时。

好处:动静分离后,即使动态服务不可用,但静态资源不会受到影响

通过中间件可以将动态请求和静态请求进行分离

Nginx动静分离及配置的方法是什么

2.Nginx动静分离应用案例

Nginx动静分离及配置的方法是什么

2.1.环境规划

系统服务服务地址
centos7.5负载均衡Nginx proxy192.168.81.210
centos7.5静态资源Nginx static192.168.81.220
centos7.5动态资源Tomcat server192.168.81.230

2.2.配置静态资源

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

Nginx动静分离及配置的方法是什么

2.3.配置动态资源

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

2.4.整合动静分离

2.4.1.配置动静分离负载均衡
[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;
        }
}
2.4.2.编写整合动静分离代码
[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">
                <div id="get_data"></div>
        </body>
</html>

2.5.效果

看着是一个页面实则不同机器在做处理

Nginx动静分离及配置的方法是什么

以上就是关于“Nginx动静分离及配置的方法是什么”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI