1.Rewrite概述

Rewrite主要实现url地址重写, 以及地址重定向,就是将用户请求web服务器的地址重新定向到其他URL的过程

2.Rewrite基本概述

地址跳转,用户访问www.linux.com这个URL是,将其定向至一个新的域名www.baidu.com

协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式

伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性

搜索引擎,SEO优化依赖于url路径,好记的url便于搜索引擎录入

3.Rewrite作用

URL Rewrite即URL重写,就是把传入Web的请求重定向到其他URL的过程。URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。比如http://www.123.com/news/index.asp?id=123 使用UrlRewrite转换后可以显示为http://www.123.com/news/123.html。

4.什么是URL

URL 代表着是统一资源定位符(Uniform Resource Locator)。URL 无非就是一个给定的独特资源在 Web 上的地址。理论上说,每个有效的 URL 都指向一个唯一的资源。这个资源可以是一个 HTML 页面,一个 CSS 文档,一幅图像,等等。而在实际中,也有一些例外,最常见的情况就是一个 URL 指向了不存在的或是被移动过的资源。由于通过 URL 呈现的资源和 URL 本身由 Web 服务器处理,因此 web 服务器的拥有者需要认真地维护资源以及与它关联的URL

5.rewrite模块

if

Syntax if (condition) { … }
Default ———
Context server, location

if () {
满足条件后执行指令
}

if 常用的条件及格式

条件 解释 取反
= 精确匹配,一模一样 等于 !=
~ 过滤,支持正则,区分大小写 !~
~* 过滤,支持正则,不区分大小写 !~*
-f 文件存在 !-f
-d 目录存在 !-d
-e 文件或目录存在 !-e
-x 文件有执行权限 !-x

nginx内置变量

$http_user_agent 客户端浏览器
$request_uri 用户请求的uri(包含参数)
$host 用户访问Host内
$remote_addr 用户ip地址
$args 只取出请求行里面的参数部分

set

Syntax set $variable value
Default
Context server, location, if

return

Syntax return code [text];
return code URL;
return URL;
Default
Context server, location, if

return code [text]:状态码 内容 文本
return code URL:状态码 定向新的url
return URL:定向新的url

rewrite跳转功能

Syntax rewrite regex replacement [flag]
rewrite 正则   替换成什么   [标记];
Default
Context server, location, if

rewrite flag标记

break 停止处理后续rewrite指令集,不会跳出location作用域,不再进行重新查找,终止匹配,URL地址不变
last 停止处理当前的ngx_http_rewrite_module指示并开始搜索与改变的URI匹配的新位置
redirect 返回带有302代码的临时重定向;如果替换字符串不是以”http://”, “https://“,或者”$scheme”
permanent 返回带有301代码的永久重定向

rewrite日志

Syntax rewrite_log on | off
Default rewrite_log off
Context http, server, location, if

nginx内置变量

$http_user_agent 客户端浏览器
$request_uri 用户请求的uri(包含参数)
$host 用户访问Host内
$remote_addr 用户ip地址
$args 只取出请求行里面的参数部分
$http_name name是http请求报文中的内容

 

6.案例

案例1:用户的客户端如果是 包含spider 或者bot 的(不区分大小写),则显示403(if)

spider:蜘蛛
bot:机器人

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/rewrite.dmxsp.com.conf
server {
listen 80;
server_name rewrite.dmxsp.com;
charset utf8;
root /code/rewrite;
index index.html;
default_type text/html;
if ( $http_user_agent ~* “spider|bot” ) {
return 200 “滚出去!!!!!!\n”;
}
}

charset utf8:字符集,如不添加页面中文乱码

default_type text/html:定义响应的默认MIME类型。文件扩展名到MIME类型的映射可以用类型指令

[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 reload nginx.service

[root@web01 ~]$ mkdir /code/rewrite
[root@web01 ~]$ echo rewrite.dmxsp.com >/code/rewrite/index.html

命令行测试

[root@web01 ~]$ curl -v -H Host:rewrite.dmxsp.com 10.0.0.45
* About to connect() to 10.0.0.45 port 80 (#0)
* Trying 10.0.0.45…
* Connected to 10.0.0.45 (10.0.0.45) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Accept: */*
> Host:rewrite.dmxsp.com
>
< HTTP/1.1 200 OK
< Server: nginx/1.20.1
< Date: Sat, 15 Oct 2022 02:50:52 GMT
< Content-Type: text/html
< Content-Length: 18
< Last-Modified: Tue, 11 Oct 2022 09:22:21 GMT
< Connection: keep-alive
< ETag: “634535cd-12”
< Accept-Ranges: bytes
<
rewrite.dmxsp.com
* Connection #0 to host 10.0.0.45 left intact

[root@web01 ~]$ curl -v -A baiduspider -H Host:rewrite.dmxsp.com 10.0.0.45
* About to connect() to 10.0.0.45 port 80 (#0)
* Trying 10.0.0.45…
* Connected to 10.0.0.45 (10.0.0.45) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: baiduspider
> Accept: */*
> Host:rewrite.dmxsp.com
>
< HTTP/1.1 403 Forbidden
< Server: nginx/1.20.1
< Date: Sat, 15 Oct 2022 02:52:18 GMT
< Content-Type: application/octet-stream
< Content-Length: 15
< Connection: keep-alive
<
* Connection #0 to host 10.0.0.45 left intact
滚出去!!!!!!

页面测试

案例2:过滤Nginx请求中包含a1=3526的http请求到10.16.3.5的8080端口处理(if)

uri内容是固定

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/rewrite.dmxsp.com.conf
server {
listen 80;
server_name rewrite.dmxsp.com;
charset utf8;
root /code/rewrite;
index index.html;
default_type text/html;
location / {
if ( $request_uri ~* “a1=3526” ) {    ###一般会要求a1等于4位数或更多位数\d代表数字。
proxy_pass http://10.16.3.5:8080;
}
}
}

测试

[root@web01 ~]$ curl -H Host:rewrite.dmxsp.com 10.0.0.45?a1=3526

url请求不固定

a1=6666   a1=9999 a1=1234 4位数字匹配满足写法

if ( $request_uri ~* ‘a1=[0-9]{4}’ ) {
proxy_pass   http://10.16.3.5:8080;
}

[0-9]+ 匹配连续的数字 1 12 123 23135
[0-9]{4} 前面的内容出现4次 4位数字
[0-9]{4,8} 前面的内容出现4-8次 4位数字 -8位数字

#if ( $request_uri ~* ‘a1=[0-9]{4}’ ) {
if ( $request_uri ~* ‘a1=\d{4}’ ) {
proxy_pass   http://10.16.3.5:8080;
}

高级正则 perl正则
[0-9X]{18}  
\d 数字

案例3:用户访问不同的域名进入不同的界面(set)

用户请求 rewrite.dmxsp.com.cn   用户访问  /code/rewrite/cn/index.html dmxsp cn

用户请求 rewrite.dmxsp.com.jp   用户访问  /code/rewrite/jp/index.html  dmxsp jp

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/rewrite.dmxsp.com.conf
server {
listen 80;
server_name rewrite.dmxsp.com rewrite.dmxsp.com.cn rewrite.dmxsp.com.jp;
charset utf8;
root /code/rewrite;
index index.html;
default_type text/html;
if ($host ~ ‘\.cn$’ ) {
set $lang “cn”;
}
if ($host ~ ‘\.jp$’ ) {
set $lang “jp”;
}
rewrite ^/$ http://rewrite.dmxsp.com/$lang/ redirect;
}

命令行测试

[root@web01 ~]$ curl -H Host:rewrite.dmxsp.com.jp 10.0.0.45 -L
dmxsp jp  
[root@web01 ~]$ curl -H Host:rewrite.dmxsp.com.cn 10.0.0.45 -L
dmxsp cn

页面测试

案例4:return跳转

需求1: 如果用户使用ie浏览器访问rewrite.dmxsp.com 则返回值字符串. 请更换浏览器,使用Edge/Chrome/Firefox

 

 

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/rewrite.dmxsp.com.conf
server {
listen 80;
server_name rewrite.dmxsp.com;
charset utf8;
root /code/rewrite;
index index.html;
default_type text/html;
if ( $http_user_agent ~* “MSIE” ) {
return 200 “请更换浏览器,使用Edge/Chrome/Firefox/……”;
}
}

命令行测试

[root@web01 ~]$ curl -A MSIE -H Host:rewrite.dmxsp.com 10.0.0.45
请更换浏览器,使用Edge/Chrome/Firefox/……

需求2: 如果用户使用ie浏览器访问rewrite.dmxsp.com则返回500错误

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/rewrite.dmxsp.com.conf
server {
listen 80;
server_name rewrite.dmxsp.com;
charset utf8;
root /code/rewrite;
index index.html;
default_type text/html;
if ( $http_user_agent ~* “MSIE” ) {
return 500;
}
}

命令行测试

[root@web01 ~]$ curl -A MSIE -H Host:rewrite.dmxsp.com 10.0.0.45
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

需求3: 如果用户使用ie浏览器访问rewrite.dmxsp.com,则直接跳转到baidu.com

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/rewrite.dmxsp.com.conf
server {
listen 80;
server_name rewrite.dmxsp.com;
charset utf8;
root /code/rewrite;
index index.html;
default_type text/html;
if ( $http_user_agent ~* “MSIE|Chrome” ) {
return http://baidu.com;
}
}

测试

案例5:rewrite跳转 break vs last、redirect vs permanent

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
root /code/url;

location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /3.html;
}
location /3.html {
rewrite /3.html /a.html;
}
}

测试

[root@web01 ~]$ curl -H Host:url.dmxsp.com 10.0.0.45/1.html
a.html url

break

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
root /code/url;

location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /3.html;
}
location /3.html {
rewrite /3.html /a.html;
}
}

测试

[root@web01 ~]$ curl -H Host:url.dmxsp.com 10.0.0.45/1.html
2.html url

last

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
root /code/url;

location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /3.html;
}
location /3.html {
rewrite /3.html /a.html;
}
}

测试

[root@web01 ~]$ curl -H Host:url.dmxsp.com 10.0.0.45/1.html
a.html url

redirect 

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
root /code/url;

location / {
rewrite /1.html /2.html redirect;
rewrite /2.html /3.html redirect;
}
location /2.html {
rewrite /2.html /3.html redirect;
}
location /3.html {
rewrite /3.html /a.html redirect;
}
}

测试

permanent

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
root /code/url;

location / {
rewrite /1.html /2.html permanent;
rewrite /2.html /3.html permanent;
}
location /2.html {
rewrite /2.html /3.html permanent;
}
location /3.html {
rewrite /3.html /a.html permanent;
}
}

测试

案例6:rewrite案例

1.网站要有多国语言支持

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/rewrite.zhen.com.conf
server {
listen 80;
server_name rewrite.zhen.com;
charset utf8;
root /code/url;
location / {
if ($http_accept_language ~ “zh-CN|zh” ) {
set $language “zh”;
}
if ($http_accept_language ~ “en” ) {
set $language “en”;
}
rewrite ^/$ /$language/ redirect;
}
}

$http_accept_language:分析浏览器语言

配置

[root@web01 ~]$ mkdir -p /code/url/zh
[root@web01 ~]$ mkdir -p /code/url/en
[root@web01 ~]$ echo 你好,中国. >/code/url/zh/index.html
[root@web01 ~]$ echo FBI Warning >/code/url/en/index.html

测试

[root@web01 ~]$ curl -H Accept-Language:en -H Host:rewrite.zhen.com 10.0.0.45 -Lv

案例7:rewrite案例

用户通过手机设备访问url.dmxsp.com 跳转至url.dmxsp.com/m

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
charset utf8;
root /code/url;

location / {
if ( $http_user_agent ~* “android|ios” ) {
# rewrite ^/$ /m/ redirect;
# rewrite ^/(.*)$ /m/$1 redirect;
return 302 http://url.dmxsp.com/m$request_uri;
}
}
}

^/$:只能匹配 /  

(.*):反向引用/后向引用

$1:表示前面第1个括号里面的内容

测试

[root@web01 ~]$ curl -A ios -H Host:url.dmxsp.com 10.0.0.45/dmxsp.html -Lv

案例8:rewrite案例

用户通过手机设备访问url.dmxsp.com 跳转至m.dmxsp.org

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
charset utf8;
root /code/url;

location / {
if ( $http_user_agent ~* “android|ios” ) {
# rewrite ^/(.*)$ http://m.dmxsp.org redirect;
return 302 http://m.dmxsp.org$request_uri;
}
}
}

测试

[root@web01 ~]$ curl -A ios -H Host:url.dmxsp.com 10.0.0.45 -Lv

案例9:rewrite案例

用户通过http协议请求,能自动跳转至https协议

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
charset utf8;
root /code/url;

location / {
# rewrite ^/(.*)$ https://url.dmxsp.com/$1 redirect;
#rewrite ^/(.*)$ https://$http_host/$1 redirect;}
return 302 https://url.dmxsp.com$request_uri;
}
}

案例10:rewrite案例

当网站遇到403 404 502 等错误时,自动跳转至临时维护的静https://404.life

第一种

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
charset utf8;
root /code/url;
error_page 403 404 500 502 504 /error.html;

location / {
rewrite ^/$ http://url.dmxsp.com ;
}
}

第二种

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
charset utf8;
root /code/url;
error_page 404 /404.html;
error_page 403 @error;

location @error {
rewrite ^.* /403.html break;
}
}

案例11:rewrite案例

公司网站在停机维护时,指定的ip能够正常访问,其他的ip跳转到维护页

第一种

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
charset utf8;
root /code/url;
default_type text/html;
error_page 403 404 500 502 504 /error.html;

location / {
if ( $remote_addr != ‘10.0.0.1’ ) {
return 200 “网站维护中” ;
}
}
}

第二种

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.com.conf
server {
listen 80;
server_name url.dmxsp.com;
charset utf8;
root /code/url;
default_type text/html;
error_page 403 404 500 502 504 /error.html;

location / {
set $ip 0;
if ( $remote_addr != ‘10.0.0.1’ ) {
set $ip 1;
}
if ( $ip = 0 ) {
return 200 “网站维护中”;
}
}
}

假设ip是10.0.0.1

set $ip 0 ;  $ip内容是0
判断 如果$remote_addr是10.0.0.1 则 修改$ip内容为1;
判断 如果$ip内容是0 则提示网站维护.

假设ip是10.0.0.7

set $ip 0 ;  $ip内容是0
判断如果$remote_addr是10.0.0.1 则设置$ip内容为1 ,不是10.0.0.1 跳过这个判断
$ip 内容是0 ,显示 网站正在维护中.

案例12:rewrite案例

公司网站后台/admin,只准许公司的出口公网ip可以访问,其他的ip访问全部返回500,或直接跳转至首页

配置文件(举例)

第一种

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.org.conf
server {
listen 80;
server_name url.dmxsp.org;
root /code/url;
rewrite_log on;
charset utf8;
location / {
index index.html;
}
location /admin {
if ( $remote_addr != ‘10.0.0.1’) {
return http://blog.wordpress.com;
}
}
}

第二种

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.org.conf
server {
listen 80;
server_name url.dmxsp.org;
root /code/url;
rewrite_log on;
charset utf8;
location / {
index index.html;
}
location /admin {
set $ip 0;
if ( $remote_addr = ‘10.0.0.1’) {
set $ip 1;
}
if ( $ip = 0 ) {
return http://blog.wordpress.com;
}
}
}

案例13:rewrite案例

网站维护过程中,希望用户访问所有网站重定向至一个维护页面

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/url.dmxsp.org.conf
server {
listen 80;
server_name url.dmxsp.org;
root /code/url;
rewrite_log on;
charset utf8;
rewrite ^.* /404.html break;
location / {
index index.html;
}
}

作者 dmxsp

发表回复

您的电子邮箱地址不会被公开。