简体   繁体   English

GCP API 网关忽略尾部斜杠 URL 并在调用 Django API 时导致 ERR_TOO_MANY_REDIRECTS

[英]GCP API Gateway ignores a trailing slash of URL and causes ERR_TOO_MANY_REDIRECTS in calling Django APIs

I have been testing GCP's API Gateway and found a problem in handling my Django's REST APIs running on GAE .我一直在测试 GCP 的 API 网关,发现在处理我在GAE上运行的 Django 的 REST API 时出现问题。

I got ERR_TOO_MANY_REDIRECTS error when accessed the API with the URL generated by the Gateway (it's like https://gw--fake-test-xyz9ry1.uk.gateway.dev/hello ).使用网关生成的 URL 访问 API 时出现ERR_TOO_MANY_REDIRECTS错误(类似于https://gw--fake-test-xyz9ry1.uk.gateway.dev/hello )。

My Gateway's API Config looks like below (Note: all URLs are fake):我的网关的 API 配置如下所示(注意:所有 URL 都是假的):

 swagger: '2.0' info: title: test-01 description: Sample APIs through API Gateway with GAE version: 1.0.0 schemes: - https produces: - application/json paths: /hello: get: summary: Just a simple get function operationId: hello x-google-backend: address: https://myfakeapp.uk.a.appspot.com/hello/ responses: '200': description: A successful response schema: type: string

I confirmed that the access to the original GAE's API ( https://myfakeapp.uk.a.appspot.com/hello/ ) worked fine.我确认访问原始 GAE 的 API ( https://myfakeapp.uk.a.appspot.com/hello/ ) 工作正常。

My guess was that Django's APPEND_SLASH setting was trying to append a slash and it caused the redirection trouble.我的猜测是 Django 的 APPEND_SLASH 设置试图 append 一个斜线,它导致了重定向问题。 To solve it, I set APPEND_SLASH = False in settings.py in Django and redeployed the GAE app and recreated the Gateway.为了解决这个问题,我在 Django 的settings.py中设置APPEND_SLASH = False并重新部署 GAE 应用程序并重新创建网关。 Since the URL with slash was set in the x-google-backend address, I thought the Gateway would transfer the request to that address.由于在 x-google-backend 地址中设置了带斜杠的 URL,我认为网关会将请求转移到该地址。

When I accessed the API with the GW's URL, however, the response was 404 error saying the request URL was https://myfakeapp.uk.a.appspot.com/hello (Note that there is no trailing slash in this URL).但是,当我使用 GW 的 URL 访问 API 时,响应是 404 错误,表示请求 URL 是https://myfakeapp.uk.a.appspot.com/hello (请注意,此 URL 中没有尾部斜杠)。 The slash in the x-google-backend address value in API Config seemed to be ignored. API Config 中 x-google-backend 地址值中的斜线似乎被忽略了。

How can I map Django's APIs to the Gateway's URLs without problems?我怎样才能 map Django 的 API 到网关的 URL 没有问题?

I was able to map my Cloud Run's API (which is Flask-based) to the Gateway, but I need to use the APIs by Django running on GAE.我能够 map 我的 Cloud Run 的 API(基于 Flask)到网关,但我需要通过在 GAE 上运行的 Django 使用 API。

you can solve this issue ?你能解决这个问题吗? y have the same error你有同样的错误

I had the same issue deploying Django Rest Framework #DRF with Google API Gateway.我在使用 Google API 网关部署 Django Rest Framework #DRF 时遇到了同样的问题。 and here is how I solved it:这是我解决它的方法:

  1. Setting APPEND_SLASH = False in your settings.py file.在 settings.py 文件中设置 APPEND_SLASH = False。
  2. Removed slashes from urls inside the code itself.从代码本身的 url 中删除了斜线。
  3. Make sure to remove slashes in API paths.确保删除 API 路径中的斜线。

Here is a snippet from the config file:这是配置文件中的一个片段:

paths:
  /movie:
    get:
      x-google-backend:
        address: https://XXXXXXX.a.run.app/api/v1/movie

NOTE:笔记:

  • This will led to 404 when calling the backend directly if a trailing slash enforced from, may be, the browser, but curl will continue works fine.如果浏览器强制执行尾部斜杠,则直接调用后端时将导致404 ,但 curl 将继续正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM