If you only need to host a static site or a single-page javascript app such as AngularJS-app, then the following Nginx-configuration is for you.

The following is a very basic configuration, a starting point. You can add more configuration options based on your needs. I won’t go to those options in this post.

nginx server logo

First make sure the nginx.conf in /etc/nginx/ includes the sites-enabled directory to itself in the http-block of the configuration. You should see the following somewhere in the file:

http{
  // lots of configurations here

  include /etc/nginx/sites-enabled/*;
}

I like to create site/server per file configurations. In another words, I create a single file for each site. Here we configure site called “hello-world”.

Create file /etc/site-available/hello-world. You can do this for example with vim:

sudo vim /etc/site-available/hello-world Then write the following to that file. The server will listen connection request coming to port 80. If hello-world.your.domain is requested, then the request is directed to the root. We also set what happens when the location / is requested.

server{

  listen 80;

  server_name hello-world.your.domain;
  set $host_path "/path/to/site/root";

  root $host_path;
  index index.html;

  location / {
    try_files $uri $uri/ /index.html =404;
  }
}

Now save the file. We still need to link the file to nginx’s sites-enabled directory with a symbolic link and restart the server so that the changes take affect.

Linking is done with ln command:

sudo ln -s /etc/site-available/hello-world /etc/site-enabled/hello-world

Finally we restart the server. You could also use the reload command instead of restart. Reload just loads the configurations again.

sudo service nginx restart