title: Error connecting to websocket server with minikube NGINX ingress
body: <p>My question is very similar to this <a href="https://stackoverflow.com/questions/57642650/websocket-connection-in-kubernetes-cluster-with-nginx-ingress">post</a>. I am trying to access a socket server with NGINX ingress in minikube. I am not sure if different annotations are still necessary for cluster v1.22.2, as described <a href="https://kubernetes.github.io/ingress-nginx/user-guide/miscellaneous/#websockets" rel="nofollow noreferrer">here</a>, but I tried adding them as described in various stackoverflow <a href="https://stackoverflow.com/questions/53770981/using-socket-io-on-gke-with-nginx-ingress">posts</a> and attempted <a href="https://stackoverflow.com/questions/58697950/kubernetes-nginx-ingress-and-socket-io-connection-issues">this</a> and <a href="https://stackoverflow.com/questions/54163642/nginx-ingress-controller-not-able-to-connect-with-websockets">this</a>, but none worked for me.</p>
  <p>Although it works fine when I forward the service port, I am getting error when I am trying to access it with ingress.</p>
  <pre><code>*WebSocket connection to 'ws://textchess.com/socket.io/?EIO=4&amp;transport=websocket' failed:*
  </code></pre>
  <p>The <code>driver\etc\host</code> file has minikube's ip as textchess.com</p>
  <p><strong>index.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const express = require(&quot;express&quot;);
  const { createServer } = require(&quot;http&quot;);
  const { Server } = require(&quot;socket.io&quot;);
  
  const app = express();
  const httpServer = createServer(app);
  const io = new Server(httpServer, {
    cors: {
      origin: &quot;*&quot;,
      methods: [&quot;GET&quot;, &quot;POST&quot;],
    },
  });
  
  io.on(&quot;connection&quot;, (socket) =&gt; {
  
    console.log(&quot;connection established&quot;);
  
    socket.on(&quot;client_emit&quot;, (arg) =&gt; {
      socket.emit(&quot;server_emit&quot;, arg);
    });
  });
  
  httpServer.listen(3000);
  </code></pre>
  <p><strong>Deployment/ClusterIP service</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: socket-deployment
    labels:
      app: socket
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: socket
    template:
      metadata:
        labels:
          app: socket
      spec:
        containers:
          - name: socket
            image: socket
  
  ---
  # clusterIP service
  apiVersion: v1
  kind: Service
  metadata:
    name: socket-srvc
  spec:
    type: ClusterIP
    selector:
      app: socket
    ports:
      - name: socket-port
        protocol: TCP
        port: 3000
        targetPort: 3000
  </code></pre>
  <p><strong>ingress</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1
  kind: Ingress
  metadata:
    name: ingress-test
    annotations:
      nginx.ingress.kubernetes.io/rewrite-target: /
      nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
      nginx.org/websocket-services: &quot;socket-srvc&quot;
  spec:
    rules:
      - host: textchess.com
        http:
          paths:
            - path: /test
              pathType: Prefix
              backend:
                service:
                  name: socket-srvc
                  port:
                    number: 3000
  
            - path: /socket.io/.*
              pathType: Prefix
              backend:
                service:
                  name: socket-srvc
                  port:
                    number: 3000
  
    ingressClassName: nginx
  
  ---
  apiVersion: networking.k8s.io/v1
  kind: IngressClass
  metadata:
    name: nginx
  spec:
    controller: k8s.io/ingress-nginx
  </code></pre>
  <p><strong>client</strong></p>
  <pre class="lang-js prettyprint-override"><code>&lt;script&gt;
    // const socket = io(&quot;http://localhost:3000&quot;, { transports: [&quot;websocket&quot;] });
  
    const socket = io(&quot;http://textchess.com:/test&quot;, {
      reconnection: true,
      transports: [&quot;websocket&quot;],
    });
  
    socket.on(&quot;server_emit&quot;, (args) =&gt; {
      console.log(args);
    });
    document.getElementById(&quot;event_emitter&quot;).onclick = function changeContent() {
      var d = new Date();
      var time = d.toLocaleTimeString();
      socket.emit(&quot;client_emit&quot;, time);
    };
  &lt;/script&gt;
  </code></pre>