Nginx ingress controller websocket support

淺唱寂寞╮ 提交于 2020-07-03 02:43:06

问题


Recently I've been working on a toy app using Kubernetes. Part of the app is a web server that needs to support WebSockets. Currently, I'm using port-forwarding to access the web server and everything works just fine.

I'd like to switch to using an Ingress and IngressController to avoid using the port forwarding.

Here is my Ingress config:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/secure-backends: "true"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
spec:
  rules:
  - http:
      paths:
      - path: /app
        backend:
          serviceName: web-svc
          servicePort: 3030
      - path: /ws
        backend:
          serviceName: web-svc
          servicePort: 3030

Now accessing the app through $(minikube ip)/app works just fine, but the WebSocket requests all fail because nginx is returning a 200 and not a 101.

I've tried adding the nginx.org/websocket-services annotation but that doesn't seem to be working either.

Has anyone encountered a similar situation?

Cheers


回答1:


From looking at the nginx ingress controller docs and the nginx docs you probably need something like this as an annotation on your Kubernetes Ingress:

nginx.ingress.kubernetes.io/configuration-snippet: |
   proxy_http_version 1.1;
   proxy_set_header Upgrade "websocket";
   proxy_set_header Connection "Upgrade";

Note that once you add that annotation all of your Ingress rules will have that snippet in the location block in your nginx configs. So if you want to ignore it for other rules you will have to create a separate Kubernetes Ingress.



来源:https://stackoverflow.com/questions/52883653/nginx-ingress-controller-websocket-support

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