How to add a new nginx site

This post lists the step I use to set up sites under using Nginx. As a long time user of Apache web server, I am still new to setting up things under Nginx, and having the steps to follow will save me from having to look up the information each time.

Setup Steps

  • Make a directory structure under /var/www for the site files.
mkdir -p /var/www/sitename/html
  • Make sure permissions are correct.
chmod -R 755 /var/www/sitename
  • Create an index page, index.html in /var/www/sitename/html/.
<html>
    <head>
        <title>Welcome to Sitename</title>
    </head>
    <body>
        <h1>Success!  Sitename server block is working!</h1>
    </body>
</html>
  • Create a server block for Nginx that references the index and directory structure for sitename that were created previously by creating a file named sitename in /etc/nginx/sites-available.
server {
        listen 80;
        listen [::]:80;

        root /var/www/sitename/html;
        index index.html;

        location / {
                try_files $uri $uri/ =404;
        }
}
  • Enable created server block configuration file by creating symbolic link to it under /etc/nginx/sites-enabled.
ln -s /etc/nginx/sites-available/sitename /etc/nginx/sites-enabled/
  • Ensure nginx.conf has server_names_hash_bucket_size line uncommented and set to 64.
...
http {
    ...
    server_names_hash_bucket_size 64;
    ...
}
...
  • Test the Nginx configuration.
nginx -t
systemctl restart nginx