Nginx动静分离基本概述

本文介绍了Nginx动静分离的概念和优势,包括单台和多台服务器的实践,以及如何通过配置实现静态资源和动态请求的分离。此外,还详细讲述了在不同设备(如iPhone和Android)上如何利用Nginx进行资源分离的场景,并提供了配置和测试步骤,确保动静资源互不影响。
摘要由CSDN通过智能技术生成

Nginx动静分离基本概述

动静分离,通过中间将动静分离和静态请求进行分离;
通过中间件将动态请求和静态请求分离,可以建上不必要的请求消耗,同时能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下 :
在这里插入图片描述
动静分离只有好处:动静分离后,即使动态服务不可用,但静态资源不会受到影响。

Nginx动静分离场景实践

单台服务器实现动静分离

location / {
    root /code/wordpress;
    index.php;
}
location ~* \.(png|jpg|mp4)${
     #指定图片路径
    root /code/wordpress/images;
    #压缩
    gzip on;
    .....
}
location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    .....
}

多台服务器实现动静分离

在这里插入图片描述


环境准备

系统 作用 服务 地址
Centos7.5 负载均衡 nginx proxy 10.0.0.5
Centos7.5 静态资源 nginx static 10.0.0.7
Centos7.5 动态资源 tomcat server 10.0.0.8

web01配置静态资源

[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# cat ds_oldboy.conf 
server {
        listen 80;
        server_name pic.drz.com;
        root /code;
        index index.html;

        location ~* .*\.(jpg|png|gif)$ {
                root /code/images;
        }
}

#配置一个主页
[root@web01 conf.d]# echo "zls_test_web01" > /code/index.html

#创建图片目录
[root@web01 conf.d]# mkdir /code/images/

#上传一个静态文件
[root@web01 conf.d]# cd /code/images/
[root@web01 images]# rz bg1.jpg

[root@web01 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@web01 conf.d]# nginx -s reload

验证

打开浏览器访问

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值