How can I use Nginx as a reverse proxy to my RabbitMQ's websocket function?

1,289 views
Skip to first unread message

Fred Wang

unread,
Aug 1, 2016, 2:26:54 AM8/1/16
to rabbitmq-users
I've enabled RabbitMQ's Web-Stomp plugin by following this, because we need STOMP over WebSockets, and it works. Now what I need is a Nginx server as a reverse proxy in front of my RabbtMQ server. Here are the configuration part from Nginx server as following.

http {

    upstrem websocket {
            # this is the actual rabbitmq server address
            server 15.15.181.73:15674 
    }

    server {
            # the nginx server addres is 15.15.182.108
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;

    location /ws/ {
                proxy_pass http://websocket;
                proxy_http_version 1.1;
                proxy_set_header Upgrade websocket;
                proxy_set_header Connection upgrade;
            }
    }
}
And here is the JavaScript code for accessing the server
var WebSocket = require('ws');
var Stomp = require('stompjs');
var ws = new WebSocket('ws://15.15.182.108/ws', {
  protocolVersion: 8,
  origin: 'http://15.15.182.108/ws',
  rejectUnauthorized: false
});

var client = Stomp.over(ws);
var on_connect = function(){


        client.send("/queue/test", {priority: 9}, "Hello, STOMP for /queue/test");

};

var on_error = function(error){
    console.log("error");
    console.log(error.headers.message);
};

client.connect('test','test',on_connect,on_error,'/');

Now what confusing me is that if I need to access the rabbitmq server through Websockets, then I should append /ws after the ip address, and it works if I access it directly. However, it seems that I can't put the /ws in the upstream section after the ip address in the Nginx configuration file. So what should I do if I need to make this work? Thanks.
Reply all
Reply to author
Forward
0 new messages