RabbitMQ Virtual Hosts

In rabbitmq, virtual hosts are like a virtual box which contains a logical grouping of connections, exchanges, queues, bindings, user permissions, policies and many more things.

 

Following is the pictorial representation of virtual hosts in rabbitmq.

 

Virtual Hosts in RabbitMQ

 

In rabbitmq, we can create different virtual hosts and each virtual host will have users based on requirements.

RabbitMQ Create Virtual host

To create a virtual host in rabbitmq, we need to login into rabbitmq web management portal with default credentials (guest) like as shown below.

 

RabbitMQ Web Management Portal Login

 

Once login into web management portal, then choose admin tab. In admin section you will see vertical menus in the right part of the page, in that choose “Virtual Hosts” like as shown below.

 

RabbitMQ Select Virtual Hosts from Admin Section

 

After choosing “Virtual Hosts”, you will see the default “Virtual Hosts” which is already present or created. To create a new virtual host we have a panel called “Add a new virtual host”, just click on it to expand like as shown below.

 

RabbitMQ Click on Add a New Virtual Host Panel

 

After expanding the panel, you can see Name parameter here we are entering the name as “demohost”, next click on Add virtual host button to create “demohost” virtual host like as shown below.

 

RabbitMQ Enter Details to Create New Virtual Host

 

After adding a virtual host, we can see the newly added virtual host in All virtual hosts panel like as shown below.

 

RabbitMQ Available Virtual Hosts

RabbitMQ Assign Virtual Hosts to Users

We are done with creation of virtual host but if we want to assign users to newly created virtual host, then we need to create users with required privileges and assign virtual host for that user. To know how to create users in rabbitmq check this, rabbitmq users.

 

Here, we are going to create a new user “virtualuser” by setting a password as “123456” and privilege as “administrator” (just click on Admin link, it will set “administrator” in tags field) like as shown below.

 

RabbitMQ Create User to Assign Virtual Host

 

After creating the user, we can see the newly created user details in All users panel like as shown below. If you observe new user (“virtualuser”) details in grid, the “Can access virtual hosts” column has a value of “No access” which means the user doesn’t have the privilege to access any virtual hosts.

 

Users List in RabbitMQ Management Portal

RabbitMQ Set Privilege to Access Virtual Hosts

To set a permission for users to access virtual hosts, just click on the username which we have created “virtualuser” and go to Permissions panel which is just below the Overview panel and select required virtual host from dropdownlist and click on Set permission button like as shown below.

 

RabbitMQ Add Virtual Host to New User

 

Here, the default value “.*” is used to allow user to access all exchanges and queues in the virtual host. Once the permission is set, you’ll see the “Current permission” like as shown below.

 

RabbitMQ User with Virtual Hosts

 

Now, the newly created user (“virtualuser”) has a permission to login into rabbitmq web management. Let’s login into rabbitmq web management plugin with new user “virtualuser” like as shown below.

 

RabbitMQ Login with Virtual Hosts User

 

Following is the snapshot after logging into rabbitmq web management plugin with new user “virtualuser”.

 

RabbitMQ Virtual Hosts in Admin Section

 

This is how we can create virtual hosts in rabbitmq and assign it to users based on our requirements.