[英]GCP Load Balancing (non classic) - URL rewrite
I have a very simple use case and I can't seem to find a way to do it.我有一个非常简单的用例,我似乎无法找到一种方法来做到这一点。 My application has an app part (angular) and an api part (nodejs).我的应用程序有一个应用程序部分(角度)和一个 api 部分(nodejs)。 From the UI, although I call the api as /api/*
, the api server itself does not have the /api
prefix.在 UI 中,虽然我将 api 称为/api/*
,但 api 服务器本身没有/api
前缀。 Because of this, I need to rewrite any request for /api
to /
.因此,我需要将对/api
的任何请求重写为/
。
In the development environment, my angular application simply has a proxy configuration which does the rewriting:在开发环境中,我的 angular 应用程序只有一个执行重写的代理配置:
{
"/api/*": {
"target": "http://localhost:3000",
"pathRewrite": {
"^/api/": ""
},
"secure": false,
"logLevel": "debug"
}
}
So, for the production environment, I need to configure the GCP load balancer for the following scenarios:所以,对于生产环境,我需要为以下场景配置GCP负载均衡器:
incoming path传入路径 | backend-service后端服务 | path rewrite路径重写 |
---|---|---|
/ / | ui-backend-service用户界面后端服务 | (N/A) (不适用) |
/app /应用程序 | ui-backend-servicw用户界面后端服务 | (N/A) (不适用) |
/api /API | api-backend-service api后端服务 | / / |
While I am able to configure the simple Routing rules for mapping the host and path to the ui-backend-service, it looks really difficult to rewrite url for /api
.虽然我能够配置用于将主机和路径映射到 ui-backend-service 的简单路由规则,但为/api
重写 url 看起来真的很难。
Any example that I could find for URL rewriting shows the Classic Load Balancing, which does not seem to be applicable anymore.我能找到的 URL 重写的任何示例都显示了经典负载平衡,它似乎不再适用。
I tried modifying the Advanced host and path rules
, but it tells me that I can either provide pathRules
or routeRules
, not both.我尝试修改Advanced host and path rules
,但它告诉我我可以提供pathRules
或routeRules
,而不是两者。 I cannot create a separate rule, because the host (which is *
in my case) cannot be used more than once.我无法创建单独的规则,因为主机(在我的例子中是*
)不能多次使用。
I don't want to setup an nginx on my api server just for this.我不想为此在我的 api 服务器上设置 nginx。 Is there another way to do it?还有另一种方法吗?
I finally managed to resolve my problem.我终于设法解决了我的问题。
Apparently, the web UI of GCP Console for Load Balancing does not yet support a way to let users add rewrite rules through a form.显然,用于负载平衡的 GCP Console 的 web UI 尚不支持让用户通过表单添加重写规则的方法。 The only way to do it is to add the configuration manually.唯一的方法是手动添加配置。
I found two ways of doing this:我找到了两种方法:
(This answer assumes that you already have a load-balancing configuration created) (此答案假定您已经创建了负载平衡配置)
Advanced host and path rules
and add the following in the text-area provided for the rules:在路由规则中,select Advanced host and path rules
,并在为规则提供的文本区域中添加以下内容:defaultService: projects/my-project/global/backendServices/ui-backend-service
name: path-matcher-1
pathRules:
- paths:
- /
service: projects/my-project/global/backendServices/ui-backend-service
- paths:
- /app
- /app/*
service: projects/my-project/global/backendServices/ui-backend-service
- paths:
- /api
- /api/*
service: projects/my-project/global/backendServices/api-backend-service
routeAction:
urlRewrite:
pathPrefixRewrite: /
gcloud compute url-maps edit <lb-name>
This will retrieve the load-balancing configuration and show it in an editor (in linux it shows it in a vi editor in the terminal)这将检索负载平衡配置并将其显示在编辑器中(在 linux 中,它将在终端的 vi 编辑器中显示)
Comment out (by placing #
at the beginning of each line) or delete the existing configuration.注释掉(通过在每行的开头放置#
)或删除现有配置。 Insert the above configuration (with some changes, as explained below) in the vi editor in place of the lines commented out or deleted.在 vi 编辑器中插入上述配置(有一些更改,如下所述)以代替注释掉或删除的行。
When updating the configuration through gcloud, the backend services need to be referenced using URIs.通过 gcloud 更新配置时,需要使用 URI 引用后端服务。 So, just add https://www.googleapis.com/compute/v1/
before each service reference.因此,只需在每个服务引用之前添加https://www.googleapis.com/compute/v1/
。
The configuration should look similar to the one shown below:配置应类似于下图所示:
defaultService: https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/ui-backend-service
name: path-matcher-1
pathRules:
- paths:
- /app
- /app/*
service: https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/ui-backend-service
- paths:
- /
service: https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/ui-backend-service
- paths:
- /api
- /api/*
service: https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/api-backend-service
routeAction:
urlRewrite:
pathPrefixRewrite: /
Keep in mind that any changes to the load-balancing configurations usually take a few minutes to take effect.请记住,对负载平衡配置的任何更改通常需要几分钟才能生效。 So, be patient and try out the changes accordingly.因此,请耐心等待并尝试相应的更改。
Inspiration : https://cloud.google.com/load-balancing/docs/https/setting-up-url-rewrite#gcloud灵感: https://cloud.google.com/load-balancing/docs/https/setting-up-url-rewrite#gcloud
In the "inspirational" link above, the Console
version seems outdated (at the time of writing this answer).在上面的“鼓舞人心的”链接中, Console
版本似乎已经过时(在撰写此答案时)。 The gcloud
version, however, works fine.然而, gcloud
版本工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.