Nginx Rewrite重写

Nginx的Rewrite重写

一、Rewrite基本概述

  • 什么是rewrite

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

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

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

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

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

二、Rewrite标记Flag

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

# 配置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";
    }   
}

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

# 配置hosts访问测试
10.0.0.7 linux.rewrite.com
  • 结论
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;
  • redirect和permanent的区别

# 配置nginx
[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;
    }
}

# 配置hosts测试
1.配置hosts
10.0.0.7 linux.rewrite.com

#关闭nginx访问测试
	1)配置redirect时,访问失败,直接拒绝访问
	2)配置permanent时,访问成功,继续进行跳转,清除缓存后访问失败
  • 结论
redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。
permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。

三、rewrite案例

  • 将 http 请求跳转到 https
#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;
        server_name blog.driverzeng.com;
        ssl on;
}

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

四、rewrite的伪静态

  • 搭建discuz论坛

创建站点目录

[root@web01 ~]# mkdir /code/discuz

解压代码

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/

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

重启

[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

配置hosts访问测试

10.0.0.7 linux.discuz.com
http://linux.discuz.com/install/

根据页面操作

创建数据库

[root@db01 ~]# mysql -uroot -p3306

#建库
MariaDB [(none)]> create database discuz;

#授权用户
MariaDB [(none)]> grant all on discuz.* to discuz@'172.16.1.%' identified by '123456';
  • 配置伪静态
[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匹配优先级

  • 优先级
1.先执行server块的rewrite指令
2.其次执行location匹配规则
3.最后执行location中的rewrite
  • 配置
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;
    }
}
  • rewrite的环境变量

$server_name

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

server {
        listen 80;
        server_name linux.test.com;
        rewrite ^(.*)$ https://$server_name$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;
}
  • $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 / {...}
}
  • rewrite开启日志

[root@web01 ~]# vim /etc/nginx/nginx.conf
... ...
error_log  /var/log/nginx/error.log notice;
... ...
http {
    ... ...
    rewrite_log on;
    ... ...
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值