简体   繁体   English

GCP 负载平衡(非经典)- URL 重写

[英]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 ,但它告诉我我可以提供pathRulesrouteRules ,而不是两者。 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) (此答案假定您已经创建了负载平衡配置)

1. Console 1.控制台

  1. edit the HTTP(S) load balancer编辑 HTTP(S) 负载平衡器
  2. add Frontend and backend configurations添加前端和后端配置
  3. in Routing rules, select 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: /

2. gcloud 2.gcloud

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.

相关问题 GCP:负载均衡器重写路径 - GCP: load balancer rewrite path GCP:HTTP(S) 负载平衡后端服务区域选择 - GCP: HTTP(S) Load Balancing backend service region select 如何在 ingress.yaml 中定义全局负载均衡器而不是默认为经典负载均衡器 - gcp - How to define global load balancer in ingress.yaml instead of defaulting to classic load balancer - gcp WordPress & 谷歌云平衡负载平衡 - WordPress & Load balancing on Google Cloud Balancing 无法在 GCP 中使用 Terraform 创建基于经典路由的 VPN 隧道 - Cannot create a classic route based VPN tunnel with Terraform in GCP 将 Google Load Balancer 设置为 GCP 存储桶时,“$[DEFAULT_SERVICE_URL]”指的是什么? - What does "$[DEFAULT_SERVICE_URL]" refer to when setting Google Load Balancer towards a GCP Storage Bucket? 检索 GCP 工作流回调 URL - Retrieve GCP Workflow callback URL 在 GCP 中部署非 web 应用程序 - Deploying non web applications in GCP Kubernetes 服务:IPVS 负载均衡算法 - Kubernetes Service: IPVS load balancing algorithm GKE - 使用 Ingress 和内部负载平衡公开服务 - GKE - expose service with Ingress and Internal Load Balancing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM