I have an angular application, that I've localized in multiple languages (let's say english, and french).
I would like to serve different locale version of the app depending on the url:
- myapp.io/en/account
- myapp.io/en/settings
- myapp.io/fr/account
- ...
I tried to follow the steps in the official documentation, with no luck.
So this is my nginx config:
http {
# Browser preferred language detection (does NOT require
# AcceptLanguageModule)
map $http_accept_language $accept_language {
~*^en en;
~*^fr fr;
}
server {
listen 4200;
server_name localhost;
root /usr/share/nginx/html;
# index index.html index.htm;
include /etc/nginx/mime.types;
add_header Access-Control-Allow-Origin "*";
add_header Access-Control-Allow-Methods "GET, POST, PATCH, PUT, DELETE, OPTIONS";
add_header Access-Control-Allow-Headers "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Origin, Accept";
add_header Access-Control-Expose-Headers "Content-Length,Content-Range";
# Fallback to default language if no preference defined by browser
if ($accept_language ~ "^$") {
set $accept_language "en";
}
# Redirect "/" to Angular application in the preferred language of the browser
rewrite ^/$ /$accept_language permanent;
# Everything under the Angular application is always redirected to Angular in the
# correct language
location ~ ^/(en|fr) {
try_files $uri $uri/ /index.html;
}
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}
}
... and this is how I've configured the router:
const routes: Routes = [
{
path: "account",
component: AccountComponent,
},
{
path: "settings",
component: SettingsComponent,
},
{
path: "",
redirectTo: "account",
pathMatch: "full",
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
At this point I can visit to https://myapp.io/en/. I can navigate the website, and reach https://myapp.io/en/account. If I press reload at this point I get this nginx error:
Does anyone know what I am doing wrong? Or can anyone point to an open source working example, or a good tutorial?
Update
I've changed the nginx configuration as suggested here; this fixed the problem in the screenshot, but still a problem remains: when I try to load the host (https://myapp.io/) I get redirected to https://myapp.io:4200/en/
Update 2
Fixed changing this block:
location = / {
try_files $uri /$accept_language/index.html;
}