【问题标题】:NGINX i18n without changing URLNGINX i18n 无需更改 URL
【发布时间】:2020-09-12 00:14:34
【问题描述】:

我想知道如何使用 NGINX在不更改 URL 的情况下根据浏览器语言重定向流量

这是我的文件结构:

???? en
|- ???? index.html
???? fr
|- ???? index.html

我关注了this tutorial,它按预期工作,例如/home 被重定向到 /en/home

现在是否可以在不更改地址栏 URL 的情况下做同样的事情?

这是我的 NGINX 配置:

server {
  listen 80;
  server_name localhost;

  location /en/ {
    alias /usr/share/nginx/html/en/;
    try_files $uri$args $uri$args/ /en/index.html;
  }
  location /fr/ {
    alias /usr/share/nginx/html/fr/;
    try_files $uri$args $uri$args/ /fr/index.html;
  }

  set $first_language $http_accept_language;
  if ($http_accept_language ~* '^(.+?),') {
    set $first_language $1;
  }

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

  location / {
    rewrite ^/$ /$language_suffix/index.html permanent;
  }
}

【问题讨论】:

  • 尝试:rewrite...last 而不是 rewrite...permanent
  • @RichardSmith 它不起作用。似乎是因为 Angular CLI 将 HTML 基本 href 更改为 /en/ 或 /fr/ 并且这种行为无法更改。有没有 NGINX 方法来解决这个问题?

标签: nginx internationalization angular-i18n


【解决方案1】:

将文件结构更改为(en 作为默认语言环境):

? index.html
? fr
|- ? index.html

它适用于我的 Node.js localhost,但我还没有在生产服务器上尝试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2013-10-05
    • 2013-11-30
    • 2017-03-13
    • 2013-02-25
    • 1970-01-01
    • 2012-05-17
    相关资源
    最近更新 更多