Question

Nginx multiple domains, www issue.

  • Posted on August 11, 2013
  • EcazAsked by Ecaz

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.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Replace <pre>server_name ecaz.net; </pre> with <pre>server_name ecaz.net www.ecaz.net; </pre> and restart nginx. That should fix it :]

Hi, <br> <br>I have the same problem and adding www.seconddomain.com to the server block didn’t work. <br> <br>My setup for DNS management is: <br>firstdomain.com <br>@ => IP address <br>* => @ <br> <br>seconddomain.com <br>@ => IP address <br>* => @ <br> <br>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) <br>server { <br> listen 80; <br> <br> server_name firstdomain.com www.firstdomain.com; <br> root /var/www/firstdomain; <br> <br> location / { <br> try_files $uri $uri/ /index.html; <br> } <br> <br> error_page 404 /404.html; <br> <br> error_page 500 502 503 504 /50x.html; <br> location = /50x.html { <br> root /usr/share/nginx/html; <br> } <br> <br> # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 <br> location ~ .php$ { <br> try_files $uri =404; <br> fastcgi_pass unix:/var/run/php5-fpm.sock; <br> fastcgi_index index.php; <br> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam
<br>e; <br> include fastcgi_params; <br> <br> } <br>} <br> <br>server { <br> listen 80; <br> <br> server_name seconddomain.com www.seconddomain.com; <br> root /var/www/seconddomain; <br> <br> location / { <br> try_files $uri $uri/ /index.html; <br> } <br> <br> error_page 404 /404.html; <br> <br> error_page 500 502 503 504 /50x.html; <br> location = /50x.html { <br> root /usr/share/nginx/html; <br> } <br> <br> # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 <br> location ~ .php$ { <br> try_files $uri =404; <br> fastcgi_pass unix:/var/run/php5-fpm.sock; <br> fastcgi_index index.php; <br> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam
<br>e; <br> include fastcgi_params; <br> <br> } <br>} <br> <br>server { <br> listen 80; <br> <br> server_name experimental.firstdomain.com; <br> root /var/www/experimental; <br> <br> location / { <br> try_files $uri $uri/ /index.html; <br> } <br> <br> error_page 404 /404.html; <br> <br> error_page 500 502 503 504 /50x.html; <br> location = /50x.html { <br> root /usr/share/nginx/html; <br> } <br> <br> # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 <br> location ~ .php$ { <br> try_files $uri =404; <br> fastcgi_pass unix:/var/run/php5-fpm.sock; <br> fastcgi_index index.php; <br> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam
<br>e; <br> include fastcgi_params; <br> <br> } <br>}

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: <br> <br>https://p.kk7.me/fokacajetu.nginx (Lines 2 and 3 are the most important bits of this config)