1

I'd like nginx to rewrite any http://api.example.org/xyz requests to actually call example.org/api/xyz behind the scenes. The tricky part too is that I'm using CodeIgniter so rather than load a php script at /xyz/index.php I'll need it to reference CodeIgniter's /index.php with the XYZ controller. Right now I have it set up like this:

if ($host = "api.example.org") {
    rewrite ^(.*)$ http://example.org/api$1 permanent;
}

...but that causes a noticeable redirection to occur in the browser. I want the rewrite to be seamless/occur behind the scenes. So I tried:

if ($host = "api.example.org") {
    rewrite ^(.*)$ /api$1 last;
}

But that gives me a "No input file specified" error. :( Has anyone tried rewriting a subdomain into a codeigniter controller on nginx before?

Thanks!

taber
  • 131
  • 6
  • Can't you just have your code serve the API on `api.example.org`? You wouldn't need _any_ redirects in this case, and it would be much cleaner. – Michael Hampton Sep 01 '13 at 19:40
  • Thanks for the suggestion! I could but I think then I'd need to finagle a new CodeIgniter instance or hook it to the other one somehow. It shares the config, libraries, etc. as the main site so I wanted to keep them together. – taber Sep 01 '13 at 21:01

1 Answers1

1

I spent all day trying to figure this out and it ended up being really simple, ugh. What I ended up doing was:

location ~* \.php$ {
    ...
    if ($host ~ ^api\.) {
        set $api "/api";
    }
    fastcgi_param REQUEST_URI $api$request_uri;
    ...
}

Woo! If anyone sees anything potentially wrong about this please lemme know! Seems to be working though.

taber
  • 131
  • 6