Nginx实现Rewrite重写

Nginx实现Rewrite重写

什么是rewrite

rewrite主要实现uri地址重写,以及重定向,就是把传入web的请求重定向到其他uri的过程。
做伪静态,将动态页面转换成静态的页面uri

Rewrite使用场景

1.地址跳转
	将 www.taibao.com跳转成:main.taobao.com
2.协议跳转
	http://baidu.com 跳转成:https://baidu.com
3.伪静态
	- 将动态页面显示为静态页面的一种技术,方便搜索引擎的录入,同时减少动态URI地址对外暴露过多的参数,提升更高的安全性
	- 搜索引擎,SEO优化依赖于uri路径,记号的uri便于支持搜索引擎录入

伪静态的配置

语法 :Syntax: rewrte regex relacement [flag]
默认:Default :--
语境:Context: server,lovation,if

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

rewrite的flag

概述 flag
匹配到last的规则后可以继续匹配后面的location last
匹配到break的规则后,无法再匹配后面的location break
302临时重定向 redirect
301永久重定向 permanent

last和break的区别

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

        location /break {
                rewrite ^/break /test/ break;
        }
        location /last {
                rewrite ^/last /test/ last;
        }
        location /test/ {
                default_type application/json;
                return 200 "ok";
        }
}

浏览器访问break

浏览器访问last

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

# break请求
1.请求rewrite.zh.com/break
2.首先 会去查找本地的/code/test/index.html;
3.如果找到了,则会返回/code/test/index.html的内容;
4.如果没找到该目录则会报错404,如果找到了该目录没找到对应的文件则403

# last请求
1.请求rewrite.zh.com/last
2.首先 会去找本地的/code/test/index.html;
3.如果找到了,则会返回/code/test/index.html的内容
4.如果没找到,会对当前server重新发一次请求,rewrite.zh.com/test/
5.如果没有location匹配上,则直接返回location的内容。
6.如果也没有location匹配,再返回404;


所以在访问/break和/last请求时,虽然对应的请求目录/test都是不存在的,理论上都应该返回404,但是实际请求/last的时候,是会有后面location所匹配到的结果返回的。

redirect和permanent的区别

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

        location /test {
                rewrite ^(.*)$ https://www.baidu.com redirect;
        }
}

浏览器访问

关闭nginx后访问

# permanent
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf 
server{
        listen 80;
        server_name rewrite.zh.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;
        }
}

浏览器访问

关闭nginx后访问

# redirect
每次请求都会询问服务器,如果服务器不可用时,则会跳转失败

# permanent
第一次请求会询问,浏览器会记录跳转地址,第二次则不再询问服务器,直接通过缓存的地址跳转

rewrite实践

开启rewrite日志

# 在写rewrite规则之前,我们需要开启rewrite日志对规则的匹配进行调试
[root@web01 ~]# vim /etc/nginx/nginx.conf
/var/log/nginx/error.log notice;
 
http{
    rewrite_log on;
}

案例一

# 用户访问/abc/1.html 实际上真实访问的是/ccc/bbb/2.html
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf 
server{
        listen 80;
        server_name rewrite.zh.com;
        root /code;
        location /abc/1.html {
                rewrite  ^(.*)$ /ccc/bbb/2.html redirect;
        }
}

案例二

# 用户访问/2018/ccc/2.html 实际上真实访问的是 /2014/ccc/bbb/2.html
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf 
server{
        listen 80;
        server_name rewrite.zh.com;
        root /code;
        location /2018/ccc {
                rewrite  ^(.*)$ /2014/ccc/bbb/2.html redirect;
        }
}

案例三

# 用户访问course-11-22-33.html实际上访问的是/course/11/22/33/course_33.conf
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf  
server{
        listen 80;
        server_name rewrite.zh.com;
        root /code;
        location /course {
                rewrite  course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.h
tml redirect;
        }
}

案例四

# 80端口强制跳转443端口
server{ 
        listen 80;
        server_name rewrite.zh.com;
        rewrite ^(.*) https://$server_name redirect;
        #return 302 https://$server_name$request_uri;
}

rewrite做伪静态

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 ^([^\.]*)/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;
}

修改配置文件

[root@web01 code]# vim /etc/nginx/conf.d/rewrite.conf 
server{
        listen 80;
        server_name rewrite.zh.com;
        root /code/upload;
        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;

        }
        location / {
                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;
}

        }
}

# 重新加载配置文件
[root@web01 code]# systemctl reload nginx

# 查看页面,此时显示为静态页面

posted @   FYytfg  阅读(581)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示