语法:return [code| URL]; 作用域:server,location,if
停止处理并返回指定状态码(code)给客户端, return关键字属于httpRewriteModule模块
支持的http状态码:200, 204, 400, 402-406, 408, 410, 411, 413, 416 , 500-504,还有非标准的444状态码

返回JSON

 location ~* (request/update)$ {
     default_type application/json;
     return 200 '{"update":"no"}';
 }
 // 记得加default_type application/json ,不然浏览器打开时,是个文件。

返回状态码

#不符合规则的返回403禁止访问
location /download/ {
    rewrite  ^(/download/.*)/media/(.*)..*$  $1/mp3/$2.mp3  break;
    return   403;
}

使用变量

location / {
   add_header Content-Type "text/plain;charset=utf-8";
   return 200 "Your IP Address:$remote_addr";
}

返回URL

  location / {
    return "http://www.server110.com/nginx/201308/767.html";
  }
  # 302 跳转

return 与 rewrite

results matching ""

    No results matching ""