How to serve specific static files with nginx?

Barnabas Szabolcs asked:

I have queries like /api/lang?lang=en which I want to serve with nginx as /server/i18n-angular/en.json. How can I do that?

I have the following directory structure:

/public/
/server/i18n-angular/en.json

I have the following configuration, but nginx says it is wrong to use index directive at that point.

server {
  root /public
  ...
  location /api/lang {
    if ($args ~* "?lang=(.+)") {
      set $language $1;
      index ../server/i18n-angular/$language.json;
    }
  }
} 

What directive should I use instead of index?

My answer:


I don’t see that you need a separate location at all. A simple rewrite should do.

For instance:

server {
    rewrite /api/lang /server/i18n-angular/$arg_lang.json last;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.