use*_*618 9 nginx alias redirect mime-type json
我试图弄清楚如何在 nginx 中处理对特定目录发出的所有请求并返回一个没有重定向的 json 字符串。
例子:
curl -i http://example.com/api/call1/
Run Code Online (Sandbox Code Playgroud)
预期结果:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Type: application/json
Date: Fri, 13 Apr 2012 23:48:21 GMT
Last-Modified: Fri, 13 Apr 2012 22:58:56 GMT
Server: nginx
X-UA-Compatible: IE=Edge,chrome=1
Content-Length: 38
Connection: keep-alive
{"logout": true}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我在 nginx conf 中的内容:
location ~ ^/api/(.*)$ {
index /api_logout.json;
alias /path/to/file/api_logout.json;
types { }
default_type "application/json; charset=utf-8";
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试发出请求时, Content-Type 不会坚持:
$ curl -i http://example.com/api/call1/
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Type: application/octet-stream
Date: Fri, 13 Apr 2012 23:48:21 GMT
Last-Modified: Fri, 13 Apr 2012 22:58:56 GMT
Server: nginx
X-UA-Compatible: IE=Edge,chrome=1
Content-Length: 38
Connection: keep-alive
{"logout": true}
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?我怎样才能让 application/json 类型坚持下去?
编辑:解决方案!
我发现你可以在 return 语句中发送手动字符串,所以我这样做而不是使用别名!
我使用的最终代码:
location /api {
types { }
default_type "application/json";
return 200 "{\"logout\" : true"}";
}
Run Code Online (Sandbox Code Playgroud)
您可以使用重写来获得包罗万象的行为。
location /logout.json {
alias /tmp/logout.json;
types {
application/json json;
}
}
rewrite ^/api/.* /logout.json;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3336 次 |
最近记录: |