Angular i18n nginx redirection



早上好,

我正在尝试部署我的 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;
}

提前致谢:)

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

只需更改

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

希望它对某人有用!

最新更新