【问题标题】:Angular i18n nginx redirectionAngular i18n nginx 重定向
【发布时间】:2020-06-26 09:54:55
【问题描述】:

早上好,

我正在尝试部署 Angular 9 应用的本地化版本。我已经将它部署在英语(主要语言)和西班牙语(本地语言)上

它工作得很好,除非我尝试访问一个没有 /es 或 /en-US 的 URL。

访问https://example.com/es/login 时工作正常,但访问https://example.com/login 时返回错误信息。我尝试了所有可能的 nginx 配置,但没有任何运气。

我不知道这是否可能,或者我只是在浪费时间......

我的 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;
}

提前致谢:)

【问题讨论】:

    标签: angular nginx internationalization angular-i18n


    【解决方案1】:

    我刚刚找到了解决方案,它对我有用:

    只是改变

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

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

    希望它对某人有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-26
      • 2018-10-25
      • 2018-09-28
      • 2015-05-09
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多