Nginx实现Rewrite重写

Nginx实现Rewrite重写

Rewrite基本概述

什么是rewrite

Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他的url的过程。比如访问baidu.com就会重定向到www.baidu.com。

Rewrite使用场景

  1. url地址跳转

url地址跳转,例如用户访问lw.com就会跳转到roweyy.com

  1. 协议跳转

用户通过http协议请求网站时,将其重新跳转至https协议方式

  1. 伪静态

伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态URL地址对外暴露过多的参数,提升更高的安全性。

  1. SEO优化

搜索引擎SEC优化依赖于url路径,好记的url以便支持搜索引擎录入

  1. 用户方面

可以调整用户浏览的url,看起来更规范,合乎开发及产品人员的需求例如根据用户访问手机型号,调整适配用户手机屏幕的url

Rewrite配置语法

句法:Syntax: rewrite regex replacement [flag]
默认:Default: --
语境:Context: server,location,if

rewrite:模块
regex:正则表达式(匹配当前的url)
replacement:要替换成的url

Rewrite标记Flag

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

flag 作用
last 匹配到last的规则后可以继续匹配后面的location
break 匹配到break的规则后无法再匹配后面的location
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301临时重定向,地址栏会显示跳转后的地址

last与break区别对比

last、break开发用的比较多,作用类似Python中的break和continue

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

       location ~ ^/break {
               rewrite ^/break /test/ break;
               # 如果配置flag是break的话那么将停止检索,输入域名rewrite.lw.com/text,直接读取/code/text/index.html下的文件
       }

       location ~ ^/last {
               rewrite ^/last /test/ last;
               # 如果配置flag是last的话那么将继续检索,输入域名rewrite.lw.com/text,会跳转到下方的location/test.
       }

       location /test/ {
               default_type application/json;
                # 定义字符串类型,参照/etc/nginx/mime.types文件
               return 200 "ok";
                # 返回ok
       }
}

2. 重启Nginx服务
systemctl restart nginx

3. 在本机添加域名解析
C:\Windows\System32\drivers\etc
在hosts文件添加域名解析
10.0.0.8		rewrite.	

我们在浏览器中输入rewrite.lw.com/last,可以正常访问到ok。
输入rewrite.nana.com/break,页面返回404错误。因为我们没有在/code/text/index.html创建文件
#总结
break请求:
1、请求rewrite.drz.com/break
2、首先:会去查找本地的/code/test/index.html;
3、如果找到了,则返回/code/test/index.html的内容;
4、如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403
 
last请求:
1、请求rewrite.drz.com/last
2、首先:会去查找本地的/code/test/index.html;
3、如果找到了,则返回/code/test/index.html的内容;
4、如果没找到,会对当前server重新的发起一次请求,rewrite.drz.com/test/
5、如果有location匹配上,则直接返回该location的内容。
4、如果也没有location匹配,再返回404;

redirect与permanent区别对比示例

[root@web01 conf.d]# vim rewrite.conf 
server {
        listen 80;
        server_name rewrite.lw.com;
        root /code;
 
        location /test {
                rewrite ^(.*)$  https://roweyy.com redirect;
                #rewrite ^(.*)$  https://roweyy.com permanent;
                #return 301 https://roweyy.com;
                #return 302 https://roweyy.com;
        }
}

##测试
打开浏览器,输入rewrite.lw.com/test,会自动跳转到https://roweyy.com
关闭Nginx服务(systemctl stop nginx),我们会发现输入rewrite.lw.com/test,页面显示404。
1. 修改Nginx配置文件
[root@web01 conf.d]# vim rewrite.conf 
server {
        listen 80;
        server_name rewrite.lw.com;
        root /code;
 
        location /test {
                #rewrite ^(.*)$  https://roweyy.com redirect;
                rewrite ^(.*)$  https://roweyy.com permanent;
                #return 301 https://roweyy.com;
                #return 302 https://roweyy.com;
        }
}

2. 重启Nginx服务
systemctl restart nginx

##测试
打开浏览器,输入rewrite.lw.com/test,会自动跳转到https://roweyy.com
我们会发现输入rewrite.lw.com/test,依旧可以跳转到我的个人博客网站
#总结
redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。
 
permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。

Rewrite规则实践

在写rewrite规则之前,我们需要开启rewrite日志对规则的匹配进行调试

[root@web01 code]# vim /etc/nginx/nginx.conf
/var/log/nginx/error.log notice;

http{
    rewrite_log on;
}

案例一

用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html

#1.准备真实访问路径
[root@web03 ~]# mkdir /code/ccc/bbb -p
[root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html
 
#2.Nginx跳转配置
[root@web03 conf.d]# vim ccbb.conf 
server {
	listen 80;
	server_name rewrite.lw.com;
	root /code;
	index index.html;
	
	location /abc/1.html {
	  rewrite ^(.*)$ /ccc/bbb/2.html redirect;
	}
}

#3.重启Nginx服务
[root@web03 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@web03 conf.d]# nginx -s reload

案例二

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

### rewrite写死。。。
server {
	listen 80;
	server_name rewrite.zls.com;
	root /code;
	index index.html;
	
	location /2018/ccc {
	  rewrite ^(.*)$ /2014/ccc/bbb/2.html redirect;
	}
}

#假如用户想访问/code目录下/2014/ccc/bbb/3.html将访问不到

#解决方法
### 正则后向引用匹配
[root@web01 nginx]# vim /etc/nginx/conf.d/rewrite.conf
server {
	listen 80;
	server_name rewrite.lw.com;
	root /code;
	index index.html;

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

案例三

用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html

[root@web01 nginx]# vim /etc/nginx/conf.d/rewrite.conf
server {
	listen 80;
	server_name rewrite.lw.com;
	root /code;
	index index.html;
	
	
	location /course {
	#灵活配法
	  rewrite course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html redirect;
	#固定配法
     #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;  
	}
}

案例四http请求跳转到https

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

rewrite做伪静态

wordporess伪静态代码
if ( -f $request_filename/index.html ){
	rewrite (.*) $1/index.html break;
}
if ( -f $request_filename/index.php ){
	rewrite (.*) $1/index.php;
}
if ( !-f $request_filename ){
	rewrite (.*) /index.php;
}

Rewrite场景示例

#部署discuz论坛
[root@web01 conf.d]# vim discuz.lw.com.conf
server {
        listen 80;
        server_name discuz.lw.com;
 
        location / {
                root /code/discuz;
                index index.php index.html;
        }
 
        location ~ \.php$ {
                root /code/discuz;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
 
#创建站点目录部署代码
[root@web01 ~]# mkdir /code/discuz
[root@web01 ~]# rz Discuz_X3.3_SC_GBK.zip
[root@web01 ~]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/
 
#授权站点目录
[root@web01 discuz]# chown www.www -R /code/
 
#创建数据库
MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)
 
MariaDB [(none)]> grant all on discuz.* to discuz@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

server {
        listen 80;
        server_name discuz.lw.com;
 
        location / {
                root /code/discuz/upload;
                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;
                if (!-e $request_filename) {
                    return 404;
                }
        }
 
        location ~ \.php$ {
                root /code/discuz/upload;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
posted @ 2022-06-21 16:43  Gie  阅读(339)  评论(0编辑  收藏  举报