server variable here and define it (as you will do in the “Virtual
Hosts” section), instead of the include, but if you have multiple
sites, configuring how Nginx works for each site individually can
be useful, especially if you have one site that has a lot of traffic and
another that has little.
Whenever you make a change to the nginx.conf file, you must restart
Nginx to reload the configuration into Nginx for the changes to take effect,
like this:
Click here to view code image
matthew@seymour:~$ sudo service nginx restart
Some prefer to stop and start it, perhaps doing the configuration work in
between. This is quite common as in the past Nginx had a habit of not
performing restarts perfectly. In my experience, bad resets are rare, but using
this method prevents any doubt:
Click here to view code image
matthew@seymour:~$ sudo service nginx stop
matthew@seymour:~$ sudo service nginx start
Virtual Hosting
One of the most popular services to provide with a web server is to host a
virtual domain. Also known as a virtual host, a virtual domain is a complete
website with its own domain name, as if it were a standalone machine, but it’s
hosted on the same machine as other websites.
Nginx implements this capability in a simple way: Just create a configuration
file for your virtual host, name it for that host, and place it in
/etc/nginx/sites-enabled. We prefer to place our files in
/etc/nginx/sites-available and then create a symlink in sites-
enabled, but that is not a requirement. Doing so does allow you to disable
one out of several sites by simply deleting the symlink and reloading the
configuration or restarting Nginx while preserving the complete configuration
for the site, so we recommend you give it a try.
You can place the files for your website wherever you like. The configuration
files for Nginx tell the web server where to find them. We like to create them
in the main website creator’s/maintainer’s directory, but others prefer
/var/www, and still others opt for a different location. Choose a location
you like and make a note of it. You will need it shortly.
Here is an example of a file for a virtual host, which we will call