简体   繁体   English

Angular i18n nginx 重定向

[英]Angular i18n nginx redirection

Good morning,早上好,

I am trying to deploy a localized version of my Angular 9 app.我正在尝试部署我的 Angular 9 应用程序的本地化版本。 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.它工作得很好,除非我尝试访问没有 /es 或 /en-US 的 URL 。

Works fine when I access https://example.com/es/login but send me back an error when I access https://example.com/login .当我访问 https://example.com/es/login 时工作正常,但当我访问https://example.com/login给我发回错误。 I tried with all the configs possible for nginx without any luck.我尝试了所有可能的 nginx 配置,但没有任何运气。

I don't know if this is possible or I am just losing my time...我不知道这是否可能,或者我只是在浪费时间......

My nginx.config我的 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!希望它对某人有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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