142

I would like to host 2 different domains in the same server using Nginx. I redirected both domains to this host via @ property. Although I configure 2 different server blocks, whenever I try to access second domain, it redirects to first one.

Here is my config.

server {
    listen      `www.domain1.example:80`;
    access_log  `/var/log/nginx/host.domain1.access.log`  main;
    root `/var/www/domain1`;
    server_name `www.domain1.example`;

    location ~ \.php$ {
        # Security: must set cgi.fixpathinfo to 0 in `php.ini`!
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass `127.0.0.1:9000`;
        fastcgi_index `index.php`;
        fastcgi_param SCRIPT_FILENAME         $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include `/etc/nginx/fastcgi_params`;
    }
}

server {
    listen       www.domain2.example:80;
    access_log  /var/log/nginx/host.domain2.access.log  main;
    root /var/www/domain2;
    server_name www.domain2.example;

    location ~ \.php$ {
        # Security: must set cgi.fixpathinfo to 0 in php.ini!
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME         $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include /etc/nginx/fastcgi_params;
    }
}

How can I fix this?

1 Answer 1

183

Your "listen" directives are wrong. See this page: http://nginx.org/en/docs/http/server_names.html.

They should be

server {
    listen      80;
    server_name www.domain1.example;
    root /var/www/domain1;
}

server {
    listen       80;
    server_name www.domain2.example;
    root /var/www/domain2;
}

Note, I have only included the relevant lines. Everything else looked okay but I just deleted it for clarity. To test it you might want to try serving a text file from each server first before actually serving PHP. That's why I left the 'root' directive in there.

10
  • 10
    This fixed it for me. The problem seems to have been that in both of my server {} blocks, the sever_name directives were wildcards: .domain1.com and .domain2.com. Changing them to server_name www.domain1.com domain1.com; and server_name www.domain2.com domain2.com; now has the correct page for each site displaying when those addresses are used.
    – Steve HHH
    Dec 14, 2012 at 17:12
  • 4
    I know this is dredging up an ancient comment. But I assume that these two server blocks could be in separate config files in sites-enabled/ ?
    – labarna
    Jun 28, 2013 at 14:46
  • 4
    Absolutely, it just depends on how you want to structure your config. I tend to have one file per real domain. Each of which might contain multiple server blocks.
    – aychedee
    Jun 28, 2013 at 15:19
  • 5
    You REALLY don't want to do that. Pick either www.domain.com or domain.com and redirect one to the other. Having the same content at two different addresses is seen as spammy and can heavily damage your search ranking.
    – aychedee
    Jan 1, 2014 at 18:30
  • 6
    You can also just use .domain.com. It's a special nginx wildcard that matches domain.com and *.domain.com. See: nginx.org/en/docs/http/server_names.html
    – aychedee
    Jan 1, 2014 at 20:54

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.