Linux动静分离与Rewrite

一、动静分离

1.1 单台机器动静分离

Linux动静分离与Rewrite
1、创建NFS挂载点(NFS服务端)
mkdir /static
vim /etc/exports
/static      172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
systemctl restart nfs-server
chown -R www.www /static/

2、挂载到lb(LB服务端)
yum install nfs-utils -y
mount -t nfs 172.16.1.31:/static /opt/static/

3、将静态资源放置于挂载点内(web01)
mkdir /opt/static/s
[root@web01 static]# cp -r /opt/bbs/static/* /opt/static/s/

4、测试

1.2 多台机器动静分离

Linux动静分离与Rewrite
  1. 准备环境主机 作用 服务 IP lb01 负载均衡 nginx 172.16.1.5 web01 静态资源 nginx 172.16.1.7 web02 动态资源 tomcat 172.16.1.8
  2. 配置web01的静态资源
1.配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux.dj.com.conf
server {
    listen 80;
    server_name linux.dj.com;

    location ~* \.(jpg|png|mp4|gif)$ {
        root /code/picture;
    }
}
[root@web01 ~]# systemctl restart nginx

2.上传静态资源
[root@web01 ~]# mkdir /code/picture
[root@web01 ~]# cd /code/picture/
[root@web01 picture]# rz 1.jpg
[root@web01 picture]# ll
total 1756
-rw-r--r--. 1 root root  156617 Dec  7 08:54 1.jpg
-rw-r--r--. 1 root root   47542 Dec  7 08:54 2.jpg
-rw-r--r--. 1 root root 1586108 Dec  7 08:54 3.jpg

3.测试静态资源
    1)配置hosts
    172.16.1.7 linux.dj.com

    2)请求静态资源
    http://linux.dj.com/1.jpg
  1. 配置web02的动态资源
1.安装tomcat
[root@web02 ~]# yum install -y tomcat

2.配置动态资源
[root@web02 ~]# cd /usr/share/tomcat/webapps
[root@web02 webapps]# mkdir ROOT
[root@web02 webapps]# vim ROOT/java_test.jsp

        测试动态资源

        随机数:");
            out.println(rand.nextInt(99)+100);
        %>

3.启动方式
[root@web02 ~]# systemctl start tomcat

4.访问测试动态页面
    1)配置hosts
    172.16.1.8 linux.dj.com

    2)访问
    http://linux.dj.com:8080/java_test.jsp
  1. 配置LoadBalance负载均衡
1.配置
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.dj.com.conf
upstream dt {
    server 172.16.1.8:8080;
}

upstream jt {
    server 172.16.1.7;
}

server {
    listen 80;
    server_name linux.dj.com;

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

    location ~* \.(jpg|png|gif)$ {
        proxy_pass http://jt;
        include proxy_params;
    }

    location ~* \.(php|jsp)$ {
        proxy_pass http://dt;
        include proxy_params;
    }
}

2.重启
[root@lb01 ~]# systemctl restart nginx

3.访问测试
    1)配置hosts
    172.16.1.5 linux.dj.com

    2)访问
    http://linux.dj.com/java_test.jsp
    http://linux.dj.com/1.jpg
  1. 整合LoadBalance静态资源和动态资源
1.创建站点目录
[root@lb01 ~]# mkdir -p /code/dj

2.编辑html文件
[root@lb01 ~]# vim /code/dj/index.html

        测试ajax和跨域访问

$(document).ready(function(){
        $.ajax({
        type: "GET",
        url: "http://linux.dj.com/java_test.jsp",
        success: function(data){
                $("#get_data").html(data)
        },
        error: function() {
                alert("哎呦喂,失败了,回去检查你服务去~");
        }
        });
});

                测试动静分离

3.授权
[root@lb01 ~]# chown -R www.www /code/

4.访问域名测试
http://linux.dj.com/

结论:静态资源问题不影响动态资源,动态资源问题不影响静态资源。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Conclusion: problems in static resources do not affect dynamic resources, and problems in dynamic resources do not affect static resources.</font>*</details>

二、Nginx资源分离

2.1 准备环境

主机 IP 功能 lb01 172.16.1.5 负载均衡 web01 172.16.1.7 Android页面 web02 172.16.1.8 iPhone页面 web03 172.16.1.9 PC端页面

2.2 配置web01服务器

  1. 配置Nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
server {
    listen 80;
    server_name linux.sj.com;
    charset utf8;

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

[root@web01 ~]# systemctl restart nginx
  1. 创建站点目录
[root@web01 ~]# mkdir /code/android
[root@web01 ~]# echo "我是android" >> /code/android/index.html
[root@web01 ~]# chown -R www.www /code/android/
  1. 访问测试
1.配置hosts
172.16.1.7 linux.sj.com

2.3 配置web02服务器

  1. 配置Nginx
[root@web02 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
server {
    listen 80;
    server_name linux.sj.com;
    charset utf8;

    location / {
        root /code/iphone;
        index index.html;
    }
}
  1. 创建站点目录
[root@web02 ~]# mkdir /code/iphone
[root@web02 ~]# echo "我是Iphone" >> /code/iphone/index.html
[root@web02 ~]# chown -R www.www /code/iphone/
  1. 访问测试
1.配置hosts
172.16.1.8 linux.sj.com

2.4 配置web03服务器

  1. 配置Nginx
[root@web03 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
server {
    listen 80;
    server_name linux.sj.com;
    charset utf8;

    location / {
        root /code/pc;
        index index.html;
    }
}
[root@web02 ~]# systemctl restart nginx
  1. 创建站点文件
[root@web03 ~]# mkdir /code/pc -p
[root@web03 ~]# echo "我是pc端" >> /code/pc/index.html
[root@web03 ~]# chown -R www.www /code/pc/
  1. 访问测试
1.配置hosts
172.16.1.9 linux.sj.com

2.5 配置LoadBalance负载均衡

  1. 配置Nginx
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
upstream android {
    server 10.0.0.7;
}

upstream iphone {
    server 10.0.0.8;
}

upstream pc {
    server 10.0.0.9;
}

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

    location / {
        if ($http_user_agent ~* "Android") { #判断如果是安卓端
            proxy_pass http://android;       #代理到android虚拟主机池
        }
        if ($http_user_agent ~* "iPhone") {    #判断如果是苹果端
            proxy_pass http://iphone;       #代理到iphone虚拟主机池
        }
        if ($http_user_agent ~* "WOW64") {    #判断如果是IE浏览器
            return 403;                    #直接返回403
        }
        proxy_pass http://pc;               #如果没有匹配到以上内容,默认都代理到pc虚拟主机池
        include proxy_params;
    }
}
  1. 重启访问
1.检测并重启
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx

2.配置hosts
172.16.1.5 linux.sj.com

3.访问测试

三、Nginx的rewrite重写

3.1 Rewrite基本概述

3.1.1 什么是rewrite

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

3.1.2 rewrite使用场景
  • 地址跳转,如用户访问www.linux.com这个URL时,将其定向至一个新的域名www.baidu.com。
  • 协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式。
  • 伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态URL地址对外暴露过多的参数,提升更高的安全性。
  • 搜索引擎,SEO优化依赖于url路径,好记的url便于搜索引擎录入。
3.1.3 rewrite语法
Syntax: rewrite regex replacement [flag];
Default:    —
Context:    server, location, if

rewrite         #模块命令
regex           #请求的链接(支持正则表达式)
replacement     #跳转的链接
[flag];         #标签

eg:
location /download/ {
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  break;
    return  403;
}

3.2 Rewrite标记flag

rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记如下表格所示:

flag 作用 last 本条规则匹配完成后,停止匹配,不再匹配后面的规则 break 本条规则匹配完成后,停止匹配,不再匹配后面的规则 redirect 返回302临时重定向,地址栏会显示跳转后的地址 permanent 返回301永久重定向,地址栏会显示跳转后的地址

3.2.1 last和break的区别
  1. 配置Nginx(web01)
[root@web01 ~]# vim /etc/nginx/conf.d/linux.rewrite.com.conf
server {
    listen 80;
    server_name linux.rewrite.com;
    root /code;

    location ~ ^/break {
        rewrite ^/break /test/ break;
    }
    location ~ ^/last {
        rewrite ^/last /test/ last;
    }
    location /test/ {
        default_type application/json;
        return 200 "ok";
    }
}
  1. 检测并重启
[root@web01 ~]# 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 ~]# systemctl restart nginx
  1. 配置hosts访问测试
172.16.1.7 linux.rewrite.com
  1. 结论

    break 只要匹配到规则,就会去本地配置路径的目录中寻找请求的文件;
    而last只要匹配到规则,会对其所在的server(…)标签重新发起请求。 break请求:
    1.请求linux.rewrite.com/break
    2.匹配 location ~ ^/break 会跳转到 linux.rewrite.com/test
    3.请求跳转后,会去查找本地站点目录下的 /test
    4.如果找到了,则返回/code/test/index.html的内容;
    5.如果没找到该目录则报错404,如果找到该目录没找到对应的文件则报错403 last请求:
    1.请求linux.rewrite.com/last
    2.匹配 location ~ ^/last 会跳转到 linux.rewrite.com/test
    3.如果找到了,则返回/code/test/index.html的内容;
    4.如果没有找到,会重新对当前server发起请求,这个时候访问地址就变成 linux.rewrite.com/test
    5.重新请求server会匹配到 location /test/ 直接返回该location的内容
    6.如果也没有location匹配,再返回404;

3.2.2 redirect和permanent的区别
  1. 配置Nginx(web01)
[root@web01 ~]# vim /etc/nginx/conf.d/linux.rewrite.com.conf
server {
    listen 80;
    server_name linux.rewrite.com;
    root /code;

    location /test {
        rewrite ^(.*)$ https://www.baidu.com redirect;
        #rewrite ^(.*)$ https://www.baidu.com permanent;
    }
}
  1. 配置hosts测试
1.配置hosts
172.16.1.7 linux.rewrite.com

#关闭nginx后访问测试
    1)配置redirect时,访问失败,直接拒绝访问
    2)配置permanent时,访问成功,继续进行跳转,清除缓存后访问失败
  1. 结论

    redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。
    permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。

3.3 Rewrite案例

将http请求跳转到https(LoadBalance):

#Nginx跳转配置
server {
        listen 80;
        server_name linux.rewrite.com;
        rewrite ^(.*) https://$server_name$1 redirect;
        #return 302 https://$server_name$request_uri;
}

server {
        listen 443 ssl;
        server_name blog.driverzeng.com;
        # ssl on;
}

URL: http://www.baidu.com:80/code/index.html
URI: /code/index.html

3.4 Rewrite伪静态

搭建discuz论坛(web01)
  1. 创建站点目录
[root@web01 ~]# mkdir /code/discuz
  1. 解压代码
1.上传代码包
[root@web01 ~]# rz
[root@web01 ~]# ll
-rw-r--r--. 1 root root 10829853 Dec  7 12:04 Discuz_X3.3_SC_GBK.zip

2.解压
[root@web01 ~]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/
[root@web01 ~]# chown -R www.www /code/discuz/
  1. 配置Nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/linux.discuz.com.conf
server {
    listen 80;
    server_name linux.discuz.com;
    root /code/discuz/upload;

    location / {
        root /code/discuz/upload;
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  1. 检测并重启
[root@web01 ~]# 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 ~]# systemctl restart nginx
  1. 配置hosts访问测试
172.16.1.7 linux.discuz.com

http://linux.discuz.com/install/
  1. 根据页面操作
  2. 创建数据库
[root@db01 ~]# mysql -uroot -p123

#建库
MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)

#授权用户
MariaDB [(none)]> grant all on discuz.* to discuz@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
  1. 配置伪静态
[root@web01 ~]# vim /etc/nginx/conf.d/linux.discuz.com.conf

server {
    listen 80;
    server_name linux.discuz.com;
    root /code/discuz/upload;

    location / {
        root /code/discuz/upload;
        index index.php;

        rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
        rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
        rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
        rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
        rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
        rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
        rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
        if (!-e $request_filename) {
                return 404;
        }
    }

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

四、Rewrite补充

4.1 rewrite匹配优先级

  1. 优先级
  2. 先执行server块的rewrite指令
  3. 其次执行location匹配规则
  4. 最后执行location中的rewrite
  5. 配置
server {
    listen 80;
    server_name linux.youxianxji.com;

    rewrite (.*) http://www.baidu.com;

    location / {
        rewrite (.*) http://www.jd.com;
    }

    location =/ {
        rewrite (.*) http://www.taobao.com;
    }
}

4.2 rewrite的环境变量

  1. $server_name
$server_name    #当前用户请求的域名

server {
        listen 80;
        server_name linux.test.com;
        rewrite ^(.*)$ https://$server_name$1;
}
  1. 请求变量
$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)

$request_uri 当前请求的文件路径(不带网站的主目录/images/test.jpg)

#大多数用于http协议转https协议
server {
        listen 80;
        server_name linux.test.com;
        return 302 https://$server_name$request_uri;
}
  1. $http_host
#很古董的配置方法
server {
        listen 80;
        server_name www.baidu.com baidu.com www.baidu.cn;
        if ($http_host = baidu.com){
            rewrite (.*) http://www.baidu.com$1;
        }
}

#推荐书写格式
server {
        listen 80;
        server_name baidu.com;
        rewrite (.*) http://www.baidu.com$1;
}
server {
        listen 80;
        server_name www.baidu.com;
        location / {...}
}

4.3 rewrite开启日志

[root@web01 ~]# vim /etc/nginx/nginx.conf
... ...

error_log  /var/log/nginx/error.log notice;
... ...

http {
    ... ...

    rewrite_log on;
    ... ...

}

Original: https://www.cnblogs.com/JZjuechen/p/15777940.html
Author: JZEason
Title: Linux动静分离与Rewrite

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/524194/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Linux安装nginx

    Linux安装nginx (1)安装c++编译环境装备yum install gcc gcc-c++ (2)安装PCRE库tar -zxvf pcre-8.38.tar.gzcd …

    Linux 2023年6月13日
    058
  • LeetCode-26. 删除有序数组中的重复项

    题目来源 题目详情 由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应…

    Linux 2023年6月7日
    081
  • 绝了!起个好标题的9大技巧

    许多自媒体经常发一些标题雷人的文章,内容却非常空洞甚至低俗,技术创作领域也未能幸免,这个搞法被大家笑称为”标题党”。互联网是眼球经济,靠标题骗点击量的恶习将…

    Linux 2023年6月6日
    087
  • 如何解决 QMediaPlayer 占用歌曲导致 PermissionError: [Error 13] 的问题

    问题描述 当我们使用 QMediaPlayer 播放歌曲时,歌曲文件的句柄会被占用。如果想使用 mutagen 库对正在播放地歌曲进行数据写入,就会出现下述问题: Tracebac…

    Linux 2023年6月7日
    080
  • Typora Ubuntu 安装

    官网方法 终端命令行安装 or use sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys BA300B77…

    Linux 2023年6月7日
    080
  • Docker 安装 ElasticSearch 及失败解决方法[Ubuntu 20.04]

    1. 下载 ElasticSearch 最开始使用 docker search命令搜索后,就直接使用 docker pull命令拉取镜像,但没想到拉取失败,后来到官网看了看,原来没…

    Linux 2023年6月14日
    0123
  • 数字图像处理

    1. 图像的基本概念 连续图像:二维坐标系上连续变化的图像,图像的像点无限稠密。 离散图像:用数字序列表示的图像,像素是组成图像的基本单位。 1.1 图像数字化采样 图像经过采样与…

    Linux 2023年6月14日
    071
  • Web开发静态资源处理

    Web开发静态资源处理 7.1 静态资源处理 我们要引入前端资源,项目中有许多的静态资源,比如css,js等文件,这个SpringBoot是怎么处理呢? 如果我们是一个web应用,…

    Linux 2023年6月14日
    097
  • 制作pip离线源

    一、下载指定的包到相应的文件夹 (1)、创建存放安装包的目录:(联网环境) bash;gutter:true; pip list #查看安装的包 mkdir packs pip i…

    Linux 2023年6月8日
    089
  • 快速构建Web应用,从零学习React后台项目模版

    想要快速构建实际应用,离不开一个好的应用模版,React作为大厂出品工具,有着稳定性和可维护性的保障,同时可以使用相关的全套全家桶(React + React-router + A…

    Linux 2023年5月27日
    084
  • 文件相关命令

    pwd指令 基本语法:pwd功能:显示当前工作的绝对目录 ls指令 基本语法:ls [选项][目录或者文件]常用选项 -a 显示所有文件及目录 (. 开头的隐藏文件也会列出) -l…

    Linux 2023年6月6日
    064
  • 当保存参数使用结构体时必备的开发技巧方式

    1、前言 想必做嵌入式产品开发都遇到过设备需要保存参数,常用的方式就是按照结构体的方式管理参数,保存时将整个结构体数据保存在 Flash 中,方便下次读取。 1.1、目的 本文时分…

    Linux 2023年6月7日
    083
  • ES查询区分大小写

    ES查询在默认的情况下是不区分大小写的,在5.0版本之后将 string类型拆分成两种新的数据类型, text用于全文搜索(模糊搜索), keyword用于关键字搜索(精确搜索)。…

    Linux 2023年6月8日
    096
  • 使用 Powershell 删除N天前的文件

    在 Linux 中删除N天前的文件可以使用以下命令: find /path/to -maxdepth 1 -name "filename" -mtime +1 …

    Linux 2023年6月14日
    069
  • 常用命令记录

    npm仓库查看和修改 npm config set registry https://registry.npm.taobao.org #设置使用淘宝提供的npm仓库 npm con…

    Linux 2023年6月14日
    057
  • Redis缓存三大问题解析,看完保你面试能造火箭,工作能拧螺丝。

    前言 日常的开发中,无不都是使用数据库来进行数据的存储,由于一般的系统任务中通常不会存在高并发的情况,所以这样看起来并没有什么问题。 面试10家公司,收获9个offer,2020年…

    Linux 2023年5月28日
    079
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球