Improve nginx caching and use redirects for i18n

This commit is contained in:
wiz
2022-01-12 23:12:49 +09:00
parent 7a7e1f1f3b
commit 3c8a146302
8 changed files with 111 additions and 45 deletions

View File

@@ -35,32 +35,47 @@ add_header X-Frame-Options $frameOptions;
add_header Content-Security-Policy $contentSecurityPolicy;
# enable browser and proxy caching
add_header Cache-Control "public, no-transform";
add_header Pragma "public";
add_header Cache-Control "public";
# vary cache if user changes language preference
add_header Vary Accept-Language;
add_header Vary Cookie;
# fallback for all URLs i.e. /address/foo /tx/foo /block/000
location / {
try_files /$lang/$uri /$lang/$uri/ $uri $uri/ /en-US/$uri @index-redirect;
# see order of nginx location rules
# https://stackoverflow.com/questions/5238377/nginx-location-priority
# for exact / requests, redirect based on $lang
location = / {
if ($lang != '') {
return 302 $scheme://$host/$lang$uri;
}
try_files /en-US/index.html =404;
expires 10m;
}
location /resources {
try_files /$lang/$uri /$lang/$uri/ $uri $uri/ /en-US/$uri @index-redirect;
expires 1h;
}
location @index-redirect {
rewrite (.*) /$lang/index.html;
}
# location block using regex are matched in order
# used to rewrite resources from /<lang>/ to /en-US/
location ~ ^/(ar|bg|bs|ca|cs|da|de|et|el|es|eo|eu|fa|fr|gl|ko|hr|id|it|he|ka|lv|lt|hu|mk|ms|nl|ja|ka|no|nb|nn|pl|pt|pt-BR|ro|ru|sk|sl|sr|sh|fi|sv|th|tr|uk|vi|zh)/resources/ {
rewrite ^/[a-zA-Z-]*/resources/(.*) /en-US/resources/$1;
location ~ ^/[a-z][a-z]/resources/(.*) {
try_files $uri /en-US/resources/$1 =404;
expires 1w;
}
# used for cookie override
location ~ ^/(ar|bg|bs|ca|cs|da|de|et|el|es|eo|eu|fa|fr|gl|ko|hr|id|it|he|ka|lv|lt|hu|mk|ms|nl|ja|ka|no|nb|nn|pl|pt|pt-BR|ro|ru|sk|sl|sr|sh|fi|sv|th|tr|uk|vi|zh)/ {
try_files $uri $uri/ /$1/index.html =404;
location ~ ^/([a-z][a-z])$ {
try_files $uri /$1/index.html /en-US/index.html =404;
expires 10m;
}
location ~ ^/([a-z][a-z])/ {
try_files $uri /$1/index.html /en-US/index.html =404;
expires 10m;
}
# fallback to serving resources from en-US
location /resources {
try_files $uri /en-US/$uri /en-US/index.html;
expires 1w;
}
# fallback for all URLs i.e. /address/foo /tx/foo /block/000
location / {
try_files /$lang/$uri $uri /en-US/$uri /en-US/index.html =404;
expires 10m;
}