Nginx: multiple websites on one server

nginxUbuntuwebsite

Ive got three websites I wish to have served from one server. The server is an Ubuntu droplet on digitalocean, the websites are in my home directory in ~/site1 ~/site2 and ~/site3. I have nginx with stock nginx.conf. I have three files in sites-available (site1.com site2.com & site3.com) In each file they have the following structure according to each respective site:

server {
    listen 80;
    listen [::]:80;
    root /home/user/site1;
    index.html
    server_name site1.com *.site1.com
    return 301 https://$server_name$request_uri;
}

The three files are symlinked in sites-enabled and in both sites-enabled and sites-available the default file is deleted. Also, the site 2 has default_server appended to the end of the two listen lines

Nginx starts fine with no errors:

$ sudo nginx -t
$ service nginx restart

But when I go to the each respective website, they all direct me back the default_server site2 website. The website domains were bought on godaddy.com, and all point towards this server's IP.

How can I solve this problem so each website gets redirected properly? I'm fairly new to this side of things and self taught, so if at all possible a little explanation would help a lot.

The following places helped me get to the above:

Nginx block for multiple domains to redirect all traffic to https?

Domain redirects to wrong site in nginx, multiple sites in config files

https://www.digitalocean.com/community/questions/multiple-sites-using-nginx

https://stackoverflow.com/questions/11693135/multiple-websites-on-nginx-sites-available

EDIT:
The extra things I have tried now are:

  • Removed the return 301 https://$server_name$request_uri; line from each files server block.

  • Cleared the cache from multiple browsers and devices.

  • Changed the root path to /var/www/html/site1-2-3

  • Adding SERVERIP site1.com, SERVERIP site2.com, SERVERIP site3.com to /etc/hosts

Still no change in behavior and still redirecting to the default server

EDIT2:
The following are the full config files:

/etc/nginx/sites-available/site1.com

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

        root /home/deploy/site1;

        index index.html index.htm index.nginx-debian.html;

        server_name site1.com *.site1.com;

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

/etc/nginx/sites-available/site2.com

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

        root /home/deploy/site2/current/_site;

        index index.html index.htm index.nginx-debian.html;

        server_name site2.com *.site2.com;

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

/etc/nginx/sites-available/site3.com

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

        root /home/deploy/site3;

        index index.html index.htm index.nginx-debian.html;

        server_name site3.com *.site3.com;

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

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml app$

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

EDIT3
I noticed that the version of nginx was 1.10.3, which is not the most current, so i removed it with apt-get remove purge etc. Then I downloaded and built from source 1.12.0 with the whole ./configure, make, make install process, it depended on a few other packages, but once done and server blocks in place, included in nginx.conf, I still have the same problem, redirecting to the default_server.

Best Answer

I'm not confident this will help, but it's worth a shot. It's too difficult to format in a comment. If it doesn't work, tell me, I'll think some more or delete the answer.

It's basically a super simple config with nothing special.

server {
    server_name site1.com www.site1.com;
    listen 80;

    root /home/deploy/site1;
    try_files $uri $uri/ =404;

    index index.html index.htm index.nginx-debian.html;
}

Also, if it doesn't work, move your default_server statement to another server then check all three sites. I just want to make sure it changes as expected.

Related Topic