关于Nginx的return关键字小技巧

Nginx的return关键字属于HttpRewriteModule模块:

语法:return http状态码
默认值:无
上下文:server,location,if

该指令将结束执行直接返回http状态码到客户端.

支持的http状态码:200, 204, 400, 402-406, 408, 410, 411, 413, 416 , 500-504,还有非标准的444状态码.

使用方法:

#不符合规则的返回403禁止访问

location /download/ {
    rewrite  ^(/download/.*)/media/(.*)\..*$  $1/mp3/$2.mp3  break;
    return   403;
}

小技巧

这些小技巧都是wiki里没有介绍的,而系统却是支持的。

如下配置文件:

server {
    server_name  test.liguangming.com;
    listen  80;
    location / {
        add_header Content-Type "text/plain;charset=utf-8";
        return 200 "Your IP Address:$remote_addr";
    }
}

执行请求:

curl -i http://test.liguangming.com

返回内容如下:

HTTP/1.1 200 OK
Server: nginx/1.0.13
Date: Thu, 10 May 2012 10:01:15 GMT
Content-Type: application/octet-stream
Content-Length: 30
Connection: keep-alive
Content-Type: text/plain;charset=utf-8

Your IP Address:123.128.217.19

好玩吧,还有呢,比如如下的配置文件:

server {
    server_name  test.liguangming.com;
    listen  80;
    location / {
        return http://liguangming.com/;
    }
}

执行请求:

curl -i http://test.liguangming.com

返回内容如下:

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.0.13
Date: Thu, 10 May 2012 10:06:58 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: http://liguangming.com/
Content-Type: text/plain;charset=utf-8

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

是个302转向,为什么会这样呢?在nginx的源代码里找到src/http/modules/ngx_http_rewrite_module.c文件, 找到return关键字的解析配置:

{ ngx_string("return"),
  NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                   |NGX_CONF_TAKE12,
  ngx_http_rewrite_return,
  NGX_HTTP_LOC_CONF_OFFSET,
  0,
  NULL },

看到NGX_CONF_TAKE12,原来return允许接受一个或者两个参数啊.

再接着往下看找到ngx_http_rewrite_return函数:

static char *
ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_rewrite_loc_conf_t  *lcf = conf;

    u_char                            *p;
    ngx_str_t                         *value, *v;
    ngx_http_script_return_code_t     *ret;
    ngx_http_compile_complex_value_t   ccv;

    ret = ngx_http_script_start_code(cf->pool, &lcf->codes,
                                     sizeof(ngx_http_script_return_code_t));
    if (ret == NULL) {
        return NGX_CONF_ERROR;
    }

    value = cf->args->elts;

    ngx_memzero(ret, sizeof(ngx_http_script_return_code_t));

    ret->code = ngx_http_script_return_code;

    p = value[1].data;

    ret->status = ngx_atoi(p, value[1].len);

    if (ret->status == (uintptr_t) NGX_ERROR) {

        if (cf->args->nelts == 2
            && (ngx_strncmp(p, "http://", sizeof("http://") - 1) == 0
                || ngx_strncmp(p, "https://", sizeof("https://") - 1) == 0
                || ngx_strncmp(p, "$scheme", sizeof("$scheme") - 1) == 0))
        {
            ret->status = NGX_HTTP_MOVED_TEMPORARILY;
            v = &value[1];

        } else {
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                               "invalid return code "%V"", &value[1]);
            return NGX_CONF_ERROR;
        }

    } else {

        if (cf->args->nelts == 2) {
            return NGX_CONF_OK;
        }

        v = &value[2];
    }

    ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));

    ccv.cf = cf;
    ccv.value = v;
    ccv.complex_value = &ret->text;

    if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
        return NGX_CONF_ERROR;
    }

    return NGX_CONF_OK;
}

当一个参数的时候,并不一定要是状态码,如果是一个网址,以http,https,或者与请求相同的协议,就会返回一个302重定向. 相当于:

return 302 http://liguangming.com/;

第二个参数会作为内容返回,其实这不就是一个简单的原生的echo模块吗? 赶着回去吃饭,匆忙写成的,难免有疏漏,有时间再修改.

参考资源