第十七章 nginx动静分离和rewrite重写

一、动静分离

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

 

 

1.单台机器动静分离

#配置
[root@web01 /code]# cat /etc/nginx/conf.d/linux.blog.com.conf
server {
listen 80;
server_name linux.blog.com;
root /code/wordpress;

location / {
index index.php;
}

location ~* .(jpg|png)$ {
root /code/pic;
root /code/wordpress;
}

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

#创建目录
[root@web01 /code]# mkdir /code/pic
#做软连接
[root@web01 /code]# ln -s /code/wordpress/wp-content /code/pic

 

2.多台机器做动静分离

1)环境准备
主机IP身份
lb01 10.0.0.4,172.16.1.4 负载均衡
web01 172.16.1.7 静态资源
web03 172.16.1.9 动态资源
2)web01配置静态资源
[root@web01 /code]# vim /etc/nginx/conf.d/jt.conf
server {
  listen 80;
  server_name linux.djfenli.com;

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

#重启
[root@web01 /code]# systemctl restart nginx
#上传一些图片
[root@web01 /code]# mkdir pic
[root@web01 /code/pic]# ll
total 1188
-rw-r--r-- 1 root root 407030 Sep  2 12:22 1.gif
-rw-r--r-- 1 root root 298866 Sep  2 12:21 3_web01.jpg
-rw-r--r-- 1 root root 410120 Sep  2 12:22 4_web02.jpg
-rw-r--r-- 1 root root  60494 Sep  2 12:21 timg_(2).jpg
-rw-r--r-- 1 root root  30607 Sep  2 12:21 timg_(3).jpg
3)web03配置动态资源
#部署tomcat
[root@web03 ~]# yum install -y tomcat
[root@web03 ~]# mkdir /usr/share/tomcat/webapps/ROOT
[root@web03 ~]# cat /usr/share/tomcat/webapps/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>

[root@web03 ~]# systemctl start tomcat
[root@web03 ~]# netstat -lntp
tcp6       0      0 :::8009                 :::*                   LISTEN      34369/java
tcp6       0      0 :::8080                 :::*                   LISTEN      34369/java
tcp6       0      0 127.0.0.1:8005         :::*                   LISTEN      34369/java
4)配置负载均衡
[root@lb01 ~]# cat /etc/nginx/conf.d/linux.djfenli.com.conf
upstream jt {
server 172.16.1.7:80;
}

upstream dt {
  server 172.16.1.9:8080;
}

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

location ~* .gif$ {
proxy_pass http://jt;
include proxy_params;
}

location ~* .jsp$ {
proxy_pass http://dt;
include proxy_params;
}
}
5)检查配置文件并重启
[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
6)配置hosts访问
10.0.0.4 linux.djfenli.com

二、nginx资源分离

1.资源分离

Nginx通过负载均衡实现手机与PC调度至不通的后端节点应用案例

2.环境准备

主机IP资源端口
lb01 10.0.0.4 负载均衡 80
web01 172.16.1.7 Android的页面 8081
web01 172.16.1.7 iphone的页面 8082
web01 172.16.1.7 pc的页面 8083

3.配置web服务器

[root@web01 ~]# vim /etc/nginx/conf.d/linux.ziyuan.com.conf
[root@web01 ~]# cat /etc/nginx/conf.d/linux.ziyuan.com.conf
server {
listen 8081;
server_name linux.ziyuan.com;

location / {
root /code/android;
index index.html;
}
}
server {
  listen 8082;
  server_name linux.ziyuan.com;

  location / {
      root /code/iphone;
      index index.html;
  }
}
server {
  listen 8083;
  server_name linux.ziyuan.com;

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

[root@web01 ~]# systemctl restart nginx

4.配置站点目录

[root@web01 ~]# mkdir /code/{android,iphone,pc}
[root@web01 ~]# echo "我是pc" > /code/pc/index.html
[root@web01 ~]# echo "我是iphone" > /code/iphone/index.html
[root@web01 ~]# echo "我是android" > /code/android/index.html
[root@web01 ~]# chown -R www:www /code/

5.配置负载均衡

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.ziyuan.com.conf
upstream Android {
  server 172.16.1.7:8081;
  server 172.16.1.8:8081;
}

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

  location / {
       if ($http_user_agent ~* "Windows") {
          proxy_pass http://172.16.1.7:8083;
      }

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

       if ($http_user_agent ~* "Android") {
          proxy_pass http://Android;
      }
       
      return 500;
  }
}

6.配置hosts访问测试

10.0.0.4 linux.ziyuan.com

三、nginx的rewrite重写

1.什么是rewrite?

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

2.rewrite使用场景

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

3.rewrite语法

Syntax: rewrite regex replacement [flag];
Default:
Context: server, location, if

rewrite #调用模块
regex #请求的链接(可以使用正则表达式)
replacement #跳转的链接
[flag]; #标签

#示例
server {
  ...
  rewrite ^(/download/.*)/media/(.*)..*$ $1/mp3/$2.mp3 last;
  rewrite ^(/download/.*)/audio/(.*)..*$ $1/mp3/$2.ra last;
  ...
}

4.rewrite的flag标记

rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示:
flag作用
last 本条规则匹配完成后,停止匹配,不再匹配后面的规则
break 本条规则匹配完成后,停止匹配,不再匹配后面的规则
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301永久重定向,地址栏会显示跳转后的地址

5.last和break的区别

1.配置nginx
[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";
      }
}

2.重启
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl restart nginx

3.配置hosts测试
10.0.0.7 linux.rewrite.com

#结果
1)访问 linux.rewrite.com/break,返回结果404
2)访问 linux.rewrite.com/last,返回结果ok

4.结论
break只要匹配到规则,则会去本地配置路径的目录中寻找请求的文件;
而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。

break请求:
   1)请求 linux.rewrite.com/break
   2)匹配 location ~ ^/break 会跳转请求 到 linux.rewrite.com/test
   3)请求跳转后,会去查找本地的站点目录下的 test/index.html;
   4)如果找到了,则返回站点目录下的 test/index.html的内容;
   5)如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403

last请求:
   1)请求 rewrite.drz.com/last
   2)匹配 location ~ ^/last 会跳转请求 到 linux.rewrite.com/test
   2)请求跳转后,会去查找本地的站点目录下的 test/index.html;
   3)如果找到了,则返回站点目录下的 test/index.html的内容;
   4)如果没找到,会对当前server重新的发起一次请求,linux.rewrite.com/test/
   5)如果有location匹配上,则直接返回该location的内容。
   4)如果也没有location匹配,再返回404;

 


 6.redirect和permanent的区别

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

  location /test {
       #rewrite ^(.*)$ https://www.baidu.com redirect;
       #rewrite ^(.*)$ https://www.baidu.com permanent;
       #return 301 https://www.baidu.com;
      return 302 https://www.baidu.com;
  }
}

2.配置hosts测试
#配置redirect时
关闭nginx之后访问失败
#配置permanent时
关闭nginx仍然访问成功

#结论:
redirect,每次访问服务器都会进行询问,是否进行跳转
permanent,记录一次跳转,以后都不会询问,直接跳转页面,除非清空缓存

四、rewrite实践

1.案例一:用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html
1.创建页面
[root@web01 ~]# mkdir /code/ccc/bbb/ -p
[root@web01 ~]# echo "/ccc/bbb/2.html" > /code/ccc/bbb/2.html

2.配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
  listen 80;
  server_name nginx.rewrite.com;
  root /code;

  location ~* /abc {
      rewrite ^(.*)$ /ccc/bbb/2.html redirect;
  }
}

2.案例二:用户访问/2018/ccc/bbb/2.html实际上真实访问的是/2014/bbb/ccc/2.html

[root@web01 ~]# mkdir /code/2014/bbb/ccc/ -p
[root@web01 ~]# echo "/2014/bbb/ccc/2.html" > /code/2014/bbb/ccc/2.html

#配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
  listen 80;
  server_name nginx.rewrite.com;
  root /code;

  location ~* ^/2018 {
      rewrite ^/2018/(.*)/(.*)/(.*) /2014/$2/$1/$3 redirect;
  }
}

3.案例三:用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html

[root@web01 ~]# mkdir /code/course/11/22/33/ -p
[root@web01 ~]# echo "/course/11/22/33/course_33.html" >/code/course/11/22/33/course_33.html

#配置
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
  listen 80;
  server_name nginx.rewrite.com;
  root /code;

  location ~* ^/course {
       #灵活配法
      rewrite ^/(.*)-(.*)-(.*)-(.*).html$ /$1/$2/$3/$4/$1_$4.html redirect;
       #固定配法
       #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
  }
}

4.案例四:将http请求跳转到https

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

server {
      listen 443;
      server_name www.baidu.com;
      ssl on;
}

浏览器输入:baidu.com
浏览器转换:http://www.baidu.com/index.html
server层转换,rewrite跳转:https://www.baidu.com/index.html

五、rewrite实现伪静态

1.搭建discuz论坛

1.创建站点目录
[root@web01 /code]# mkdir discuz
[root@web01 /code]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/
#授权
[root@web01 /code]# chown -R www.www /code/discuz/

2.配置nginx配置文件
[root@web01 /code]# vim /etc/nginx/conf.d/linux.discuz.com.conf
server {
  listen 80;
  server_name linux.discuz.com;
  root /code/discuz/upload;

  location / {
      index index.php index.html;
  }

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

3.重启访问
[root@web01 /code]# nginx -t
[root@web01 /code]# systemctl restart nginx

#配置hosts
10.0.0.7 nginx.rewrite.com linux.discuz.com

4.创建数据库
[root@db01 ~]# mysql -uroot -pLinhd@123

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.01 sec)

5.配置伪静态
[root@web01 /code]# cat /etc/nginx/conf.d/linux.discuz.com.conf
server {
listen 80;
server_name linux.discuz.com;
root /code/discuz/upload;

  location / {
      index index.php index.html;
  }
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 ^([^.]*)/forum-(w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;

http://linux.discuz.com/forum-2-1.html
http://linux.discuz.com/forum.php?mod=forumdisplay&fid=2&page=1

六、rewrite补充

1.rewrite匹配优先级

1.匹配优先级
1)先执行server层的rewrite
2)再根据location匹配优先级匹配
3)再执行location下的rewrite
4)最后再执行location下if配置的rewrite

2.配置
[root@web01 /code]# vim /etc/nginx/conf.d/youxianji.conf
server {
  listen 80;
  server_name linux.youxian.com;
  location / {
      rewrite (.*) http://www.jd.com;
  }

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

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

2.rewrite的环境变量

1.$server_name

$server_name #当前请求的域名

server {
      listen 80;
      server_name test.linux.com;
      rewrite ^(.*)$ https://$server_name$1;
}

http://test.linux.com/1.html
https://test.linux.com/1.html

2.请求文件
$request_filename #请求的文件路径名(带网站的站点目录 /code/images/test.jpg)
$request_uri #当前请求的文件路径(不带网站的站点目录 /images/test.jpg)

#大多数用于http协议转https协议
server {
      listen 80;
      server_name test.linux.com;
      root /code;
      return 302 https://$server_name$request_uri;
}

URL: http://test.linux.com/images/test.jpg
URI: /images/test.jpg

3.$scheme
$scheme     用的协议,比如http或者https

4.$http_host
#很久之前的配置方法
server {
      listen 80;
      server_name www.baidu.com baidu.com;
       if ($http_host = baidu.com){
          rewrite (.*) http://www.baidu.com$1;
      }
}

#推荐书写格式
server {
      listen 80;
      server_name baidu.com;
      return 302 http://www.baidu.com$request_uri;
}
server {
      listen 80;
      server_name www.drz.com;
}

3.rewrite开启日志

#配置
[root@web01 /code]# vim /etc/nginx/nginx.conf
... ...
error_log /var/log/nginx/error.log notice;
... ...
http {
  ... ...
  rewrite_log on;
  ... ...
}

 

原文地址:https://www.cnblogs.com/jhno1/p/13608562.html