In RabbitMQ, a virtual host (vhost) is a logical grouping of resources such as exchanges, queues, bindings, and users. It allows you to create isolated environments within a single RabbitMQ instance, enabling multiple applications or tenants to share the same RabbitMQ server without interfering with each other. Each vhost has its own set of permissions and configurations, which helps manage access control and resource allocation.

RabbitMQ Virtual Hosts

Key Points:

  1. Isolation: Vhosts provide isolation between different applications or tenants.
  2. Permissions: Access to resources within a vhost is controlled by user permissions.
  3. Configuration: Each vhost can have its own configuration settings.
  4. Multi-tenancy: Vhosts enable multi-tenancy on a single RabbitMQ instance.

Example Use Case:

Suppose you have two different applications, AppA and AppB, running on the same RabbitMQ server. You can create two separate vhostsvhostA and vhostB, to ensure that the resources and messages of AppA do not interfere with those of AppB.

We can create different virtual hosts and each virtual host will have users.

How to Create a Virtual Host

To create a new virtual host, first log in using the default credentials, then go to the Admin tab.

Open RabbitMQ Management UI (http://localhost:15672/).

RabbitMQ Login

Go to Admin > Virtual Hosts.

Click Add a new virtual host.

RabbitMQ Virtual Hosts

After selecting ‘Virtual Hosts,’ you’ll see the default virtual hosts. Below that, click ‘Add a new virtual host’ to expand the panel.

Create Virtual Hosts

After expanding the panel, enter a name for the virtual host. Here, I’m using ‘DemoVirtualHost.’ Then, click ‘Add Virtual Host‘ to create it.

Adding Virtual Hosts

After adding a virtual host, it will appear in the ‘All Virtual Hosts‘ panel.

All Virtual Hosts

So far, we’ve created a virtual host but haven’t assigned a user to it. Now, let’s create a new user.

How to Create a User

In this section, we’ll create a new user. Set the username to ‘AdminUser‘ and the password to ‘Admin@123‘ Then, assign the ‘administrator‘ privilege by clicking the ‘Admin‘ link, which will populate the tags field. Finally, click ‘Add User‘ to create the new user.

Create User in RabbitMQ

After adding a user, expand the ‘All Users‘ panel to view the list of users. You’ll see the new user, ‘AdminUser,’ but in the ‘Can Access Virtual Hosts’ column, the value is ‘No Access,’ indicating that the user doesn’t have privileges for virtual hosts.

User with No access

Granting privileges to access virtual hosts

For setting permission just click on the username which we have created “AdminUser”.

Granting privileges

Click on ‘AdminUser,’ then navigate to the Permissions panel below the Overview panel.

Expand the Permissions panel and click the ‘Set Permission‘ button to grant access to ‘AdminUser.

Set Permission

The default value is ‘.*‘, which allows access to all exchanges and queues in the virtual host.

Once the permission is set, the “Current permission” will be displayed as shown below.

Set Current permission

Now, you can log in as an ‘AdminUser‘ to the RabbitMQ Web Management Plugin.

Let’s Login

Login

Below is a screenshot after logging into the RabbitMQ Web Management Plugin with the new user, “AdminUser“.

Login with New User

We have now completed creating a new virtual host and assigning a new user to it.

Conclusion

In conclusion, we have walked through the entire process of creating a new virtual host in RabbitMQ. This included logging in with default credentials, navigating to the ‘Virtual Hosts‘ section, and adding a new virtual host. We then created a new user, ‘AdminUser,’ assigned the necessary ‘administrator‘ privileges, and ensured the user could access the virtual host. Finally, we verified that the user had access by logging into the RabbitMQ Web Management Plugin. This process ensures that the virtual host is properly set up and that the user has the necessary permissions to interact with it.

By Saineshwar

Microsoft MVP for Developer Technologies | C# Corner MVP | Code project MVP | Senior Technical Lead | Author | Speaker | Love .Net | Full Stack developer | Open source contributor.