Skip to content

accept-language: NGINX Accept-Language module

Debian/Ubuntu installation

These docs apply to the APT package nginx-module-accept-language provided by the GetPageSpeed Extras repository.

  1. Configure the APT repository as described in APT repository setup.
  2. Install the module:
sudo apt-get update
sudo apt-get install nginx-module-accept-language
Show suites and architectures
| Distro   | Suite             | Component   | Architectures   |
|----------|-------------------|-------------|-----------------|
| debian   | bookworm          | main        | amd64, arm64    |
| debian   | bookworm-mainline | main        | amd64, arm64    |
| debian   | trixie            | main        | amd64, arm64    |
| debian   | trixie-mainline   | main        | amd64, arm64    |
| ubuntu   | focal             | main        | amd64, arm64    |
| ubuntu   | focal-mainline    | main        | amd64, arm64    |
| ubuntu   | jammy             | main        | amd64, arm64    |
| ubuntu   | jammy-mainline    | main        | amd64, arm64    |
| ubuntu   | noble             | main        | amd64, arm64    |
| ubuntu   | noble-mainline    | main        | amd64, arm64    |

This module parses the Accept-Language header and gives the most suitable locale for the user from a list of supported locales from your website.

Syntax

set_from_accept_language $lang en ja pl;
  • $lang is the variable in which to store the locale
  • en ja pl are the locales supported by your website

If none of the locales from Accept-Language is available on your website, it sets the variable to the first locale of your website's supported locales (in this case, en).

Caveat

It currently assumes that the Accept-Language is sorted by quality values (from my tests it's the case for safari, firefox, opera and ie) and discards q (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). In the situation where I'm using the module, this assumption works... but buyer beware :-)

Example configuration

If you have different subdomains for each languages

server {
    listen 80;
    server_name your_domain.com;
    set_from_accept_language $lang en ja zh;
    rewrite ^/(.*) http://$lang.your_domain.com redirect;
}

Or you could do something like this, redirecting people coming to '/' to /en (or /pt):

location / {
    set_from_accept_language $lang pt en;
     if ( $request_uri ~ ^/$ ) {
       rewrite ^/$ /$lang redirect;
       break;
     }
}

Why did I create it?

I'm using page caching with merb on a multi-lingual website and I needed a way to serve the correct language page from the cache I'll soon put an example on http://gom-jabbar.org

Acknowledgement

Thanks to Evan Miller for his guide on writing nginx modules.