Nginx has grown in popularity since its release due to its light-weight resource utilization and its ability to scale easily on minimal hardware. Nginx excels at serving static content quickly and is designed to pass dynamic requests off to other software that is better suited for those purposes. Nginx is often selected by administrators for its resource efficiency and responsiveness under load.
In Nginx 1.9+ there is no site-available or sites-enabled directories. The default configurations for the hosts are in the /etc/nginx/conf.d/default.conf directory. All we would need to do is use this default.conf file to create our own Virtual Hosts.
The default root directory for Nginx is /usr/share/nginx/html. We will be creating our projects in this directory to serve our websites.
It’s not necessary to put the projects in the Nginx default directory. Using Virtual Hosts we can place our prpjects anywhere we want. Just put the appropriate path for the root of your project. For eg: If we want to serve our site to serve from “/srv/public_html/site” then this should be the root of your project in the Virtual Host configuration file
Step 1: Create your projects
Step 2: Add your Virtual host configuration files.
Next we add the Virtual host files which will have the Virtual host infomation for the projects.
sudo cp /usr/nginx/conf.d/default.conf /usr/nginx/conf.d/site1.local.conf
sudo cp /usr/nginx/conf.d/default.conf /usr/nginx/conf.d/site2.local.conf
Step 3: Setup your Virtual Host
Open your Virtual host file for Site 1 and configure it.
sudo vim site1.local.conf
We need to make a couple of changes in this file.
- Change the server name to access your local project.
- Change the Document root to serve the project to Nginx.
Similarly, setup the Virtual host for Site 2.
The index section tell what is the type of the files that is allowed as the project’s index files. For PHP it will be index index.php
Step 4: Restart the Nginx server
You will need to restart the Nginx server to have the changes in effect.
sudo services nginx restart
Step 5: Add in the site entries in hosts file.
For localhosts you will have to add the server_name in the hosts file to access.
sudo vim /etc/hosts
You can add the local hosts details to this file. As long as that line is there, directing your browser toward, say, example.com will give you all the virtual host details for the corresponding IP address.
The last block can be used when you are woking on a VM or a Vagrant box. So, you can add the ip of the VM along with the host name to point your browser to the desired location and access with the Virtual Host.
Step 6: Access you projects with the Virtual Hosts in the browser.
Now, when you type in http://site1.local it should show up like this.
For Site Two, enter http://site2.local.
Creating More Virtual Hosts
To add more virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate domain name, and then creating and activating the new virtual host file.