[英]Setup nginx-ingress to match two paths to the same serviceName and servicePort
What I'm trying to do我正在尝试做的事情
/
needs to go to the FE client
running on 3000
./
需要到 go 到运行在3000
上的 FE client
的流量。/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),
]
/api/auth
work fine./api/auth
工作正常。/api/admin
do not./api/admin
不会。 Django removes the /api
from the URL making it just /admin
which doesn't exist. /api
,使其仅/admin
不存在。 This seems to be the default behavior of Django. What does work有什么作用
- path: /?(.*)
backend:
serviceName: server-cluster-ip-service
servicePort: 5000
/admin
works fine./admin
工作正常。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.所以不太清楚如何解决这个问题。
nginx-ingress
?nginx-ingress
解决这个问题? Somehow strip off the /api
after the request is submitted?/api
?/api
off of /api/admin
?/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.