Configuring Websites with Nginx on a Mac

Configuring websites with Nginx requires you to add server blocks. These are blocks of configuration that are kept in individual files associated to each website.

Each configuration file should be kept in a sites-available directory. This directory should be located in the same directory level as the nginx.conf file.

As an example, let’s add some Nginx configuration for the domain. Within the sites-available directory, add a file and add the following configuration.

server {
        listen 8080;

        root /path/to/your/website;
        index index.php index.html index.htm index.nginx-debian.html;


        location / {
                try_files $uri $uri/ =404;

The website has been set to listen on port 8080, the same as the nginx.conf configuration file.

The root directive specifies the directory path to the website, so change this accordingly to where the website files are located.

The server_name directive has been set to match the domain name of the website we are configuring.

Note that for the index directive, an index.php has been defined for PHP applications.

Typically within the nginx.conf, an include directive is specified so that individual site configuration can be loaded.

Within nginx.conf, just before the closing http curly brace, add the the include:

include sites-enabled/*;

Note that here the sites-enabled directory is included, and not the sites-available directory that was created to contain the configuration.

Create a sites-enabled directory and create a symbolic link to the file.

$ sudo ln -s /usr/local/etc/nginx/sites-available/ /usr/local/etc/nginx/sites-enabled/

If you have not done so already, uncomment the location ~ \.php$ and the content within the nginx.conf file.

Because we’re testing, a real domain, a modification to the hosts file needs to be made to point the domain to use the localhost IP.

// /private/etc/hosts

Now check that the Nginx configuration is correct and restart the service using the following commands.

$ sudo nginx -t
$ sudo nginx -s reload

Assuming that you have existing PHP files within the website’s directory, you should notice that the site will load correctly when typing in into the address bar.