简体   繁体   English

设置 nginx-ingress 以匹配两个路径到相同的 serviceName 和 servicePort

[英]Setup nginx-ingress to match two paths to the same serviceName and servicePort

What I'm trying to do我正在尝试做的事情

  • Traffic to / needs to go to the FE client running on 3000 ./需要到 go 到运行在3000上的 FE client的流量。
  • Traffic to /api needs to go to the BE server ./api的流量需要 go 到 BE server
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/add-base-url: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - http:
        paths:
          - path: /?(.*)
            backend:
              serviceName: client-cluster-ip-service
              servicePort: 3000
          - path: /api?/(.*)
            backend:
              serviceName: server-cluster-ip-service
              servicePort: 5000

The problem问题

# urls.py
urlpatterns = [
    path('auth/', include('authentication.urls')),
    path('admin/', admin.site.urls),
]
  • Fetches to /api/auth work fine.获取/api/auth工作正常。
  • Fetches to /api/admin do not.获取/api/admin不会。 Django removes the /api from the URL making it just /admin which doesn't exist. Django 从 URL 中删除/api ,使其仅/admin不存在。 This seems to be the default behavior of Django.这似乎是 Django 的默认行为。 As far as I can tell there is no way to override it.据我所知,没有办法覆盖它。

What does work有什么作用

- path: /?(.*)
  backend:
    serviceName: server-cluster-ip-service
    servicePort: 5000
  • Navigating to /admin works fine.导航到/admin工作正常。
  • However, this removes the ability for traffic to be routed to the client .但是,这消除了将流量路由到client的能力。

Somehow, I need to prevent Django from stripping off the prefix.不知何故,我需要防止 Django 剥离前缀。 Or maybe there is a way to handle this type of routing from nginx-ingress .或者也许有一种方法可以从nginx-ingress处理这种类型的路由。

What doesn't work什么不起作用

Any variation of the following:以下任何变体:

- http:
    paths:
      - path: /?(.*)
        backend:
          serviceName: client-cluster-ip-service
          servicePort: 3000
      - path: /api?/(.*)
        backend:
          serviceName: server-cluster-ip-service
          servicePort: 5000
      - path: /admin?/(.*)
        backend:
          serviceName: server-cluster-ip-service
          servicePort: 5000

urlpatterns = [
    path('auth/', include('authentication.urls')),
    path('/', admin.site.urls),
]

urlpatterns = [
    path('auth/', include('authentication.urls')),
    path('', admin.site.urls),
]

urlpatterns = [
    path('auth/', include('authentication.urls')),
    path('api/admin/', admin.site.urls), # this justmakes it /api/api/admin given the ingress
]

# This just makes the URL pattern:
# - /api/api/auth/
# - /api/api/admin/
urlpatterns = [
    path('api/', include([
        path('auth/', include('authentication.urls'), name='auth'),
        path('admin/', admin.site.urls),
    ])),
]

Question问题

So not quite sure how to resolve this.所以不太清楚如何解决这个问题。

  • Is there a way to resolve this via nginx-ingress ?有没有办法通过nginx-ingress解决这个问题? Somehow strip off the /api after the request is submitted?提交请求后以某种方式剥离/api
  • Is there a way to prevent the default behavior of Django that strips /api off of /api/admin ?有没有办法防止 Django 的默认行为将/api/api/admin中剥离出来?

Ok, finally figured it out.好的,终于想通了。 Definitely was a Django setting.绝对是 Django 设置。 Added the following to the Django settings.py :在 Django settings.py添加了以下内容:

FORCE_SCRIPT_NAME = '/api/'

Then I had to update the STATIC_URL because it was no longer serving the assets for the admin portal:然后我不得不更新STATIC_URL因为它不再为管理门户提供资产:

STATIC_URL = '/api/static/`

Added the following to the Django settings.py:在 Django settings.py 中添加了以下内容:

 FORCE_SCRIPT_NAME = '/api/'

Update the STATIC_URL because it will no longer serve the assets for the admin portal:更新 STATIC_URL,因为它将不再为管理门户提供资产:

 STATIC_URL = '/api/static/`

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

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