Skip to content

Instantly share code, notes, and snippets.

@christophmeissner
Last active May 5, 2023 21:55
Show Gist options
  • Save christophmeissner/8964086 to your computer and use it in GitHub Desktop.
Save christophmeissner/8964086 to your computer and use it in GitHub Desktop.
Naive multilingual static file serving with nginx using Accept-Language
#
# Naive multilingual static file serving with nginx
#
# Requests to root (/) will be redirected to language-prefixed sub location
# according to Accept-Language header. Supports only the first language in the
# Accept-Language header field.
#
# At any other location than /: Tries requested resource at given location and
# falls back to root directory (will function as default language).
#
# For translating single documents or entire directories, just duplicate
# content into language directory in root dir, eg. 'fr'. Switching language
# by user manually (ie. overriding the browser settings) can be achieved by
# a href to (the same document within) another language dir.
#
# Note: Some clients use upper case language codes. This might be worked
# around by creating an uppercase symbolic link to the lower case language
# dir.
#
map $http_accept_language $lang {
default en;
~^(?P<lcode>[a-zA-Z][a-zA-Z]).* $lcode;
}
server {
listen 80;
server_name example.com www.example.com;
# access_log /home/www_user/example.com/log/access.log;
# error_log /home/www_user/example.com/log/error.log;
# Error page (/404.html) is not translated.
error_page 404 /404.html;
location = / {
rewrite ^ http://$host/$lang$request_uri;
}
location ~ (/[a-zA-Z][a-zA-Z])?(/?.*) {
alias /home/www_user/example.com/public/;
index index.html;
try_files $uri $2 $uri/index.html $2/index.html =404;
autoindex off;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment