Nginx multiple domains

1.7k views Asked by At

I've been spending a few days trying to figure out how to setup multiple domains on my Ubuntu 14.04 server using nginx.

In my /etc/nginx/sites-available/default file I have:

#Domain + IP one.
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    root /var/www/alive.gg/html;
    index index.php index.html index.htm;
    #149.202.86.66 IP ONE
    server_name alive.gg www.alive.gg;

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

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }  
}

#Domain + IP two
server {
    listen 80;
    listen [::]:80;
    root /var/www/blazeplay.com/html;
    index index.php index.html index.htm;
    #213.186.35.171 IP TWO
    server_name blazeplay.com www.blazeplay.com;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

My /etc/network/interfaces file

auto eth0
iface eth0 inet static
    address 149.202.86.66
    netmask 255.255.255.0
    network 149.202.86.0
    broadcast 149.202.86.255
    gateway 149.202.86.254
    post-up /sbin/ifconfig eth0:0 213.186.35.147 netmask 255.255.255.255 br$
    post-down /sbin/ifconfig eth0:0 down
    post-up /sbin/ifconfig eth0:1 213.186.35.167 netmask 255.255.255.255 br$
    post-down /sbin/ifconfig eth0:1 down
    post-up /sbin/ifconfig eth0:2 213.186.35.171 netmask 255.255.255.255 br$
    post-down /sbin/ifconfig eth0:2 down

The last one being the 2nd IP I'm trying to use. (213.186.35.171)

I have redirected my domains DNS, example Blazeplay.com as so:

A   @    213.186.35.171

What happens now when i go to Blazeplay.com: I can view the correct page /var/www/blazeplay.com/html - however it cannot load CSS IMG etc, it throws a 404 error. And if I go to my wordpress login site /wp-admin, it redirects me to the correct IP, but when I hit enter it redirects me once again to the alive.gg domain. Aka the default_server, so I must have set something wrong since its turning to the default_server. and this is where I get lost on what to do. Because, if I replace the the server_name blazeplay.com www.blazeplay.com; with the IP address 213.186.35.171, instead of the domain everything loads properly with no issues.

Feel free to try the above, all the settings are still the same so if you head to the domain blazeplay.com/wp-admin and try to login the same thing should happen to you.

I'm not an Ubuntu or nginx expert, am I missing some small step to configure multiple domains, or can I do some sort of troubleshoot to locate my issue? Any help is greatly appreciated!

If you need any other information let me know. :-)

Thank you for reading.

2

There are 2 answers

0
Codey93 On

Jeeze, such time spent and all I misssed was a listen: ip:port; in each server block.

1
kwishnu On

As per this article, in virtual.conf (/etc/nginx/conf.d/virtual.conf) create server blocks for your URL’s:

server {
    listen 80;
       root /opt/htdocs/bestflare;
    index index.html index.htm;
       server_name bestflare.com;
       location / {
           try_files $uri $uri/ =404;
       }
    }
server {
       listen 80;
       root /opt/htdocs/usefulread;
       index index.html index.htm;
       server_name usefulread.com;
       location / {
           try_files $uri $uri/ =404;
       }
    }

changing the values for root, server_name based on your particulars. Hope this helps!