简体   繁体   中英

Angular i18n nginx redirection

Good morning,

I am trying to deploy a localized version of my Angular 9 app. I have deployed it on English (main language) and spanish (located language)

It works just fine except when I try to access an URL that doesn't have /es or /en-US on it.

Works fine when I access https://example.com/es/login but send me back an error when I access https://example.com/login . I tried with all the configs possible for nginx without any luck.

I don't know if this is possible or I am just losing my time...

My nginx.config

server {
  listen 443 ssl;
  server_name https://example.com;
  ssl_certificate /etc/nginx/certs/certificate.crt;
  ssl_certificate_key /etc/nginx/certs/certificate.key;
  ssl on;
  root   /usr/share/nginx/html;
  index  index.html index.htm;
  location /en-US/ {
        alias   /usr/share/nginx/html/en-US/;
        try_files $uri$args $uri$args/ /en-US/index.html;
    }
    location /es/ {
        alias   /usr/share/nginx/html/es/;
        try_files $uri$args $uri$args/ /es/index.html;
    }
    
     set $first_language $http_accept_language;
    if ($http_accept_language ~* '^(.+?),') {
        set $first_language $1;
    }

    set $language_suffix 'en';
    if ($first_language ~* 'es') {
        set $language_suffix 'es';
    }

     location / {
        rewrite ^/$ https://$host/$language_suffix$request_uri;
    }
}

server {
    listen 80;
    listen [::]:80;

    server_name example.com;

    return 301 https://$host$request_uri;
}

Thanks in advance:)

I have just found the solution and it works for me:

Just change

location / {
        rewrite ^/$ https://$host/$language_suffix$request_uri;
    }

to

location / {
        rewrite ^/$ https://$host/$language_suffix$request_uri;
        try_files $uri$args $uri$args/ /$language_suffix/index.html;
    }

Hope it's useful for someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM