我遇到了问题,因为我想使用 i18n 在 Kubernetes 上运行 Angular 9 应用程序。
我的应用使用 /api/ 调用 API,该 API 由 nginx 代理到另一个 pod (rest-api)。
自从我添加了 i18n,我可以在生产环境中使用不同版本的应用程序。但是找不到让 API 调用工作的方法!在每个版本中,我的 api 调用以 /fr/api/some_request 或 /en/api/some_request .. 开始,这是由 baseHref 动态添加的
将 /en/api/ - /fr/api/ 转换为 /api/ 的 nginx 配置是什么?
我的结构是:
- dist/ :
- fr/
- en/
我使用以下命令构建 dist:
ng build --prod --i18n-locale fr --i18n-format xlf --i18n-file src/translate/messages.fr.xlf --output-path=dist/fr --base-href /fr/
ng build --prod --i18n-locale en --i18n-format xlf --i18n-file src/translate/messages.en.xlf --output-path=dist/en --base-href /en/
这是我的 nginx 配置:
server {
...
location /fr/ {
autoindex on;
try_files $uri$args $uri$args/ /fr/index.html;
}
location /en/ {
autoindex on;
try_files $uri$args $uri$args/ /en/index.html;
}
# Default to FR
location / {
# Autoindex is disabled here + the $uri$args/ is missing from try_files
try_files $uri$args /fr/index.html;
}
# Proxy to API call (old config working without i18n)
location /api/ {
proxy_pass http://rest-api:9000;
}
}
谢谢:)
答案 0 :(得分:0)
考虑这个位置:
location ~ .*/api/(.*) {
proxy_pass http://rest-api:9000/api/$1;
}
这是一个正则表达式位置,它匹配任何带有 /api/ 的 URI 中的任何位置。例如,它将适用于:
example.com/api/foo/bar
example.com/api/
example.com/something/api/foo/bar
有一个捕获组 (.*) 来记录 /api/ 之后的内容。它被保存为位置变量 $1(因为它是表达式中的第一个也是唯一的捕获组)。使用它,您可以构建到 API 后端的准确路径:
proxy_pass http://rest-api:9000/api/$1;
它将把上面的例子变成:
proxy_pass http://rest-api:9000/api/foo/bar
proxy_pass http://rest-api:9000/api/
proxy_pass http://rest-api:9000/api/foo/bar