<p>Here is a sample WebSocket app that I'm trying to get it to work from a Kubernetes ingress-nginx controller.</p>

<p>Kubernetes yaml:</p>

<pre><code>echo "
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: ws-example
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: wseg
    spec:
      containers:
      - name: websocketexample
        image: nicksardo/websocketexample
        imagePullPolicy: Always
        ports:
        - name: http
          containerPort: 8080
        env:
        - name: podname
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
---
apiVersion: v1
kind: Service
metadata:
  name: ws-example-svc
  labels:
    app: wseg
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: wseg
---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ws-example-svc
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: myhostname.com
    http:
      paths:
      - backend:
          serviceName: ws-example-svc
          servicePort: 80
        path: /somecontext

" | kubectl create -f -
</code></pre>

<p>I get this error:</p>

<pre><code>WebSocket connection to 'ws://myhostname.com/somecontext/ws?encoding=text' failed: Error during WebSocket handshake: Unexpected response code: 400
</code></pre>

<p>When I try to connect using a WebSocket client web page like this <a href="http://www.websocket.org/echo.html" rel="noreferrer">http://www.websocket.org/echo.html</a></p>

<p>The version of ingress-nginx is 0.14.0.  This version supports WebSockets.</p>

<hr>

<p>Update, I'm able to directly access the websocket running pod, when I port-forward from my localhost to pod's port.</p>

<pre><code>[rpalaniappan@sdgl15280a331:~/git/zalenium] $ kubectl get pods -l app=wseg
NAME                          READY     STATUS    RESTARTS   AGE
ws-example-5dddb98cfb-vmdt5   1/1       Running   0          5h
[rpalaniappan@sdgl15280a331:~/git/zalenium] $ kubectl port-forward ws-example-5dddb98cfb-vmdt5 8080:8080
Forwarding from 127.0.0.1:8080 -&gt; 8080
Forwarding from [::1]:8080 -&gt; 8080
Handling connection for 8080
</code></pre>

<hr>

<pre><code>[rpalaniappan@sdgl15280a331:~/git/zalenium] $ wscat -c ws://localhost:8080/ws
connected (press CTRL+C to quit)
&lt; Connected to ws-example-5dddb98cfb-vmdt5
&gt; hi
&lt; hi
&lt; ws-example-5dddb98cfb-vmdt5 reports time: 2018-12-28 01:19:00.788098266 +0000 UTC
</code></pre>

<p>So basically this:</p>

<pre><code>nginx.ingress.kubernetes.io/rewrite-target: /
</code></pre>

<p>is stripping the <code>/ws</code> from the request (combined with <code>path: /ws</code>) that gets sent to the backend everytime your browser tries to issue a WebSocket connection request.  The backend expects <code>/ws</code> when it receives a connection request.</p>

<p>If you specify <code>path: /mypath</code> and <code>/mypath/*</code> it works (works for me):</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ws-example-svc
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: myhostname.com
    http:
      paths:
      - backend:
          serviceName: ws-example-svc
          servicePort: 80
        path: /mypath
      - backend:
          serviceName: ws-example-svc
          servicePort: 80
        path: /mypath/*
</code></pre>
