Nginx Rewrite和Return

[TOC]

Rewrite配置(一)

nginx的rewrite配置是nginx配置中比较核心的部分,rewrite可以实现域名跳转(重定向)、URL重写(伪静态)、动静分离(跳转域名,并接入CDN实现加速)。rewrite依赖pcre库,用到的模块是ngx_http_rewrite_module。

rewrite相关指令

if指令

格式: if (条件判断) { 具体的rewrite规则 }

  • 条件举例

    1. 条件判断语句有nginx内置变量、逻辑判断符号和目标字符串三部分组成。
    2. 其中,内置变量是nginx固定的非自定义的变量,如$request_method、$request_uri等。
    3. 逻辑判断符号有 =、!=、~、~、!~、!~
    4. !表示取反,~为匹配符号,它右侧为正则表达式,区分大小写,而~*为不区分大小写匹配。
    5. 目标字符串可以是正则表达式,通常不用加引号,但表达式中有特殊符号时,比如空格、花括号、分号等,需要用单引号引起来。
  • 示例1:

    1
    2
    3
    4
    if ($request_method = POST)     # 当请求的方法为POST时,直接返回405状态码。if中支持用return指令。
    {
    return 405;
    }
  • 示例2:

    1
    2
    3
    4
    if ($http_user_agent ~ MSIE )    # 当user_agent带有MSIE(IE浏览器)字符的请求,直接返回403状态码。
    {
    return 403;
    }

如果想同时限制多个user_agent,还可以写成这样:

1
2
3
4
if ($http_user_agent ~ "MSIE|firefox|spider")
{
return 403;
}

  • 示例3:

    1
    2
    3
    4
    if (!-f $request_filename)      # 当请求的文件不存在时,将会执行下面的rewrite规则。
    {
    rewrite 语句;
    }
  • 示例4:

1
2
3
4
if ($request_uri ~* 'gid=\d{9,12}/')  # \d表示数字,{9,12}表示数字出现的次数是9到12次,比如gid=123456789是符合条件的,就会执行下面的rewrite规则。
{
rewrite 语句;
}

break和last指令

两个指令用法相同,但含义不同,需要放到rewrite规则的末尾,用来控制重写后的链接是否继续被nginx配置执行(主要是rewrite、return指令)。

  • 示例1:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    vim /usr/local/nginx/conf/vhost/www.1.com.conf

    server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on; #打开rewrite日志,在error.log中
    rewrite /1.html /2.html;
    rewrite /2.html /3.html;
    }

访问测试:

1
2
curl -x127.0.0.1:80 www.1.com/1.html
333333 # 说明已经从1.html跳转到3.html,实际访问到的是3.html。

查看日志:

1
2
3
4
2020/02/18 15:58:35 [notice] 4229#0: *56 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 15:58:35 [notice] 4229#0: *56 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 15:58:35 [notice] 4229#0: *56 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 15:58:35 [notice] 4229#0: *56 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"

  • 示例2:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    vim /usr/local/nginx/conf/vhost/www.1.com.conf

    server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;
    rewrite /1.html /2.html break;
    rewrite /2.html /3.html;
    }

访问测试:

1
2
curl -x127.0.0.1:80 www.1.com/1.html  # 说明这一次是从1.html跳转到2.html,没有继续往下面跳转。
222222

查看日志:

1
2
2020/02/18 16:15:51 [notice] 4522#0: *60 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:15:51 [notice] 4522#0: *60 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"

  • 示例3:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;
    rewrite /1.html /2.html last;
    rewrite /2.html /3.html;
    }

访问测试:

1
2
curl -x127.0.0.1:80 www.1.com/1.html   # 说明这一次也是从1.html跳转到2.html,没有继续往下面跳转。在server部分配置break和last作用一致
222222

查看日志:

1
2
2020/02/18 16:15:51 [notice] 4522#0: *60 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:15:51 [notice] 4522#0: *60 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"

  • 示例4:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    vim /usr/local/nginx/conf/vhost/www.1.com.conf

    server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;

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

访问测试:

1
2
3
4
5
6
7
8
9
curl -x127.0.0.1:80 www.1.com/1.html

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

查看日志:

1
2
3
4
5
6
7
8
9
2020/02/18 16:23:04 [notice] 4564#0: *64 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:23:04 [notice] 4564#0: *64 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:23:04 [notice] 4564#0: *64 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:23:04 [notice] 4564#0: *64 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:23:04 [notice] 4564#0: *64 "/3.html" matches "/3.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:23:04 [notice] 4564#0: *64 rewritten data: "/b.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:23:04 [notice] 4564#0: *64 "/1.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:23:04 [notice] 4564#0: *64 "/2.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:23:04 [error] 4564#0: *64 open() "/data/wwwroot/www.1.com/b.html" failed (2: No such file or directory), client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"

首先匹配到1.html,1.html跳转到2.html;再匹配到2.html,2.html又跳转到3.html;接下来匹配到3.html,3.html跳转到b.html;b.html还会继续匹配,但没有匹配到,所以访问b.html,因为b.html不存在,所以返回404状态码。

  • 示例5:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    vim /usr/local/nginx/conf/vhost/www.1.com.conf

    server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;

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

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

    location /3.html {
    rewrite /3.html /b.html;
    }
    }

访问测试:

1
2
curl -x127.0.0.1:80 www.1.com/1.html
222222

查看日志:

1
2
2020/02/18 16:26:02 [notice] 4608#0: *1 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:26:02 [notice] 4608#0: *1 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"

可以看到,只rewrite一次,从1.html跳转到2.html就直接退出,后面的location部分也不再执行了。

  • 示例6:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    vim /usr/local/nginx/conf/vhost/www.1.com.conf

    server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;

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

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

    location /3.html {
    rewrite /3.html /b.html;
    }
    }

访问测试:

1
2
3
4
5
6
7
8
9
curl -x127.0.0.1:80 www.1.com/1.html

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

查看日志:

1
2
3
4
5
6
7
2020/02/18 16:27:02 [notice] 4614#0: *2 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:27:02 [notice] 4614#0: *2 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:27:02 [notice] 4614#0: *2 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:27:02 [notice] 4614#0: *2 rewritten data: "/a.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:27:02 [notice] 4614#0: *2 "/1.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:27:02 [notice] 4614#0: *2 "/2.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2020/02/18 16:27:02 [error] 4614#0: *2 open() "/data/wwwroot/www.1.com/a.html" failed (2: No such file or directory), client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"

先从1.html跳转到2.html,因为有last,所以本location段内的下面的内容不再执行,但是会继续执行下面的location段,最后匹配到2.html(因为比 / 更精准),从2.html跳转到a.html,因为a.html不存在,所以返回404状态码。

综上,我们可以得到结论:

  • 当rewrite规则在location{}外,break和last作用一样,遇到break或last后,其后续的rewrite/return语句不再执行。但后续有location{}的话,还会近一步执行location{}里面的语句,当然前提是请求必须要匹配该location。

  • 当rewrite规则在location{}里,遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行。

  • 当rewrite规则在location{}里,遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头开始执行所有规则,哪个匹配执行哪个。

Nginx rewrite配置(二)

格式:rewrite regex replacement [flag]

  • rewrite配置可以在server、location以及if配置段内生效

  • regex是用于匹配的正则表达式,其不会匹配到$host(域名)

  • replacement是目标跳转的uri,可以以http://或https://开头,也可以省略掉$host,直接写$request_uri部分(即请求链接)

  • flag,用来设置rewrite对uri的处理行为,其中有break、last、redirect、permanent。redirect和permanent的区别在于,redirect是临时重定向(302),而permanent是永久重定向(301)。对于用户访问来说,两者效果一致;但对于搜索引擎爬虫来说,使用301更利于SEO。所以,建议replacement是以http://或https://开头的,flag使用permanent

  • 示例1:

    1
    2
    3
    location / {
    rewrite /(.*) http://www.123.com/$1 permanent; # 说明:.* 为正则表达式,用()括起来,在后面的URL中可以调用它,第一次出现的()用$1调用,第二次出现的()用$2调用,依次类推。
    }
  • 示例2:

    1
    2
    3
    location / {
    rewrite /.* http://www.123.com$request_uri permanent; # 说明:在replacement中,支持变量,这里的$request_uri就是客户端请求的链接。
    }
  • 示例3:

    1
    2
    3
    4
    5
    6
    7
    server {
    listen 80;
    service_name www.123.com;
    root /tmp/123.com;
    index index.html;
    rewrite /(.*) /abc/$1 redirect; # 注明:本例中的rewrite规则有问题,会造成连续循环,而nginx有个最大50次限制,循环超过50次会失败。
    }

修改配置:

1
2
3
4
5
6
7
8
9
10
11
12
vim /usr/local/nginx/conf/vhost/www.2.com.conf 

server {
listen 80;
server_name www.2.com;
index index.html;
root /data/wwwroot/www.2.com;

location / {
rewrite /(.*) /abc/$1 redirect;
}
}

访问测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
curl -x127.0.0.1:80 www.2.com/1.html

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

curl -x127.0.0.1:80 www.2.com/1.html -L
curl: (47) Maximum (50) redirects followed

url -x127.0.0.1:80 www.2.com/1.html -I

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 22 Apr 2019 13:41:15 GMT
Content-Type: text/html
Content-Length: 154
Location: http://www.2.com/abc/1.html
Connection: keep-alive

curl -x127.0.0.1:80 www.2.com/abc/1.html -I

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 22 Apr 2019 13:41:27 GMT
Content-Type: text/html
Content-Length: 154
Location: http://www.2.com/abc/abc/1.html
Connection: keep-alive

可以看到,一直在循环 /abc ,直到循环超过50次。

  • 示例:4
    1
    2
    3
    4
    5
    6
    7
    server {
    listen 80;
    service_name www.123.com;
    root /tmp/123.com;
    index index.html;
    rewrite /(.*) /abc/$1 break;
    }

说明:在rewrite中使用break,可以避免循环。

  • 示例5:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    server {
    listen 80;
    service_name www.123.com;
    root /tmp/123.com;
    index index.html;
    if ($request_uri !~ '^/abc/')
    {
    rewrite /(.*) /abc/$1 redirect;
    }
    }

说明:增加一个条件判断,也可以避免循环。

修改配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
vim /usr/local/nginx/conf/vhost/www.2.com.conf 

server {
listen 80;
server_name www.2.com;
index index.html;
root /data/wwwroot/www.2.com;

if ($request_uri !~ '^/abc/')
{
rewrite /(.*) /abc/$1 redirect;
}
}

访问测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
curl -x127.0.0.1:80 www.2.com/1.html -I

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 22 Apr 2019 13:48:42 GMT
Content-Type: text/html
Content-Length: 154
Location: http://www.2.com/abc/1.html
Connection: keep-alive

# curl -x127.0.0.1:80 www.2.com/abc/1.html -I

HTTP/1.1 404 Not Found
Server: nginx
Date: Mon, 22 Apr 2019 13:50:21 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive

nginx全局变量

nginx常用全局变量:

变量 说明
$args 请求中的参数,如 www.123.com/1.php?a=1&b=2 的$args就是 a=1&b=2
$content_length http请求信息里的“Content-Length”
$content_type http请求信息里的“Content-Type”
$content_root nginx虚拟主机配置文件中的 root参数对应的值
$document_uri 当前请求中不包含指令的URI,如 www.123.com/1.php?a=1&b=2 的 $document_uri 就是1.php,不包含后面的参数
$host 主机头,即域名
$http_user_agent 客户端的详细信息,也就是浏览器的标识,用curl -A可以指定
$http_cookie 客户端的cookie信息
$limit_rate 如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,没有设置则显示为0
$remote_addr 客户端公网ip
$remote_port 客户端的port
$remote_user 如果nginx有配置认证,该变量代表客户端认证的用户名
$request_body_file 做反向代理时发给后端服务器的本地资源的名称
$request_method 请求资源的方式,GET/PUT/DELETE等
$request_filename 当前请求的资源文件的路径名称,相当于是 $document_root/$document_uri 的组合
$request_uri 请求的链接,包括 $document_uri 和 $args
$scheme 请求的协议,如ftp,http,https
$server_protocol 客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr 服务器IP地址
$server_name 服务器的主机名
$server_port 服务器的端口号
$uri 和$document_uri相同
$http_referer 客户端请求时的referer,通俗讲就是该请求是通过哪个链接跳过来的,用curl -e可以指定

return用法

return指令一般用于对请求的客户端直接返回响应状态码。在该作用域内return后面的所有nginx配置都是无效的。可以使用在server、location以及if配置中。

除了支持跟状态码,还可以跟字符串和url链接。

返回状态码

  • 示例1:
    1
    2
    3
    4
    5
    6
    server {
    listen 80;
    server_name www.1.com;
    return 403;
    rewrite /(.*) /abc/$1; #该行配置不会被执行
    }

. 表示所有,$1表示前面的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vim /usr/local/nginx/conf/vhost/default.conf

server {
listen 80 default_server;
return 403;
rewrite /(.*) /abc/$1;
}

# /usr/local/nginx/sbin/nginx -s reload

# curl -x127.0.0.1:80 e2rwejqw.com

<html>
<head><title>403 Forbidden</title></head> #返回403
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>
  • 示例2:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    server {
    ......
    if ( $request_uri ~ "\.htpasswd|\.bak" ) {
    return 405;
    rewrite /(.*) /aaa.txt; #该行配置不会被执行
    }

    #如果下面还有其他配置,会被执行
    ......
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
vim /usr/local/nginx/conf/vhost/www.1.com.conf

server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;

rewrite_log on;

if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return 405;
rewrite /(.*) /aaa.txt;
}
}

# /usr/local/nginx/sbin/nginx -s reload

# curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I

HTTP/1.1 405 Not Allowed #返回405
Server: nginx
Date: Mon, 11 Mar 2019 08:20:55 GMT
Content-Type: text/html
Content-Length: 166
Connection: keep-alive

返回字符串

  • 示例3:
    1
    2
    3
    4
    5
    server {
    listen 80;
    server_name www.1.com;
    return 200 "hello";
    }

如果想返回字符串,必须加上状态码,否则会报错。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
vim /usr/local/nginx/conf/vhost/www.1.com.conf

server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;

rewrite_log on;

if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return 200 "error";
rewrite /(.*) /aaa.txt;
}
}

# /usr/local/nginx/sbin/nginx -s reload

# curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 11 Mar 2019 08:26:58 GMT
Content-Type: application/octet-stream
Content-Length: 5
Connection: keep-alive

# curl -x127.0.0.1:80 www.1.com/123/.htpasswd
error

另外还可以支持JSON数据;支持写一个变量;支持html代码。

  • 场景实战
    背景:网站被黑,凡是在百度点击到本网站的请求,全部都跳转到一个赌博网站。

通过nginx解决:

1
2
3
4
5
server {
if ( $http_referer ~ 'baidu.com' ) {
return 200 "<html><script>window.location.href='//$host$request_uri';</script></html>";
}
}

如果写成:return http://$host$reauest_uri;,这在浏览器中会提示“重定向的次数过多”。

测试:

1
2
3
4
5
6
7
8
9
10
11
12
curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 11 Mar 2019 08:54:17 GMT
Content-Type: application/octet-stream
Content-Length: 79
Connection: keep-alive

curl -x127.0.0.1:80 www.1.com/123/.htpasswd

<html><script>window.location.href='//www.1.com/123/.htpasswd';</script></html>

返回url

  • 示例4:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
server {
listen 80;
server_name www.1.com;
return http://www.baidu.com; #注意:return后面的url必须是以http://或者https://开头的。
rewrite /(.*) /abc/$1; #该行配置不会被执行
}

vim /usr/local/nginx/conf/vhost/www.1.com.conf

server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;

rewrite_log on;

if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return http://www.baidu.com;
rewrite /(.*) /abc/$1;
}
}

/usr/local/nginx/sbin/nginx -s reload

curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 11 Mar 2019 08:44:07 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://www.baidu.com #临时重定向到www.baidu.com

url前面也可以加状态码,但只能是301或302,如果是200,这url会变成字符串返回。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
vim /usr/local/nginx/conf/vhost/www.1.com.conf

server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;

rewrite_log on;

if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return 200 http://www.baidu.com;
rewrite /(.*) /abc/$1;
}
}

# /usr/local/nginx/sbin/nginx -s reload

# curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 11 Mar 2019 09:02:15 GMT
Content-Type: application/octet-stream
Content-Length: 20
Connection: keep-alive

# curl -x127.0.0.1:80 www.1.com/123/.htpasswd
http://www.baidu.com