Name based virtual hosts not working


I just did a lemp setup but I can’t get my virtual hosts to work
I´m totally lost and out of ideas.
Two domains and one starting on e is always responding showing its root
the other starting on s is just dead. I understand that nginx is
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
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
a solution. I hope this forum are holding cutting edge professionals to
me out.

config file

server {
listen 80;

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


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

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

include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;


Posted at Nginx Forum:

Can you try

listen IP:80;

instead of

listen 80;

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


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:


The both files should start similarily. Like so:

server {
listen 80;

… other stuff …


Just that the file for obviously has a different

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! :slight_smile:

Kind regards,