<p>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.</p>

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

<p>Here is my <code>Ingress</code> config:</p>

<pre><code>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
</code></pre>

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

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

<p>Has anyone encountered a similar situation?</p>

<p>Cheers</p>

<p>From looking at the <a href="https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/nginx-configuration/annotations.md#configuration-snippet" rel="noreferrer">nginx ingress controller docs</a> and the <a href="http://nginx.org/en/docs/http/websocket.html" rel="noreferrer">nginx docs</a> you probably need something like this as an annotation on your Kubernetes <code>Ingress</code>:</p>
<pre><code>nginx.ingress.kubernetes.io/configuration-snippet: |
   proxy_http_version 1.1;
   proxy_set_header Upgrade &quot;websocket&quot;;
   proxy_set_header Connection &quot;Upgrade&quot;;
</code></pre>
<p>Note that once you add that annotation all of your <code>Ingress</code> rules will have that snippet in the <code>location</code> block in your nginx configs. So if you want to ignore it for other rules you will have to create a separate Kubernetes <code>Ingress</code>.</p>
<p>EDIT:</p>
<p>As per the <a href="https://gist.github.com/jsdevtom/7045c03c021ce46b08cb3f41db0d76da#file-ingress-service-yaml" rel="noreferrer">gist</a> and the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/miscellaneous/#websockets" rel="noreferrer">Nginx ingress docs</a> 📄, it seems like this annotation fixed the problem:</p>
<pre class="lang-yaml prettyprint-override"><code>nginx.ingress.kubernetes.io/proxy-read-timeout: 3600
nginx.ingress.kubernetes.io/proxy-send-timeout: 3600
</code></pre>
