Configure HTTP Nginx

Configure HTTP Nginx

by John Vincent


Posted on March 1, 2017


This is part of a series of discussions regarding Deploying to a Digital Ocean Droplet. For more details, please see Overview of johnvincent.io website

Configure HTTP Nginx

Create HTTP configurations for

  • www.johnvincent.io
  • www.test.johnvincent.io
  • www.ghost.johnvincent.io
  • www.linkedin.johnvincent.io

Configuration

Stop Nginx

sudo systemctl stop nginx

Remove default site

cd /etc/nginx/
sudo rm sites-enabled/default

Create directories for http and https configurations

cd /etc/nginx/sites-available
sudo mkdir http https

Configuration for each domain and subdomains

Create web server files

cd /var/www
sudo mkdir -p johnvincent.io/html/.well-known
sudo mkdir -p ghost/html/.well-known
sudo mkdir -p linkedin/html/.well-known
sudo mkdir -p test/html/.well-known

Note, the .well-known is for the SSL certs tasks.

For each domain/subdomain, create file

* sudo vi /var/www/{domain-name}/html/index.html
<html>
    <head>
        <title>Welcome to {{DOMAIN-NAME}}com!</title>
    </head>
    <body>
        <h1>Success!  The {{DOMAIN-NAME}} server block is working!</h1>
    </body>
</html>

Change the directory ownership

sudo chown -R jv:jv /var/www/{domain-name}/html

Change ownership of files

cd /var/www/{domain-name}/html
find . -type d -print0 | xargs -0 chmod 0755 # For directories
find . -type f -print0 | xargs -0 chmod 0644 # For files

Configure johnvincent.io

sudo vi /etc/nginx/sites-available/http/johnvincent.io
server {
  listen 80 default_server;
  listen [::]:80 default_server;

  server_name johnvincent.io www.johnvincent.io;
  root /var/www/johnvincent.io/html;
  index index.html;

  location / {
    try_files $uri $uri/ =404;
  }
  location ~ /.well-known {
    allow all;
  }
}

Configure test.com

sudo vi /etc/nginx/sites-available/http/test.com
server {
  listen 80;
  listen [::]:80;

  server_name test.johnvincent.io www.test.johnvincent.io;
  root /var/www/test.com/html;
  index index.html;

  location / {
    try_files $uri $uri/ =404;
  }
  location ~ /.well-known {
    allow all;
  }
}

Configure Ghost HTML Only

Configure a basic server to verify access via the subdomain.

sudo vi /etc/nginx/sites-available/http/ghost-just-html
server {
  listen 80;
  listen [::]:80;

  server_name ghost.johnvincent.io www.ghost.johnvincent.io;
  root /var/www/ghost/html;
  index index.html;

  location / {
    try_files $uri $uri/ =404;
  }
  location ~ /.well-known {
    allow all;
  }
}

Configure linkedin

sudo vi /etc/nginx/sites-available/http/linkedin
server {
  listen 80;
  listen [::]:80;

  server_name linkedin.johnvincent.io www.linkedin.johnvincent.io;
  root /var/www/linkedin/html;
  index index.html;

  location / {
    try_files $uri $uri/ =404;
  }
  location ~ /.well-known {
    allow all;
  }
}

Enable Server Blocks

Use symlinks as files in /etc/nginx/sites-enabled will be run by the server.

sudo ln -s /etc/nginx/sites-available/http/test.com /etc/nginx/sites-enabled/test.com
sudo ln -s /etc/nginx/sites-available/http/johnvincent.io /etc/nginx/sites-enabled/johnvincent.io
sudo ln -s /etc/nginx/sites-available/http/ghost-just-html /etc/nginx/sites-enabled/ghost
sudo ln -s /etc/nginx/sites-available/http/linkedin /etc/nginx/sites-enabled/linkedin

Test Server

Restart Nginx

sudo nginx -t
sudo systemctl restart nginx

Test from browser, now using port 80:

They all should be working.

http://www.johnvincent.io
http://johnvincent.io

http://www.test.johnvincent.io
http://test.johnvincent.io

http://www.ghost.johnvincent.io
http://ghost.johnvincent.io

http://www.linkedin.johnvincent.io
http://linkedin.johnvincent.io
```