NGINX Catch All VHost


nginx catch all vhost

NGINX allows you to configure multiple virtual hosts on a single server. However, what about the virtual hosts that are not listed in your server config file? Instead of letting your server throw an error, it is advisable to configure a catch all virtual host for your website. Let us take a look at how to configure NGINX catch all VHost.

 

How to Configure NGINX Catch All VHost

Here are the steps to configure NGINX catch all VHost for your website/application.

 

1. Open NGINX config file

Open NGINX config file in a text editor. You will find NGINX config file at one of the following locations, depending on the type of installation


/etc/nginx/conf.d/default.conf
/etc/nginx/sites-enabled/default
/etc/nginx/nginx.conf

$ sudo vim /etc/nginx/nginx.conf

 

2. Setup NGINX Catch All VHost

Create a server block in your config file. User ‘server_name _’ and the keyword default_server in your server config.

server {

   listen 80 default_server;
   server_name _;

   root /var/www/default;
...
}

For HTTPS, use listen 443 ssl default_server;

The above server block will act as a catch all virtual host. If you are creating separate config files for virtual hosts, then place the above code in one of those files.

 

 

3. Reload NGINX web server

Test the config file to ensure there are no errors.

$ sudo nginx -t

 

If you get no error message, reload NGINX server to apply changes.

$ sudo service nginx reload

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!