rewrite
rewrite使用场景
1、地址跳转,用户访问www.drz.com这个URL是,将其定向至一个新的域名mobile.drz.com
2、协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。
4、搜索引擎,SEO优化依赖于url路径,好记的url便于支持搜索引擎录入
rewrite语法
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
server {
rewrite 规则 路径或者内容 flag类型;
rewrite (.*) http://www.baidu.com$1
}
rewrite #模块命令
regex #请求的链接(支持正则表达式)
replacement #跳转的链接
[flag] #标签
1.规则:字符串,也可以使用正则匹配url;
2.路径或者内容:表示匹配到规则后要跳转的路径或内容,如果前面规则里面有正则,则可以使用$1等获取值
3.flag类型:last break redirect permanent
rewrite的flag类型
flag类型 |
作用 |
last |
匹配完本条规则,停止匹配,重新请求server |
break |
匹配完本条规则,停止匹配,不再匹配后面的内容 |
redirect |
302跳转,临时重定向 |
permanent |
301跳转,永久重定向 |
last和 break的区别
[root@web01 conf.d]# vim rewrite.con
server {
listen 80;
server_name rw.linux.com;
root /code/rewrite;
location ~ ^/break {
rewrite ^/break /test/ break;
}
location ~ ^/last {
rewrite ^/last /test/ last;
}
location /test/ {
default_type application/json;
return 200 "ok";
}
}
break 只要匹配到规则,就回去本地路径目录中寻找请求的文件;
last 匹配到规则,跳转后没有内容,则带着跳转后的请求,重新的向server发起一次请求
break和last都可以继续执行后面的rewrite;
在location里面,如果配置break,则直接生效,停止后面匹配的location
break请求:
1.请求rw.linux.com/break;
2.首先,会去查找本地的/code/rewrite/test/index.html;
3.如果找到了,则返回/code/rewrite/test/index.html内容;
4.如果没有找到则返回404,找到目录却没有主页,则返回403;
last请求:
1.请求rw.linux.com/last;
2.首先,会去查找本地的/code/rewrite/test/index.html;
3.如果找到了,则返回/code/rewrite/test/index.html内容;
4.如果没找到,会带着新跳转的URI再向server发起一次请求,请求rw.linux.com/test;
5.如果匹配到新的location,则返回该location匹配的内容;
6.如果没有匹配到新的,则再返回404或403;
redirect和permanent的区别
[root@web01 conf.d]# cat rewrite.conf
server {
listen 80;
server_name rw.linux.com;
root /code/rewrite;
location /test {
rewrite ^(.*)$ http://www.baidu.com redirect;
#rewrite ^(.*)$ http://www.baidu.com permanent;
}
}
redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。
permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。
rewrite规则匹配实例
用户访问/abc/1.html
实际上真实访问的是/ccc/bbb/2.html
[root@web01 conf.d]# vim rw.conf
server {
listen 80;
server_name rw.linux.com;
root /code;
location ~ /abc {
rewrite ^(.*)$ /ccc/bbb/2.html redirect;
#rewrite /abc/1.html /ccc/bbb/2.html redirect;
}
}
#/abc/1.html /ccc/bbb/1.html
location ~ /abc {
rewrite /abc/(.*)\.html /ccc/bbb/$1.html redirect;
}
用户访问/2018/ccc/2.html
实际上真实访问的是 /2014/ccc/bbb/2.html
[root@web01 conf.d]# mkdir /code/2014/ccc/bbb/ -p
[root@web01 conf.d]# echo "2014-ccc-bbb-222" > /code/2014/ccc/bbb/2.html
server {
listen 80;
server_name rw.linux.com;
root /code;
location ~ /2018 {
rewrite /2018/ccc/2.html /2014/ccc/bbb/2.html redirect;
}
}
#/2018/ccc/bbb/2.html 跳转 /2014/ccc/bbb/2.html
location ~ /2018 {
rewrite /2018/(.*) /2014/$1 redirect;
}
用户访问/test
实际上真实访问的是www.baidu.com
server {
listen 80;
server_name rw.linux.com;
root /code;
location ~ /test {
rewrite (.*) https://www.baidu.com redirect;
}
}
用户访问 couese-11-22-33.html 实际上真实访问的是 /course/11/22/33/course_33.html
[root@web01 conf.d]# mkdir /code/course/11/22/33/ -p
[root@web01 conf.d]# echo "course-111-222-333" > /code/course/11/22/33/course_33.html
[root@web01 conf.d]# vim rw.conf
server {
listen 80;
server_name rw.linux.com;
root /code;
location / {
#rewrite ^/course-11-22-33.html /course/11/22/33/course_33.html redirect;
rewrite ^/(.*)-(.*)-(.*)-(.*).html /$1/$2/$3/$4/$1_$4.html redirect;
}
}
将http请求跳转到https
server {
listen 80;
server_name www.mumusir.com;
#rewrite (.*) https://www.mumusir.com redirect;
return 302 https://www.mumusir.com;
}
http://www.mumusir.com --> https://www.mumusir.com
server {
listen 443;
server_name www.mumusir.com;
ssl...... *.key;
ssl..... *.crt;
}
ewrite伪静态实例
搭建discuz论坛
#创建站点目录
[root@web01 ~]# mkdir /code/discuz
[root@web01 code]# rz Discuz_X3.3_SC_GBK.zip
[root@web01 code]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/
#授权站点目录
[root@web01 code]# chown -R www.www /code/discuz/
#配置discuz论坛的配置文件
[root@web01 conf.d]# cp blog.conf discuz.linux.com.conf
server {
listen 80;
server_name discuz.linux.com;
location / {
root /code/discuz/upload;
index index.php;
}
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;
}
}
#配置hosts访问
#创建数据库
[root@db02 ~]# mysql -u root -p123456
。。。。。。
MariaDB [(none)]> create database discuz charset utf8;
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.04 sec)
MariaDB [(none)]>
配置hosts,访问论坛,发表帖子
#查看帖子地址http://discuz.linux.com/forum.php?mod=viewthread&tid=1&extra=
配置rewrite伪静态
[root@web01 conf.d]# vim /etc/nginx/conf.d/discuz.linux.com.conf
server {
listen 80;
server_name discuz.linux.com;
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$ {
root /code/discuz/upload;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]# systemctl restart nginx
http://discuz.linux.com/thread-1-1-1.html
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
discuz.linux.com/thread-1-1-1.html
discuz.linux.com/forum.php?mod=viewthread&tid=1&extra=page%3D1&page=1
ewrite规则补充
rewrite匹配的优先级
1.先执行server模块的rewrite指令
2.其次执行location匹配规则
3.最后执行location里面的rewrite
server {
listen 80;
server_name rw.linux.com;
#rewrite ^(.*)$ http://www.jingdong.com;
location /test {
rewrite ^(.*)$ http://www.mumusir.com;
}
location =/ {
rewrite ^(.*)$ http://www.baidu.com;
}
}
rewrite全局变量
$server_name #当前用户请求的域名
server {
listen 80;
server_name rw.linux.com;
root /code;
rewrite ^(.*)$ https://$server_name;
}
$request_filename #请求的文件路径和名字(带着网站站点目录的路径和文件 /code/images/1.jpg)
$request_uri #请求的文件路径和名字(不带网站站点目录的路径和文件 /images/1.jpg)
server {
listen 80;
server_name rw.linux.com;
root /code;
rewrite ^(.*)$ https://$server_name$request_uri;
}
#很久很久以前,网站优化
server {
listen 80;
server_name www.baidu.com baidu.com;
root /code;
if ($http_host = baidu.com) {
rewrite (.*) http://www.baidu.com;
}
}
#实际写法
server {
listen 80;
server_name baidu.com;
rewrite (.*) http://www.baidu.com;
}
server {
listen 80;
server_name www.baidu.com;
root /code;
}
rewrite可以开启日志
#NGINX主配置文件,错误日志级别改成notice
error_log /var/log/nginx/error.log notice;
#http层开启rewrite日志
rewrite_log on;
uri 和 url的区别
http://192.168.15.7/video-sousuo-117877-18-0-0-0-1-all-complex-0-0-0-0-0-0.html
url : http://192.168.15.7/video-sousuo-117877-18-0-0-0-1-all-complex-0-0-0-0-0-0.html
uri : /video-sousuo-117877-18-0-0-0-1-all-complex-0-0-0-0-0-0.html