On the one hand we've got the web server, Nginx. On the other hand we've got the site, made up of a bunch of files.
What we need is to create a relationship so that, when someone queries your domain name, on your server's web port which is controlled by Nginx, Nginx knows what to do, dishing up the site.
Put it another way, we need to create a simple file – a virtual host file – specifying some criteria for your site, adding that to a place Nginx keeps tabs on and, then for good measure, we'll flick a switch to enable the file adn, in turn, your site.
Got it? Good.
First up, let's assume root:-
Nginx Virtual Host Files: Theory
On the server, Nginx lives here:-
The virtual host files for your site or sites live here:-
Take a look:-
And you'll see there's a default vhost in there already. That's cool, we'll use it as a template.
So, let's say you plan to host two sites on this server. You'd be wanting two configuration files in the sites-available folder, looking like this:-
(If you've only got the one site, in this tutorial, ignore the second one! If you've got more, add some more.)
Then there's that switch I mentioned. Each site's virtual host is only activated when its corresponding link is added, kind of like creating a circuit. That link goes here:-
… We don't create anything manually in that directory. Instead, WHEN you have your virtual host file in place, you create the link for each site like this:-
Nginx Virtual Host Files: Configuration
OK, let's copy the default virtual host file, in the sites-available folder, to a new vhost for your site. Just change this site name, site1.com, to something appropriate:-
And open the new file:-
You'll see a server block, something like this:-
We need to change three values:-
Look for this line near the top of the file:-
If it's commented using a # like this …
… then just lose the comment, changing to:-
That's what we want – listen 80; – which tells Nginx to listen for queries for this site on your server's default HTTP port, which is 80.
Identify Your Domain
Now look for this:-
Just change example.com to your domain name. Now Nginx knows that this configuration file refers to your specified domain … which you probably guessed.
Specify the Web Files
Finally, having received a request on port 80 for yourSite.com, Nginx needs to know where the files live. They may well be in a complex folder hierarchy which, in turn, may well link to a separate database too. However simple or complex you site may be, there will always be a core folder … what we refer to as the root folder, and it's that folder that we pinpoint here.
Look for the root directive:-
And, editing USERNAME to the name of the user we already setup, and swapping the domain name to yours, change the above path to the root folder of your site:-
Fine. Save and exit the file.
Nginx Virtual Host Files: Enabling
Enabling a site, as I've explained, is as simple as creating a link – what we call a symbolic link or symlink – from your new virtual host file in the sites-available folder. The link itself goes in the sites-enabled folder.
Editing the site name, do this:-
The new site won't be recognised by Nginx until the web server is restarted:-
And quit root's superpowers:-