networking, web

Multiple domains on NGINX

Simple configuration which will allow to handle more than one domain by server using NGINX. Edit config files as following:

/etc/nginx/sites-available/first-domain.com

server {
        listen [::]:80;
        listen [::]:443;

        root /var/www/html/first-domain/public;

        server_name first-domain.com www.first-domain.com;

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

/etc/nginx/sites-available/second-domain.com

server {
        listen [::]:80;
        listen [::]:443;

        root /var/www/html/second-domain/public;

        server_name second-domain.com www.second-domain.com;

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

It is possible to put above two files in /etc/nginx/sites-enabled, but I prefer to create symlinks to it.

$ sudo ln -s /etc/nginx/sites-available/first-domain.com /etc/nginx/sites-enabled/first-domain.com
$ sudo ln -s /etc/nginx/sites-available/second-domain.com /etc/nginx/sites-enabled/second-domain.com

Include those config files in main /etc/nginx/nginx.conf:

http {
    include /etc/nginx/sites-enabled/*;
    (...)
}

Check if everything is OK, restart the server and it is done!

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ sudo systemctl restart nginx