Ecaz
By:
Ecaz

Nginx multiple domains, www issue.

August 11, 2013 7k views
I do apologize in advance if this has already been asked but I couldn't find anything related to it. I'm currently hosting two domains on my Ubuntu droplet, using Nginx. I've setup the DNS and the Virtual Host for each domain and it works. So domain1.com actually gives me the "first" domain and domain2.com gives me the second one. However, if I go to WWW.domain1.com I end up at the second domain, it doesn't actually matter what I type before the domain name and I still end up at the second one. I tried adding a CNAME to the first domain. CNAME: www => ecaz.net. But it still end up at the wrong domain. I created an A-record for each domain, maybe that is the issue? A Record: @ => Droplet IP And this is a snippet from my virtual host file, listen 80; server_name ecaz.net; root /srv/www/ecaz.net/www; index index.html index.php; location / { try_files $uri $uri/ /index.php$is_args$args; } It looks the same for the other domain, just a different server name. Any help would be appreciated, if you need more info let me know.
4 Answers
Replace
server_name ecaz.net; 
with
server_name ecaz.net www.ecaz.net; 
and restart nginx. That should fix it :]
Kamal, my saviour!
That fixes the www issues. I suppose it might be better to just alter and create virtual hosts instead of doing it the DNS-way, even if it is a bit more fancy ;).

How would I go about solving the other issue? For example, if someone types in "asdasd.ecaz.net" they are brought to the other domain and not to ecaz.net. Any way I can just "block" things like that?
Nginx sets the first virtualhost as the default one if you didn't create a default virtualhost. Here's my /etc/nginx/sites-enabled/default virtualhost:

https://p.kk7.me/fokacajetu.nginx (Lines 2 and 3 are the most important bits of this config)
Hi,

I have the same problem and adding www.seconddomain.com to the server block didn't work.

My setup for DNS management is:
firstdomain.com
@ => IP address
* => @

seconddomain.com
@ => IP address
* => @

And my server block declaratins are as follows (I added a subdomain to firstdomain.com which doesn't work either -- it gets me to firstdomain.com)
server {
listen 80;

server_name firstdomain.com www.firstdomain.com;
root /var/www/firstdomain;

location / {
try_files $uri $uri/ /index.html;
}

error_page 404 /404.html;

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

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam\
e;
include fastcgi_params;

}
}

server {
listen 80;

server_name seconddomain.com www.seconddomain.com;
root /var/www/seconddomain;

location / {
try_files $uri $uri/ /index.html;
}

error_page 404 /404.html;

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

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam\
e;
include fastcgi_params;

}
}

server {
listen 80;

server_name experimental.firstdomain.com;
root /var/www/experimental;

location / {
try_files $uri $uri/ /index.html;
}

error_page 404 /404.html;

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

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam\
e;
include fastcgi_params;

}
}
Have another answer? Share your knowledge.