# 08·Nginx实现Rewrite重写
[TOC]
## Rewrite基本概述
Rewrite主要实现url地址重写,以及重定向,就是把传入`web`的请求重定向到其他`url`的过程。
----
1、地址跳转,用户访问www.lzy.com这个URL时,将其定向至一个新的域名mobile.lzy.com
2、协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态URL地址对外暴露过多的参数,提升更高的安全性。
4、搜索引擎,SEO优化依赖于url路径,好记的url便于支持搜索引擎录入
----
```bash
句法:Syntax: rewrite regex replacement [flag]
默认:Default: --
语境:Context: server,location,if
#用于切换维护页面场景
#rewrite ^(.*)$ /page/maintain.html break;
```
## Rewrite标记Flag
`rewrite`指令根据表达式来重定向`URL`,或者修改字符串,可以应用于`server,location,if`环境下,每行`rewrite`指令最后跟一个`flag`标记,支持的`flag`标记有如下表格所示:
| flag | 作用 |
| --------- | ------------------------------------------------ |
| last | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
| break | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
| redirect | 返回302临时重定向,地址栏会显示跳转后的地址 |
| permanent | 返回301永久重定向,地址栏会显示跳转后的地址 |
```
[root@web01 conf.d]# cat nginx.conf
server {
listen 80;
server_name test.oldboy.com;
root /code/test/;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
```
修改为break和last进行测试
----
```bash
[root@web01 conf.d]# cat rewrite.conf
server {
listen 80;
server_name rewrite.lzy.com;
root /code;
location /test {
rewrite ^(.*)$ http://www.oldboy.com redirect;
#rewrite ^(.*)$ http://www.oldboy.com permanent;
#return 301 http://www.oldboy.com;
#return 302 http://www.oldboy.com;
}
}
```
----
| redirect与permanent区别)实现https |


```bash
redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。
permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。
```
## Rewrite规则实践
在写rewrite规则之前,我们需要开启rewrite日志对规则的匹配进行调试。
```bash
[root@web01 code]# vim /etc/nginx/nginx.conf
/var/log/nginx/error.log notice;
http{
rewrite_log on;
}
```
----
用户访问`/abc/1.html`实际上真实访问的是`/ccc/bbb/2.html`
```bash
#http://www.lzy.com/abc/1.html ==> http://www.lzy.com/ccc/bbb/2.html
#1.准备真实访问路径
[root@web03 ~]# mkdir /code/ccc/bbb -p
[root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html
#2.Nginx跳转配置
[root@web03 ~]# cd /etc/nginx/conf.d/
[root@web03 conf.d]# cat ccbb.conf
server {
listen 80;
location / {
root /code;
index index.html;
}
location /abc {
rewrite (.*) /ccc/bbb/2.html redirect;
#return 302 /ccc/bbb/2.html;
}
}
#3.重启Nginx服务
[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 conf.d]# nginx -s reload
```
----
用户访问`/2018/ccc/2.html`实际上真实访问的是`/2014/ccc/bbb/2.html`
```bash
##http://www.lzy.com/2018/ccc/2.html ==> http://www.lzy.com/2014/ccc/bbb/2.html
#1.准备真是的访问路径
[root@web03 conf.c]# mkdir /code/2014/ccc/bbb -p
[root@web03 conf.c]# echo "2014_ccc_bbb_2" > /code/2014/ccc/bbb/2.html
#2.Nginx跳转配置
[root@web03 conf.d]# cat ccbb.conf
server {
listen 80;
location / {
root /code;
index index.html;
}
location /2018 {
rewrite ^/2018/(.*)$ /2014/$1 redirect;
}
}
#3.重启nginx服务
[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 conf.d]# nginx -s reload
```
----
用户访问/test实际上真实访问的是https://www.oldboy.com
```bash
#1.Nginx跳转配置
[root@web03 conf.d]# cat test.conf
server {
listen 80;
location /test {
rewrite (.*) https://www.oldboy.com redirect;
}
}
#2.重启nginx服务
[root@web03 conf.d]# nginx -s reload
```
----
用户访问`couese-11-22-33.html`实际上真实访问的是`/course/11/22/33/course_33.html`
```bash
#http://www.lzy.com/couese-11-22-33.html ==> http://www.lzy.com/course/11/22/33/course_33.html
#1.准备真实的访问路径
[root@web03 ~]# mkdir /code/course/11/22/33 -p
[root@web03 ~]# echo "curl docs.etiantian.org" > /code/course/11/22/33/course_33.html
#2.Nginx跳转配置
[root@web03 conf.d]# cat test.conf
server {
listen 80;
root /code;
index index.html;
location / {
#灵活配法
rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
#固定配法
#rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
}
}
#3.重启nginx服务
[root@web03 conf.d]# nginx -s reload
```
----
将`http`请求跳转到`https`
```bash
#Nginx跳转配置
server {
listen 80;
server_name www.oldboy.com;
rewrite ^(.*) https://$server_name$1 redirect;
#return 302 https://$server_name$request_uri;
}
server {
listen 443;
server_name www.oldboy.com;
ssl on;
}
```
错误页跳转
```
server {
listen 80;
root /code;
location /test {
rewrite (.*) https://www.oldboyedu.com redirect;
}
error_page 403 404 500 501 502 @error_test;
location @error_test {
rewrite ^(.*)$ /404.html break;
}
}
变量使用
案例1:需要在跳转后的请求行加上想要的参数&showoffline=1
server {
listen 80;
server_name test.oldboy.com;
# $args为Nginx内置变量请求行的参数
set $args "&showoffline=1";
location / {
root /code;
index index.html;
}
if ($remote_addr = 10.0.0.1 ){
rewrite (.*) http://test.oldboy.com$1;
}
}
案例2:网站维护,指定IP正常访问,其他IP跳转维护页面
server {
listen 80;
server_name test.oldboy.com;
root /code;
charset utf-8,gbk;
location / {
index index.html;
set $ip 0; # 设置变量为0
if ($remote_addr = "10.0.0.1"){
set $ip 1; # 如果来源IP为0.1则设置为1
}
if ($ip = 0){ # 判断如果变量为0 则跳转维护页面
rewirte ^(.*)$ /wh.html break;
}
}
}
Nginx内置参数
$args #这个变量等于请求行中的参数。
$content_length #请求头中的Content-length字段。
$content_type #请求头中的Content-Type字段。
$document_root #当前请求在root指令中指定的值。
$host #请求主机头字段,否则为服务器名称。
$http_user_agent #客户端agent信息
$http_cookie #客户端cookie信息
$limit_rate #这个变量可以限制连接速率。
$request_body_file #客户端请求主体信息的临时文件名。
$request_method #客户端请求的动作,通常为GET或POST。
$remote_addr #客户端的IP地址。
$remote_port #客户端的端口。
$remote_user #已经经过Auth Basic Module验证的用户名。
$request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。
$query_string #与$args相同。
$scheme #HTTP方法(如http,https)。
$server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr #服务器地址,在完成一次系统调用后可以确定这个值。
$server_name #服务器名称。
$server_port #请求到达服务器的端口号。
$request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri #不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri #与$uri相同。
$X-Forwarded-For:HTTP的请求端真实的IP,只有在通过了HTTP 代理或者负载均衡服务器时才会添加该项。标准格式如下:X-Forwarded-For: client1, proxy1, proxy2
```