list nginx virtual hosts

How to List All Virtual Hosts in NGINX

NGINX is a popular web server that allows you to easily build and manage websites. It supports high traffic and delivers fast performance. In fact, it allows you to run multiple websites from a single server using virtual hosts. By creating separate virtual host for each domain or host, you can easily host multiple websites in a single server. As you keep adding more and more virtual hosts in NGINX server, you may get confused about the virtual hosts on your system. In this article, we will learn how to list all virtual hosts in NGINX.


How to List All Virtual Hosts in NGINX

Here are the steps to list all virtual hosts in NGINX. There are several ways to do this in NGINX.

Starting from NGINX 1.9.2 you can run following command to show complete NGINX configuration.

$ nginx -T

If you only want to get all server names, you can run the following command. Each virtual host is specified in NGINX using server_name directive.

$ nginx -T | grep "server_name "

Each virtual host’s configuration file needs to be placed at /etc/nginx/sites-enabled/ so that it is served by NGINX. So you can also run grep command on all files placed at this folder location.

$ grep server_name /etc/nginx/sites-enabled/* -RiI

Another way to do this is to use find command to go through the configuration files in /etc/nginx/sites-enabled to search for server_name directive in them.

$ find /etc/nginx/sites-enabled/ -type f -print0 | xargs -0 egrep '^(\s|\t)*server_name'

In this article, we have seen a few simple ways to list all virtual hosts in NGINX. It is important to keep a track of all virtual hosts supported by your server, and disable the ones that are not required. Otherwise, you are making your web server vulnerable to malicious attacks. Virtual hosts that are not maintained are often outdated and vulnerable to security exploits so it is important to enable and run only required virtual hosts on your server.

In fact, you can run the above commands as a a cron job automatically to get a list of virtual hosts on a regular basis.

Also read:

How to Rebuild RPM Database in CentOS/RHEL
How to Modify URL Without Reloading in JS
How to List All Virtual Environments in Python
How to Transfer MySQL Database from One Computer to Another
How to List Installed PHP Modules in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *