1

I have a localization folder that has a bunch of language (as JSON) files, and my webapp tries to read the browser's language to fetch the corresponding JSON file.

In certain scenarios, the corresponding language file does not exist, and nginx returns a 404 as it should. However, I'd like to return a temporary redirect to the default language file.

I've tried this, but it doesn't seem to work. What am I doing wrong?

    location /static/l10n {
            try_files $uri @missing_language;
    }

    location @missing_language {
            rewrite ^ /static/l10n/en-us.json break;
    }

EDIT 2: So I tried tweaking the try_files a bit like this.

    location /static/l10n {
            try_files $uri /static/l10n/en-us.json;
    }

But now I get a 500 error. The logs say *35 rewrite or internal redirection cycle while internally redirecting.

EDIT 3: My full nginx conf is here - https://gist.github.com/paambaati/9782e95b2e9af899b154

GPX
  • 111
  • 4
  • The error message means you're rewriting in a loop (the file you're rewriting to in the try-files example doesn't exist). [A reference](http://serverfault.com/a/505099/108287). – AD7six Feb 20 '15 at 15:26
  • Put **the config** in the question, not a link to a gist. – AD7six Feb 20 '15 at 15:51

1 Answers1

1

Use error_page

To use a given file as the 404 response - use error_page:

location /static/l10n {
    error_page 404 = /static/l10n/en-us.json;
}

Note the use of = to serve /static/l10n/en-us.json as a http 200.

AD7six
  • 2,920
  • 2
  • 21
  • 23
  • Hmm, wouldn't have guess that line would return a `200`. Now I tried this, but I get a 404, and checking the logs, I see this. `open() "/Users/myusername/Projects/myproject/static/static/l10n/en-gb.json" failed (2: No such file or directory)`. Note that `/static` appears twice on the path. – GPX Feb 20 '15 at 15:35
  • You should show the rest of your config, because I don't think nginx is going to be injecting "static/" anywhere without being told to. [Works for me](https://gist.github.com/AD7six/286478e3659444cc89bb). – AD7six Feb 20 '15 at 15:42
  • @AD7six You probably meant `=200` instead of `=` to change the return code. @GPX Change your root directive to `/Users/myusername/Projects/myproject` then. – Xavier Lucas Feb 20 '15 at 15:43
  • @XavierLucas I did not, the response code will be whatever `/static/l10n/en-us.json` returns - which if it exists will be a 200, and if it doesn't (as it certainly seems to be the case) it'll still be a 404. – AD7six Feb 20 '15 at 15:46