Published 03/06/2017
Last Updated 20/11/2024

Assume we have 2 static websites site1.com and site1.com and would like to server them from a single server using nginx

Below is a simple config file that allow nginx servers 2 sites above (bonus server block to redirect from www to non-www address). This config is verified and tested on Ubuntu 14.04 with nginx/1.4.6

server {
    server_name  www.site1.com;
    rewrite ^(.*) <http://site1.com$1> permanent;
}

server {
    server_name  site1.com;
    listen       80;
    root /usr/share/nginx/html/site1;
    index index.html index.htm;
    access_log  /var/log/nginx/site1_access.log;
    error_log   /var/log/nginx/site1_error.log;
}

server {
    server_name  www.site2.com;
    rewrite ^(.*) <http://site2.com$1> permanent;
}

server {
    server_name  site2.com;
    listen       80;
    root /usr/share/nginx/html/site2;
    index index.html index.htm;
    access_log  /var/log/nginx/site2_access.log;
    error_log   /var/log/nginx/site2_error.log;
}

In practice we should create a dedicated config file for each site in /etc/nginx/sites-available and symlink them to /etc/nginx/sites-enabled