Linux基础39 nginx动静分离, nginx资源分离, rewrite概述

发布时间 2023-09-14 23:22:46作者: 战斗小人

一、动静分离

动静分离,通过中间件将动静分离和静态请求进行分离;
通过中间件将动态请求和静态请求,可以减少不必要的请求消耗,同时能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下:

 

二、配置动静分离

1.单台机器的动静分离

[root@web01 conf.d]# vim wordpress.conf
server {
    listen 80;
    server_name blog.linux.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    # 如果请求的是以 .jpg结尾的静态文件  就去/code/images 目录下访问
    location ~* \.jpg$ {
        root /code/images;
    }

    location ~* \.php$ {
        root /code/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

[root@web01 conf.d]# systemctl restart nginx
# 创建目录
[root@web01 code]# [root@web01 code]# mkdir /code/images
# 实现动静分离
方式一: 把文件挪到/code/images/
[root@web01 code]# cp -r /code/wordpress/wp-content /code/images/
[root@web01 code]# chown -R www.www /code/images
方式二:做软链接
    cd /code
    ln -s wordpress images

2.多台机器动静分离

1)环境装备

 

主机作用服务地址
lb01 负载均衡 nginx proxy 10.0.0.5
web01 静态资源 nginx static 10.0.0.7
web02 动态资源 tomcat server 10.0.0.8

 

2)配置web01的静态内容

#配置nginx
[root@web01 conf.d]# vim jt.conf
server {
    listen 80;
    server_name dj.linux.com;

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

#重启nginx
[root@web01 conf.d]# systemctl restart nginx

#上传图片
[root@web01 conf.d]# mkdir /code/picture
[root@web01 conf.d]# cd /code/picture
[root@web01 conf.d]# rz 1.jpg

# 配置hosts,访问图片
http://dj.linux.com/1.jpg

3)在web02上配置动态资源

# 安装tomcat
[root@web02 ~]# yum install -y tomcat

# 配置动态内容
[root@web02 ~]# cd /usr/share/tomcat/webapps/
[root@web02 webapps]# mkdir ROOT
[root@web02 webapps]# vim ROOT/java_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
    <HEAD>
        <TITLE>JSP Page</TITLE>
    </HEAD>
    <BODY>
        <%
            Random rand = new Random();
            out.println("<h1>随机数:<h1>");
            out.println(rand.nextInt(99)+100);
        %>
    </BODY>
</HTML>

# 启动tomcat
[root@web02 webapps]# systemctl start tomcat

# 访问页面
http://10.0.0.8:8080/java_test.jsp

4)在负载均衡lb01上配置页面

# 配置负载均衡的nginx
[root@lb01 conf.d]# vim dj.linux.com.conf
server {
    listen 80;
    server_name dj.linux.com;

    location / {
        root /code/dj;
        index index.html;
    }
        
    location ~* \.(jpg|gif|png)$ {
        proxy_pass http://172.16.1.7;
        proxy_set_header HOST $http_host;    # 不带域名会访问对方第一个conf文件地址
    }
    
    location ~ \.jsp$ {
        proxy_pass http://172.16.1.8:8080;
        proxy_set_header HOST $http_host;
    }
}

# 重启nginx
[root@lb01 conf.d]# systemctl restart nginx

# 配置host访问页面
http://dj.linux.com/java_test.jsp

5)整合静态内容和动态内容

[root@lb01 conf.d]# mkdir /code/dj -p
[root@lb01 conf.d]# vim /code/dj/index.html
<html lang="en">
<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",
        url: "http://dj.linux.com/java_test.jsp",
        success: function(data){
                $("#get_data").html(data)
        },
        error: function() {
                alert("哎哟喂,失败了,回去检查你服务区~");
        }
        });
});
</script>
        <body>
                <h1>测试动静分离</h1>
                <img src="http://dj.linux.com/2.gif">
                <div id="get_data"></div>
        </body>
</html>

# 授权站点目录
[root@lb01 ~]# chown -R nginx.nginx /code/

# 访问域名测试页面,静态内容和动态内容关闭其一,互不影响
http://dj.linux.com/

 

三、Nginx资源分离

1.资源分离

Nginx通过负载均衡实现手机与PC调度至不同的后端节点应用案例
使用pc访问时跳转到pc配置的页面,使用手机访问时可以跳转不同的页面(同一域名下,不同ip不同服务器或同服务器不同端口)

2.配置资源分离场景

1)环境准备

主机主机作用外网ip内网ip端口
lb01 负载均衡 10.0.0.4 172.16.1.4 80
web01 提供Android手机页面 10.0.0.7 172.16.1.7 8081
web01 提供Iphone手机页面 10.0.0.7 172.16.1.7 8082
web01 提供电脑访问 10.0.0.7 172.16.1.7 8083

2)配置服务器

[root@web01 conf.d]# vim sj.linux.com.conf
server {
    listen 8081;
    server_name sj.linux.com;

    location / {
        root /code/android;
        index index.html;
    }
}

server {
    listen 8082;
    server_name sj.linux.com;

    location / {
        root /code/iphone;
        index index.html;
    }
}

server {
    listen 8083;
    server_name sj.linux.com;

    location / {
        root /code/pc;
        index index.html;
    }
}

# 重启nginx
# 检查端口
[root@web01 conf.d]# netstat -lntp

3)配置站点

[root@web01 conf.d]# mkdir /code/{android,pc,iphone}
[root@web01 conf.d]# echo "我是android" > /code/android/index.html
[root@web01 conf.d]# echo "我是iphone" > /code/iphone/index.html
[root@web01 conf.d]# echo "我是computer" > /code/pc/index.html

4)配合负载均衡

[root@lb01 ~]# vim sj.linux.com.conf
server {
    listen 80;
    server_name sj.linux.com;

    location / {
        if ($http_user_agent ~* "Android") {    # 进行匹配,不区分大小写
            proxy_pass http://172.16.1.7:8081;
        }

        if ($http_user_agent ~* "iPhone") {
            proxy_pass http://172.16.1.7:8082;
        }

        # if ($http_user_agent ~* "Chrome") {    # 如果是谷歌访问的,返回403
         #   return 403;
         # }
        proxy_pass http://172.16.1.7:8083;    # 都不是跳转到pc
    }
}

# 重启nginx
[root@lb01 conf.d]# systemctl restart nginx

5) 配置hosts访问页面测试

http://sj.linux.com/

 

 

四、Rewrite

1.rewrite概述

Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。

2.rewrite使用场景

1、地址跳转,用户访问www.drz.com这个URL时,将其定向至一个新的域名mobile.drz.com
2、协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态URL地址对外暴露过多的参数,提升更高的安全性。
4、搜索引擎,SEO优化依赖于url路径,好记的url便于支持搜索引擎录入

3.rewrite配置语法

Syntax:    rewrite regex replacement [flag];#regex访问的地址  replacement跳转的地址  flag标记可加可不加
Default:    —
Context:    server, location, if

# 一般用于切换维护场景
例: rewrite ^(.*)$ /page/404.html break;    # 任意地址跳转到404页面,break为标记

4.rewrite标记 flag

flag作用
last 本条规则匹配完成后,停止匹配,不再匹配后面的规则
break 本条规则匹配完成后,停止匹配,不再匹配后面的规则
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301永远重定向,地址栏会显示跳转后的地址
rewrite ^(.*)$ /page/404.html last;    # 标记为last,不再往下匹配
rewrite ^(.*)$ /page/404.html break;
-----------------------------------------

 

5.last和break的区别

1)配置nginx测试

# 配置nginx
[root@web01 conf.d]# vim rewrite.conf
server {
    listen 80;
    server_name rw.linux.com;
    root /code/rewrite;
    
    location ~ ^/break {    # 显示404
        rewrite ^/break /test/ break;
    }
    location ~ ^/last {        # 显示ok
        rewrite ^/last /test/ last;
    }
    location /test/ {        # 用于测试break和last的效果,无实际用途
        default_type application/json;
        return 200 "ok";    # 这里写ok上面必须定义文件类型,否则就会下载
    }
}
# 重启nginx

# 访问页面测试

2)测试后结论

break 只要匹配到规则,就会去本地路径目录中寻找请求的文件;
last  匹配到规则,跳转后没有内容,则带着跳转后的请求,重新的向server发起一起请求

break请求:
    1.请求rw.linux.com/break;
    2.首先,会去查找本地的/code/rewrite/test/index.html;
    3.如果找到了,则返回/code/rewrite/test/index.html内容;
    4.如果没有找到则返回404,找到目录却没有主页,则返回403;
    
last请求:
    1.请求rw.linux.com/last;
    2.首先,会去查找本地的/code/rewrite/test/index.html;
    3.如果找到了,则返回/code/rewrite/test/index.html内容;
    4.如果没找到,会带着新跳转的URI再向server发起一次请求,请求rw.linux.com/text;
    5.如果匹配到新的location,则返回该location匹配的内容;
    6.如果没有匹配到新的,则再返回404或403;

 

6.redirect和permanent的区别

1)配置nginx测试

[root@web01 conf.d]# vim rewrite.conf
server {
    listen 80;
    server_name rw.linux.com;
    
    location /test {
        rewrite ^(.*)$ http://www.baidu.com redirect;    # 302 临时跳转
        #rewrite ^(.*)$ http://www.baidu.com permanent;    # 301 永久跳转
    }
}

# 配置两种跳转,关闭nginx测试,查看结果

2)结论

redirect:
    每次请求都会询问服务器,是否跳转,如果服务器不可用,则跳转失败
permanent:
    请求一次后,会记录跳转的地址,以后不再询问,直接跳转,通过浏览器缓存记录
# 一般配置permanent,如果redirect配的老的服务器不用了,就会有问题