Kubernetes NGINX Ingress changes HTTP request from a POST to a GET

╄→гoц情女王★ 提交于 2021-02-19 07:18:05

问题


I'm using Kubernetes that is bundled with Docker-for-Mac. I'm trying to configure an Ingress that routes http requests starting with /v1/ to my backend service and /ui/ requests to my Angular app.

My issues seems to be that the HTTP method of the requests are changed by ingress (NGINX) from a POST to a GET.

I have tried various rewrite rules, but to no avail. I even switched from Docker-for-Mac to Minikube, but the result is the same.

If I use a simple ingress with no paths (just the default backend) then the service is getting the correct HTTP method. The ingress below works:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  backend:
    serviceName: backend
    servicePort: 8080

But this ingress does not:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"

spec:
  rules:
  - http:
      paths:
      - path: /v1
        backend:
          serviceName: backend
          servicePort: 8080
      - path: /ui
        backend:
          serviceName: webui
          servicePort: 80

When I debug the "backend" service I see that the HTTP Request is a GET instead of a POST.

I read somewhere that NGINX rewrites issue a 308 (permanent) redirect and the HTTP method is changed from a GET to a POST, but if that is the case how can I configure my ingress to support different paths for different services that require POST calls?


回答1:


I found the solution to my problem. When I add host: to the configuration then the http method is not changed. Here is my current ingress yaml (the rewrite and regex are used to omit sending the /v1 as part of the backend URL)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /v1(/|$)(.*)
        backend:
          serviceName: gateway
          servicePort: 8080


来源:https://stackoverflow.com/questions/56177198/kubernetes-nginx-ingress-changes-http-request-from-a-post-to-a-get

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!