sim4biz
By:
sim4biz

Second domain on same droplet NOT working with NginX

August 2, 2014 1.9k views

I'm trying to use my NginX to host multiple websites on the same droplet.
I can access test.domain1.com and domain1.com with or without the use of default_server tag. However, accessing domain2.com just presents the contents of domain1.com (the server block with the default_server tag) without forwarding the url to domain1.com in the browser.
I've checked for typo in my configs but can't find one. Secondly, neither the access.log nor the error.log have any issues.

The hostname of my droplet is domain1.com.
I've created 2 DNS entries: 1 for domain1.com and the other for domain2.com. Both have the same A and CNAME records.

@      A      ip.add.re.ss
www    CNAME  @
*      CNAME  @

My configs are:
/etc/nginx/nginx.conf:
http://pastebin.com/AWf5EM66
/etc/nginx/global/wordpress.conf:
http://pastebin.com/Uc9KSqhh
/etc/nginx/global/restrictions.conf:
http://pastebin.com/cLfMv6jC
/etc/nginx/sites-available/sites.conf:
http://pastebin.com/tVMSatBJ

Any clues?

3 Answers

Read it first: Virtual Host Nginx: Ubuntu or Virtual Host Nginx: CentOS
And do the same with the next domain.

Configuration servername www.domain.com and servername www.domain2.com must be separated.

On your pastebin, you must remove servername www.domain2.com first and make a second configuration for the servername www.domain2.com.

And add your second/next domain on your hosts. Then edit /etc/hosts. Make it look like this:

sudo nano /etc/hosts

127.0.0.1       localhost.localdomain   localhost
192.168.0.100   test.domain.com
192.168.0.100   www.domain2.com

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
by Etel Sverdlov
This tutorial shows you how to set up virtual hosts. Virtual Hosts are used to run more than one website or domain off of a single server and IP address. When used with nginx, virtual hosts are called Server Blocks. This tutorial covers setting up server blocks on a Centos server.
  • As an addition to @ikoardian's answer (thank you by the way), don't forget to clear the cache in your browser once you have made the change, as sometimes it will look like it hasn't worked.

Thanks for replying. The problem was that I only had www.domain2.com in server_name in the site vhost but I was trying to access it without www.
I now added the extra server block with return directive to take care of non-www.

Have another answer? Share your knowledge.