rewrite
Rewrite使用场景
1、地址跳转,用户访问blog.linux.com/test 这个URL时,将其定向至http://www.baidu.com
server {
listen 80;
server_name blog.linux.com;
location /test {
rewrite ^(.*)$ http://www.baidu.com;
}
}
2、协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
server {
listen 80;
server_name www.mumusir.com mumusir.com;
rewrite ^(.*)$ https://www.mumusir.com;
#return 302 https://$server_name$request_uri;
}
3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。
4、搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入
rewrite配置语法
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
#rewrite调用 访问的地址(支持正则) 跳转的地址 标签
rewrite regex replacement [flag];
flag
rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示:
| flag | 作用 |
|---|---|
| last | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
| break | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
| redirect | 返回302临时重定向,地址栏会显示跳转后的地址 |
| permanent | 返回301永久重定向,地址栏会显示跳转后的地址 |
1.last与break区别对比示例
[root@web01 ~]# vim /etc/nginx/conf.d/rw.linux.com.conf
server {
listen 80;
server_name rw.linux.com;
root /code/rw;
location ~ ^/break {
rewrite ^/break /test/ break;
}
location ~ ^/last {
rewrite ^/last /test/ last;
}
location /test/ {
default_type application/json;
return 200 "ok";
}
}
2.last与break区别
break 只要匹配到规则,则会去本地配置路径的目录中寻找请求的文件;
而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。
break请求:
1、请求rewrite.drz.com/break
2、匹配到相应的location,被rewrite重写
3、会去查找本地的/code/rw/test/index.html;
4、如果找到了,则返回/code/test/index.html的内容;
5、如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403
last请求:
1、请求rewrite.drz.com/last
2、匹配到相应的location,被rewrite重写
3、会去查找本地的/code/test/index.html;
4、如果找到了,则返回/code/test/index.html的内容;
5、如果没找到,会对当前server重新的发起一次请求,rewrite.drz.com/test/
6、如果有location匹配上,则直接返回该location的内容。
7、如果也没有location匹配,再返回404;
3.redirect和permanent的区别示例
[root@web01 conf.d]# cat rewrite.conf
server {
listen 80;
server_name rewrite.drz.com;
root /code;
location /test {
rewrite ^(.*)$ http://www.driverzeng.com redirect;
#return 302 http://www.driverzeng.com;
#rewrite ^(.*)$ http://www.driverzeng.com permanent;
#return 301 http://www.driverzeng.com;
}
}
4.redirect和permanent的区别
redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。
permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。
rewrite 配置实例
0.为了方便排错,开启日志
[root@web01 ~]# vim /etc/nginx/nginx.conf
error_log /var/log/nginx/error.log notice;
http {
... ...
rewrite_log on;
... ...
}
1.实例一:用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html
#1.配置跳转后的文件
[root@web01 ~]# mkdir /code/rw/ccc/bbb -p
[root@web01 ~]# echo "ccc_bbb_222" > /code/rw/ccc/bbb/2.html
#配置nginx跳转
[root@web01 ~]# vim /etc/nginx/conf.d/rw.linux.com.conf
server {
listen 80;
server_name rw.linux.com;
root /code/rw;
location /abc {
rewrite ^/abc /ccc/bbb/2.html break;
}
}
#页面访问测试
http://rw.linux.com/abc/1.html
## rewrite 伪静态
### 1.搭建discuz
```bash
#上传代码
[root@web01 ~]# cd /code/
[root@web01 code]# rz Discuz_X3.3_SC_GBK.zip
#解压代码
[root@web01 code]# unzip Discuz_X3.3_SC_GBK.zip
#授权目录
[root@web01 ~]# chown -R www.www /code/
#配置站点配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/discuz.linux.com.conf
server {
listen 80;
server_name discuz.linux.com;
location / {
root /code/upload;
index index.php;
}
location ~ \.php$ {
root /code/upload;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 ~]# systemctl restart nginx
#创建数据库
[root@db01 ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> create database ultrax;
Query OK, 1 row affected (0.02 sec)
MariaDB [(none)]> grant all on 库名字.* to 用户@'主机地址' identified by '密码';
MariaDB [(none)]> grant all on *.* to root@'%' identified by '123456';
#配置host,访问页面配置
2.rewrite 配置伪静态
[root@web01 ~]# cat /etc/nginx/conf.d/discuz.linux.com.conf
server {
listen 80;
server_name discuz.linux.com;
location / {
root /code/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/upload;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
rewrite 扩展
1.rewrite匹配优先级
1.先执行server块的rewrite指令
2.其次执行location匹配规则
3.最后执行location中if语句的rewrite
server {
listen 80;
server_name rew.linux.com;
rewrite (.*) http://www.baidu.com;
location / {
rewrite (.*) http://www.jd.com;
if ($http_user_agent ~* Windows) {
rewrite (.*) http://www.taobao.com;
}
}
}
2.Rewrite与Nginx全局变量
Rewrite在匹配过程中,会用到一些Nginx全局变量
$server_name #当前用户请求的域名
server {
listen 80;
server_name test.drz.com;
rewrite ^(.*)$ https://$server_name$1;
}
$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)
$request_uri 当前请求的文件路径(不带网站的主目录/inages/test.jpg)
#大多数用于http协议转gttps协议
server {
listen 80;
server_name php.drz.com;
return 302 https://$server_name$request_uri;
}
$scheme 用的协议,比如http或者https
3.rewrite 规范写法
server {
listen 80;
server_name www.drz.com drz.com;
if ($http_host = drz.com){
rewrite (.*) http://www.drz.com$1;
}
}
#推荐书写格式
server {
listen 80;
server_name drz.com;
rewrite ^ http://www.drz.com$request_uri;
}
server {
listen 80;
server_name www.drz.com;
}