Black Friday 2025 – NPI EA (cat = Baeldung on Linux)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Introduction

Nginx has emerged as a popular open-source web server software used to serve content on the internet. Its high performance and scalability make it a preferred choice among developers and system administrators. One of the most significant advantages of Nginx is its ability to handle multiple websites or virtual hosts on a single server.

In this article, we’ll explore the concept of virtual hosts and how to list all virtual hosts in Nginx.

2. Virtual Hosts

Virtual hosts, also known as server blocks, are a way to host multiple websites or domains on a single server. Consequently, Nginx uses virtual hosts to route requests to the appropriate website based on the domain name in the request.

Each virtual host has its own configuration, including the domain name, root directory, and other settings. Properly configured virtual hosts are essential for ensuring that websites are running smoothly. Misconfigured virtual hosts can lead to website downtime or errors, which can negatively impact the user experience and lead to lost revenue.

3. Listing All Virtual Hosts

To list all virtual hosts in Nginx, we can use the following command:

$sudo nginx -T

This command will display the entire Nginx configuration, including all virtual hosts. The output will be quite long, so we can use the grep command to filter the output and only display the virtual host configurations:

$sudo nginx -T | grep server_name

This will display the server_name directive for each virtual host:

server_name example.com;
server_name www.example.com;
server_name example2.com;

The first command, sudo nginx -T, displays the entire Nginx configuration, including all virtual hosts. The -T option tells Nginx to display the configuration in a human-readable format.

The second command, sudo nginx -T | grep server_name, uses the pipe symbol (|) to send the output of the first command to the grep command. The grep command filters the output to only display lines that contain the string server_name. This is because the server_name directive defines the domain name for each virtual host. The output displays the server_name directive for each virtual host. In this example, we have three virtual hosts: example.com, www.example.com, and example2.com.

4. Conclusion

In this tutorial, properly configured virtual hosts are essential for ensuring the smooth operation of our websites. By taking the time to manage our virtual hosts in Nginx, we can improve server resource management, prevent conflicts, and ensure that our websites are always available and running smoothly.

Misconfigured virtual hosts can lead to website downtime or errors, which can negatively impact the user experience and lead to lost revenue. With Nginx’s ability to handle multiple virtual hosts on a single server, managing virtual hosts is easy and straightforward.

Overall, virtual hosts play a crucial role in Nginx’s functionality and are a powerful tool for web developers and system administrators.