From 8a0fbf03e6be6af3a64c71a5b0c8514a75258992 Mon Sep 17 00:00:00 2001 From: "Theodore R. Smith" Date: Wed, 31 Oct 2018 20:23:59 -0500 Subject: [PATCH] (#134) Added nginx-based auto-language negotiation for /. **The Problem:** Upon following the directions to the letter, when I go to http://localhost:8080/ I get a 403: Forbidden nginx error screen. **The Reason:** No language detection / negotiation is done, and / is empty. **The Solution:** I added nginx-based language negotiation using the browser's `Accept-Language` header. If the user does not set a header (some search engines, curl, etc.), it will show the English page. This only applies to the / route. It is assumed that any other route will be in the desired language. Fixes #134. --- containers/nginx/dev.conf | 16 +++++++++++++++- containers/nginx/minds.conf | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/containers/nginx/dev.conf b/containers/nginx/dev.conf index b0858f0888..6b095b08cc 100644 --- a/containers/nginx/dev.conf +++ b/containers/nginx/dev.conf @@ -1,3 +1,12 @@ +# Dynamic language redirection. +# Ensure that "en" is last, or people with, for instance, "fr en" will always default to "en". +map $http_accept_language $lang { + default en; + ~es es; + ~fr fr; + ~en en; +} + server { listen 80; listen [::]:80 default ipv6only=on; @@ -24,6 +33,11 @@ server { port_in_redirect off; } + # Redirect to the language directory specified by the user's browser via the Accept-Language header. + location ~ ^/$ { + return 301 $scheme://$http_host$1/$lang/; + } + location ~ ^(/api|/fs|/icon|/carousel) { add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Credentials' 'true'; @@ -98,4 +112,4 @@ server { log_not_found off; deny all; } -} \ No newline at end of file +} diff --git a/containers/nginx/minds.conf b/containers/nginx/minds.conf index baed6df5f7..51ae3df3f4 100644 --- a/containers/nginx/minds.conf +++ b/containers/nginx/minds.conf @@ -1,3 +1,12 @@ +# Dynamic language redirection. +# Ensure that "en" is last, or people with, for instance, "fr en" will always default to "en". +map $http_accept_language $lang { + default en; + ~es es; + ~fr fr; + ~en en; +} + server { listen 80; listen [::]:80 default ipv6only=on; @@ -25,6 +34,11 @@ server { port_in_redirect off; } + # Redirect to the language directory specified by the user's browser via the Accept-Language header. + location ~ ^/$ { + return 301 $scheme://$http_host$1/$lang/; + } + location ~ ^(/api|/fs|/icon|/carousel) { add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload"; add_header 'Access-Control-Allow-Origin' "$http_origin";