# Name based virtual hosts not working

**URL:** <https://www.ruby-forum.com/t/name-based-virtual-hosts-not-working/244261>\
**Category:** NGINX\
**Created:** [March 12, 2016, 6:41am UTC](https://www.ruby-forum.com/t/name-based-virtual-hosts-not-working/244261 "2016-03-12T06:41:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [March 12, 2016, 6:41am UTC](https://www.ruby-forum.com/t/name-based-virtual-hosts-not-working/244261/1 "2016-03-12T06:41:23Z")

</div>

Hi!

I just did a lemp setup but I can’t get my virtual hosts to work  
properly.  
I´m totally lost and out of ideas.  
Two domains and one starting on e is always responding showing its root  
and  
the other starting on s is just dead. I understand that nginx is  
handling  
this as Apache analyzing the headers for a match and if no match it goes  
alphabetically taking the first as default domain.

I have my configuration files in /etc/nginx/conf.d, domain1.conf and  
domain2.conf and the root pointing to /var/www/domain1/html and  
/var/www/domain2/html  
The domains are pointed A records to my public IP

What can be wrong here? So many people having this problem but I can´t  
find  
a solution. I hope this forum are holding cutting edge professionals to  
help  
me out.

config file

server {  
listen 80;

```
root /var/www/domain1/html;
index index.php index.html index.htm;

server_name domain1.com www.domain1.com;

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

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
fastcgi\_pass unix:/var/run/php5-fpm.sock;  
}  
}

/zzyber

Posted at Nginx Forum:

> **[Name based virtual hosts not working](https://forum.nginx.org/read.php?2,265259,265259#msg-265259)**
>
> Hi! I just did a lemp setup but I can't get my virtual hosts to work properly. I´m totally lost and out of ideas. Two domains and one starting on e is always responding showing its root and the other starting on s is just dead. I understand that...

---

<div class="post-metadata">

**Author:** ![zzyber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8baadc/32.png) [@zzyber](https://www.ruby-forum.com/u/zzyber)\
**Post date:** [March 12, 2016, 8:36am UTC](https://www.ruby-forum.com/t/name-based-virtual-hosts-not-working/244261/2 "2016-03-12T08:36:28Z")

</div>

Can you try

listen IP:80;

instead of

listen 80;

where IP is the IP address both domains resolve to ( and I assume its  
the  
same IP).

---

<div class="post-metadata">

**Author:** ![zzyber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8baadc/32.png) [@zzyber](https://www.ruby-forum.com/u/zzyber)\
**Post date:** [March 12, 2016, 6:00pm UTC](https://www.ruby-forum.com/t/name-based-virtual-hosts-not-working/244261/3 "2016-03-12T18:00:11Z")

</div>

Hey.

I am using a setup with a bunch of domains - where I am just using the  
server\_name property.

As far as I can see, your config looks ok. But you have to keep in mind  
one thing: the “default” property, AND if it is being accessed through a  
proxy. That can - I am no expert! - possibly be a cause.

So in general, first delete - or change - the sites-enabled/default  
file. Remove the “default” keyword in the “listen” entry. Why? Well, it  
tends to cause NGINX to prefer one domain over another, and afaik,  
having multiple of these can even lead to an error, or unexpected  
behavior. Then, create files for your domains in the sites-available  
folder, and symlink them to sites-enabled. I wish there was a tool for  
that honestly… But first creating your configs in sites-available gives  
you a listing of all the sites you could offer. Again, check for the  
default keyword. Then symlink them - with their full path, not relative!

- into sites-enabled.

You should end up with - for example - two files:

sites-enabled/domain1.com  
sites-enabled/domain2.com

The both files should start similarily. Like so:

server {  
listen 80;  
server\_name [domain1.com](http://domain1.com) [www.domain2.com](http://www.domain2.com)

# … other stuff …

}

Just that the file for [domain2.com](http://domain2.com) obviously has a different  
server\_name.

NGINX has a handy tool to check your config. I strongly recommend doing  
so _before_ restarting. Because a full-blown reconfiguration is better  
applied by a restart, thats at least how it went for me.

If your config is good, restart the service - and try again! 🙂

Kind regards,  
Ingwie.
