'My Websocket works only on localhost and not on nginx

I am working on an application, I used nodejs express as the server and reactjs for the client.

Here's my config for the server for socketIO

const server = app.listen(port, () =>
  console.log(`Server running on port ${port}`)
) // Port is 8000
const io = require('socket.io')(server, {
  cors: {
    origin: '*',
  },
})

For the react app, I called

useEffect(() => {
    if (!socket.current) {
      socket.current = io(process.env.REACT_APP_API)
    } // REACT_APP_API is http://localhost:8000
}

This works very well for my localhost.

When I run this on nginx, I get WebSocket connection to 'wss://mydomain.com:

I configured my nginx as this

location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }


location /api {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }


 location /socket.io/  {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy false;

      proxy_pass http://localhost:3000/socket.io/;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }

In my .env in nginx, i have set my REACT_APP_API to point to /socket.io/ then i created another to point to the main api for /api



Solution 1:[1]

The problem is the lifetime of the Geometry object created for insertion in the map, i.e. this line:

    map.insert({"geometry-id-1", {"id1", std::move(geometry)}});

The map stores only a reference to the object, but the object itself does not live after this line.

Replacing it with the following should make it work as you expected:

    Geometry g = {"id1", std::move(geometry)};
    map.insert({"geometry-id-1", g});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nielsen