1

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.

  • You're redirecting back to https can you post an example of your ssl server block and your site2 config – Drifter104 Apr 21 '17 at 12:25
  • I have no ssl server block.. And the site2.com in sites-available is exactly the same as above, with site2 replacing site1 and the two lines starting with listen end with default server. I was under the impression nginx needed at least one site to be the default server – jupiar Apr 21 '17 at 13:05
  • If you have no ssl server blocks you shouldn't be ending up on site2 after visiting any site, because nothing would be listening on port 443 You need to remove return 301 https://$server_name$request_uri; from your config, if you really don't have any ssl – Drifter104 Apr 21 '17 at 13:10
  • Ok. I deleted that line from the three config files, restarted nginx but still have the same problem. – jupiar Apr 21 '17 at 13:16
  • Did you clear your browser cache because of the previous 301 you will need to – Drifter104 Apr 21 '17 at 13:47
0

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.

  • I changed the three config files to the above specification, and alternated default_server across the three, it still redirected to which ever server had the default_server statement. I also removed the statement, and then nginx just redirected it to site1, which is being alphabetically first. – jupiar Apr 22 '17 at 12:14
  • My best guess is there's something outside your Nginx configuration causing the problem. This config is so simple it'd be difficult to get it wrong. Unfortunately servers are complex, and people (including myself) regularly make mistakes. – Tim Apr 22 '17 at 18:25
  • Maybe, but whats the most likely other cause? It is a freshly created VPS on digitalocean, installed nginx and ruby, and thats it. I also just uninstalled nginx and built it from source, but still with the same problem – jupiar Apr 23 '17 at 6:28
  • Not so much the "other cause", the 100 other things that could be wrong. It took me three months to set up my first Nginx server, doing it in my spare time. I did however spend the time to learn AWS, learn Linux, learn Nginx, experiment with Wordpress, experiment with CDNs, learn about certificates, etc. That's after working in IT for 20 years, but not in infrastructure. This stuff can be difficult. If you want it easy you're probably best hiring someone who's learned it. – Tim Apr 23 '17 at 8:56
0

The solution to this was not on the setup of the server itself, but was actually two things, they are:

  1. On the godaddy interface to add custom name servers pointing to ns1.digitalocean.com, ns2.digitalocean.com and ns3.digitalocean.com godaddy_screenshot
  2. On the digitalocean interface, under the networking tab add domains to the three websites. digitalocean_screenshot

Once this was done, and waiting for a few hours and all worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.