我想弄清楚如何将所有请求发送到一个特定的目录,并在nginx中返回一个没有redirect的jsonstring。
例:
curl -i http://example.com/api/call1/
预期结果:
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}
以下是我在我的nginx conf中的结果:
location ~ ^/api/(.*)$ { index /api_logout.json; alias /path/to/file/api_logout.json; types { } default_type "application/json; charset=utf-8"; break; }
但是,当我尝试提出请求时,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}
有没有更好的方法来做到这一点? 我怎样才能让应用程序/ jsontypes粘贴?
编辑:解决scheme!
我想你可以发送返回语句中的手动string,所以我做了,而不是使用别名!
我使用的最终代码:
location /api { types { } default_type "application/json"; return 200 "{\"logout\" : true"}"; }
您可以使用重写来获得catchall行为。
location /logout.json { alias /tmp/logout.json; types { application/json json; } } rewrite ^/api/.* /logout.json;