3

Assuming I have a file en/index.html and a file de/index.html I'd like to serve the de/index.html file to users who have German as their default language setup in their browser and to all others I'd like to serve the en/index.html file.

How can I do this within the means of the normal nginx configuration?

1

The easiest way to do this:

set $first_language $http_accept_language;
if ($http_accept_language ~* '^(.+?),') {
  set $first_language $1;
}

set $language_suffix 'en';
if ($first_language ~* 'de') {
  set $language_suffix 'de';
}

location / {
  try_files $uri/$language_suffix/index.html $uri $uri.html;
}
1

There's other way to do this but it has some drawbacks too:

map $http_accept_language $index_page {
    default /index.html;

    "~*^de" /index.de.html;
    "~*^fr" /index.fr.html;
}

Pros: no if's (if's are evil), less code.

Cons: map can only be used inside http { } block so regexp matching will happen for all sites, not just the current one.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.