Share this page:
Virtual server files are stored in /etc/nginx/sites-available. When a site is enabled, a file system link is created from the virtual server file in /etc/nginx/sites-available to /etc/nginx/sites-enabled. Nginx looks for virtual server files in /etc/nginx/sites-enabled and uses links to access files in /etc/nginx/sites-available.
It's customary to name virtual server files after the site that they represent. If you have a site named www.example.com, you would put the settings in /etc/nginx/sites-available/www.example.com.conf. To enable the site, create a link to /etc/nginx/sites-enabled/:
To disable a site, simply delete the link from /etc/nginx/sites-enabled/:
Nginx needs to be restarted after a site has been enabled or disabled.
Server contexts contain the settings for a virtual server. The following directives are used in server contexts:
Locations are defined in server contexts, and they tell Nginx how to handle requests for specific URIs.
Locations often use the try_files directive to tell Nginx to try a succession of different methods for serving a page.
This makes Nginx look for a file matching the value stored in $uri. If the requested URL is http://example.com/foo.html, the value of $uri is /foo.html, so Nginx will look for a file called foo.html and serve it if it's available. If not, Nginx will check to see if there is a directory named /foo/. If no such directory exists, Nginx will default to serving a page called index.html.
In the following example, Nginx will default to redirecting the request to index.php, and pass the value of $uri as variable 'q':
A location can be defined for a specific page:
Locations can be defined for an entire directory:
The settings in this location will apply to any URI that starts with '/bar'
Access to locations can be allowed or denied according to the client's IP address. In this example, access is denied for IP address 192.168.1.4. This location can be accessed from all other IP addresses on subnet 192.168.1 (the '/24' means that only the first 24 bits of the IP address will be considered). Access from all other IP addresses is diabled.
Nginx supports the use of variables in its configuration files. Variables start with a '$'. This page contains a list of all the variables available in Nginx.
For example, if you're hosting several sites on a server, you can use the $host variable to append the host to the end of the root directive.
This is useful if /usr/share/nginx/www/ contains a directory for each site named after the site's domain name.