0
点赞
收藏
分享

微信扫一扫

nginx实现rewrite重写

ivy吖 2022-06-21 阅读 21

什么是rewrite

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

rewrite使用场景

  • 地址跳转
  • 将www.baidu.com跳转成:www.taobao.com
  • 协议跳转
  • ​​http://sdfhfnb.com跳转成:https://sdfhfnb.com​​
  • 伪静态
  • 将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性
  • 搜索引擎,seo优化依赖于url路径,好记得url便于智齿搜索引擎录入


伪静态的配置

句法:syntax:rewrite regex replacement [flag]
默认:default:
语境:context:server,location,if

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

rewrite http://taobao.com http://www.taobao.com;
重写跳转

# 用于切换维护页面场景
#rewrite ^(.*)$ /paga/maintain.html break;

如果懂得shell脚本,这两个就类似于脚本中的,break和continue

rewrite的flag

flag

概述

last

匹配到last的规则后可以继续匹配后面的location

break

匹配到break的规则后,无法在匹配到后面的location

redirect

302临时重定向(关闭nginx后直接失效)

permanent

301永久重定向(关闭nginx依旧可以跳转,但是不能清除缓存,不然失效)

# redirect临时重定向配置
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.yjt.com;
root /code;
index index.html;

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

## 第二种写法(不会写入日志,是一种写死的写法)
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.yjt.com;
root /code;
index index.html;

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

# 重新加载nginx
[root@web01 ~]# systemctl reload nginx

# 域名解析
10.0.0.7 rewrite.yjt.com

验证第一种方式结果

浏览器输入rewrite.yjt.com/test

 nginx实现rewrite重写_html

验证第二种方式结果

浏览器中输入rewrite.yjt.com/test

 nginx实现rewrite重写_php_02

# permanent永久重定向配置
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf

server {
listen 80;
server_name rewrite.yjt.com;
root /code;
index index.html;

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

## 第二种方式(不会写入日志,是一种写死的写法)
[root@web01 ~]# cat /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.yjt.com;
root /code;
index index.html;

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

# 重新加载nginx
[root@web01 ~]# systemctl reload nginx

# 域名解析
10.0.0.7 rewrite.yjt.com

验证第一种方式结果

浏览器输入rewrite.yjt.com/test

 nginx实现rewrite重写_html_03

验证第二种方式结果

浏览器输入rewrite.yjt.com/test

 nginx实现rewrite重写_html_04

百度的内部跳转

 nginx实现rewrite重写_php_05

rewrite实践

开启rewrite日志

# 开启rewrite日志,错误日志的级别要改成 notice,在HTTP层加上rewrite——log on;
[root@web01 ~]# cat /etc/nginx/nginx.conf
error_log /var/log/nginx/error.log notice;

http {
...
rewrite_log on;
...
}

# 重启nginx
[root@web01 ~]# systemctl restart nginx

案例一

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

server {
listen 80;
server_name rewrite.yjt.com;
root /code;
index index.html;

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

案例二

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

### 写法一(这是一种写死的写法)
server {
listen 80;
server_name rewrite.yjt.com;
root /code;
index index.html;

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

# 正则后向引用匹配
server {
listen 80;
server_name rewrite.yjt.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

server {
listen 80;
server_name rewrite.yjt.com;
root /code;
index index.html;

location /course {
rewrite course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html redirect;
}
}

 nginx实现rewrite重写_nginx_06

案例四

80端口强制跳转443端口

server{
listen 80;
server_name www.dirverzeng.com;
rewrite ^(.*) https://$server_name redirect;
#return 302 https://$server_name$request_uri;
}

server{
listen 80;
server_name www.dirverzeng.com;
rewrite www.dirverzeng.com/(.*) https://$server_name$1 redirect;
#return 302 https://$server_name$request_uri;
}

rewrite做伪静态

# 加入location /下

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;
}

然后进wordpress 改

 nginx实现rewrite重写_html_07

腾讯论坛(discuz)安装

 nginx实现rewrite重写_html_08

授权


 nginx实现rewrite重写_php_09

 nginx实现rewrite重写_html_10

 nginx实现rewrite重写_html_11 nginx实现rewrite重写_nginx_12 nginx实现rewrite重写_html_13 nginx实现rewrite重写_html_14

去里面找到rewrite规则 nginx实现rewrite重写_nginx_15

# 内容
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
}
举报

相关推荐

0 条评论