Can I alias all directory requests to a single file in nginx?

Posted by user749618 on Server Fault See other posts from Server Fault or by user749618
Published on 2012-04-13T23:53:46Z Indexed on 2012/04/14 5:31 UTC
Read the original article Hit count: 498

Filed under:
|
|
|
|

I'm trying to figure out how to take all requests made to a particular directory and return a json string without a redirect, in nginx.

Example:

curl -i http://example.com/api/call1/

Expected result:

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}

Here's what I have so far in my nginx conf:

location ~ ^/api/(.*)$ {
    index /api_logout.json;
    alias /path/to/file/api_logout.json;
    types { }
    default_type "application/json; charset=utf-8";
    break;
}

However, when I try to make the request the Content-Type doesn't stick:

$ 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}

Is there a better way to do this? How can I get the application/json type to stick?

© Server Fault or respective owner

Related posts about nginx

Related posts about redirect