Nginx – How to serve specific static files with nginx

nginxstatic-files

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?

Best 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;