Open In App

How to List All vhosts in nginx in Linux

Last Updated : 13 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

On Ngnix Web Server on Linux, the virtual host or vhost refers to the task of hosting various websites on a single server by configuring various server blocks that are associated with the specific domain or IP address. This server mainly blocks allo Ngnix to handle incoming requirements and then direct them to the proper website which is based on the configuration. If we want to see the list of all the vhosts in Nginx in Linux, then we can perform the steps which are discussed in the following sections.

What is vhosts in Ngninx?

In Nginx, “vhosts” stands for virtual hosts, also known as server blocks. Virtual hosting is a method that allows a web server to host multiple websites on a single server, distinguished by their domain names or IP addresses. Each virtual host has its configuration, defining how Nginx should handle requests for that specific domain or IP. This allows multiple websites to coexist on the same server while being independently configured. In Nginx, virtual hosts are typically defined in separate configuration files located in directories like “/etc/nginx/sites-available” or “/etc/nginx/conf.d.” These configuration files contain directives specifying the root directory of the website, the domain names associated with it, SSL settings, and other parameters. Nginx processes incoming requests and directs them to the appropriate virtual host based on the information in the configuration files.

How to List all vhosts in Nginx in Linux?

In this section, we will see how we can list all vhosts in Ngnix in Linux. So follow the below steps along with proper command execution to see the detailed list of all vhosts in Ngnix in Linux:

Step 1: Open the Terminal

Firstly, we need to launch the terminal on our Linux system, we can launch by using the Application Menu or by using the Keyboard Shortcut as “CTRL + ALT + T“.

Opening Terminal

Opening Terminal

Step 2: Navigate to the Nginx Configuration Directory

Once the terminal has been launched, we need to navigate to the Nginx configuration directory which is mainly stored in the /etc/nginx. We can execute the below cd command to naivete to the required directory.

cd /etc/nginx

Navigate to the Nginx Configuration Directory

Navigate to the Nginx Configuration Directory

Step 3: View the Contents of the sites-available Directory

Now, in the sites-available directory, we will find configuration files for each virtual host. We can use the ls command to list the files.

ls sites-available

We can see virtual host configuration files in the below output screenshot, and the default is the default configuration.

 View the Contents of the sites-available Directory

View the Contents of the sites-available Directory

Step 4: View the Contents of the Configuration Files

If we want to view the contents of a specific configuration file, then we can view it by using the cat command. You need to replace geeksforgeeks.org with the actual filename of the virtual host you want to inspect. This command will display the content of the specified configuration file.

cat sites-available/geeksforgeeks.org 

View the Contents of the Configuration Files

View the Contents of the Configuration Files

Step 5: Verify Configuration Syntax

Before applying the changes, it’s a good practice to test the Nginx configuration for syntax errors. If there are no errors, you should see a message indicating that the configuration file test is successful.

sudo nginx -t

Verify Configuration Syntax

Verify Configuration Syntax

Step 6: Restart Nginx

Apply the changes by restarting the Nginx service. The below command restarts the Nginx service to apply the new configuration with root privileges.

sudo systemctl restart nginx

Restart Nginx

Restart Nginx

Step 7: Verify Changes in nginx

To verify that the changes have taken effect, we can use the nginx -T command. This command displays the entire Nginx configuration

sudo nginx -T

Verify Changes

Verify Changes in nginx

Conclusion

In conclusion, listing all virtual hosts in Nginx on a Linux system involves navigating to the Nginx configuration directory, inspecting the contents of the sites-available directory to identify virtual host configuration files, viewing specific configuration files to understand their settings, verifying the syntax of the overall Nginx configuration to catch any errors, reloading the Nginx service to apply changes, and finally, confirming the successful application of changes by reviewing the entire Nginx configuration. These steps provide a comprehensive approach to managing virtual hosts in Nginx, ensuring proper syntax and effective implementation of configuration changes without disrupting the web server’s functionality.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads