Score:1

NGINX try_files, not loading language specific index.html

in flag

We are wanting it so that when the website is accessed, we serve the appropriate index.html for the language in the Accept-Language request header. The problem is that our configuration does not seem to be working.

The configuration we have in NGINX (the two add_header lines are for debugging):

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

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

    location / {
        root        /usr/share/nginx/html;
        add_header x-eval-lang $language_suffix;
        if (-f /index.$language_suffix.html) {
          add_header x-xxx '33';
        }
        try_files   $uri $uri/ /index.$language_suffix.html /index.html;
    }

In the folder we have:

  index.html
  index.en.html
  index.fr.html

The expected behaviour is to serve up index.en.html or index.fr.html as appropriate, but is instead serving up index.html. We aren't getting the x-xxx header added, suggesting I am not constructing the filename properly.

This is running in a Docker container.

So testing in a non-Dockerised NGINX server shows this should work, suggesting that it is not seeing the localised files for some reason.

NGINX version is 1.22.1

Richard Smith avatar
jp flag
Don't use `if` inside a `location` for debugging. It will [not work as you expect](https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/).
Richard Smith avatar
jp flag
Your `try_files` statement tests `$uri/` *before* your language specific term, which may return an `index.html` file for URIs which end with a `/`.
Score:0
in flag

After a lot of experimenting, simply providing a modified index directive resolved the issue (based on docs on static content):

    # Alternative recipe for getting header language, taken from
    # https://www.nginx.com/resources/wiki/modules/accept_language/#alternative
    set $first_language $http_accept_language;
    if ($http_accept_language ~* '^(.+?),') {
        set $first_language $1;
    }

    set $language_suffix 'en';
    # case sensitive for language portion, so en-FR, for example,
    # will not be treated as French
    if ($first_language ~ 'fr') {
        set $language_suffix 'fr';
    }

    location / {
        root        /usr/share/nginx/html;
        index index.$language_suffix.html index.html;
        try_files   $uri $uri/ /index.html;
    }

So if $language_suffix is 'fr' then the index directive resolves to:

index index.fr.html index.html

BTW the 'Docker' container difference was a red-herring, since further testing showed the NGINX could serve the file and the non-Dockerised version wasn't quite configured in the same way.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.