我试图弄清楚如何在 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 …Run Code Online (Sandbox Code Playgroud)