moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

Mosca, Docker, Reverse Proxy and Real IP #790

Open KingRial opened 5 years ago

KingRial commented 5 years ago

Hello everyone.

Right now I am using Mosca as an MQTT Broker inside a docker container offering "MQTT" and "MQTT over websocket" protocols. The challenge I am facing right now is that, because of an humongous problem of docker, the client IP I get from client.connection.stream.remoteAddress is not the correct one.

Normally, when using HTTP protocol, to solve the problem the solution is just adding a reverse proxy before the docker container which add "X-Forwarded-For" header with the real IP to the request. Such solution just works with HTTP procotols.

Using an nginx server as a reverse proxy, I am adding the header over the websocket request to Mosca:

location / {
    proxy_pass http://{{MOSCA SERVER}}:{{PORT TO WEBSOCKET}};

    # redirect all HTTP traffic
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # WebSocket support (nginx 1.4)
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

The added header seems completly ignored by Mosca and I am not understanding if that's correct or if mosca is just lacking the correct support.

Is there any way to get the real IP from websocket connection ? Or should I follow this idea ?

Any hint would be much appreciated.