Robert
Robert

Reputation: 375

Mapping accept-language header to domain with nginx (and django)

I would like nginx set an appropriate accept-language header depending on requested domain:

www.domain.ru set ru-RU www.domain.com set en-US www.domain.de set de-DE

www.domain.eu do nothing let Django get the header from the browser. For 3 specified above domains force changing of the accept-language header even if english user enters www.domain.ru (force it to use russian language).

Here's my nginx config:

server {
listen 1.1.1.1;
server_name domain.eu www.domain.eu domain.de www.domain.de domain.com www.domain.com domain.ru www.domain.ru;
if($host ~* (.*)\.ru) {
set $http_accept_language 'ru-RU';
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-For  $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://127.0.0.1:8888/;
}
}

This is a part of my config (running Django via gunicorn). Django checks accept-language header if session language is not set.

Upvotes: 1

Views: 4369

Answers (1)

cobaco
cobaco

Reputation: 10556

try

if ($host ~* \.ru$) {
  set $language 'ru-RU';
}
add_header Accept-Language $language;

setting the variable with $http_.... is probably not the best idea as variables starting with $http_ are interpreted and set with by nginx itself (specifically the name you used would mean 'content of the http header 'accept_language' see http://wiki.nginx.org/HttpCoreModule#Variables). I'm not sure whether your set or nginx's would win but why play with fire?

Upvotes: 4

Related Questions