Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have an http block where I include virtual hosts for the different domains hosted on the same server. For each virtual host I do:

listen domain.com:80;

Now, domain2.com works fine. However, when I do www.domain2.com it shows the page for domain1.com!

How to properly configure nginx? Does this have something to do whether www is a CNAME or an A record?

share|improve this question

2 Answers 2

server {
 listen WHATEVER_IP:80;
 server_name domain2.com;
 ... rest of config for domain2.com goes here

server {
 listen WHATEVER_IP:80;
 server_name www.domain2.com;
 ... rest of config for www.domain2.com goes here

This will work whether the two IPs are the same or not. You can just use listen 80; if you don't need different servers on different IPs and everything is name-based.

share|improve this answer
Two server blocks for the same configuration? –  m33lky Nov 21 '11 at 20:23
You can also merge them and use one server block for each different configuration. –  David Schwartz Nov 21 '11 at 20:46
up vote 0 down vote accepted

It looks like you can do the following in a server block:

listen 80;
server_name domain.com www.domain.com;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.