-2

I can not configure nginx to serve (redirect?) the www version of my site. The non-www version works perfectly. I suspect this is a dns issue, but I don't have enough experience to know that. Here is my working virtual host file that serves the non-www version of the site correctly.

server {
 listen 80;
 server_name  mysite.me;
 index index.html;
 root /opt/www/mysite.me/_site;

}

Also, when running any sudo commands I get this error

sudo: unable to resolve host www.mysite.com

I've also tried adding this block as well but it does not work for me either

server {
 listen 80;
 server_name  mysite.me;
 return 301 $scheme://www.mysite.me$request_uri;

}
5
  • What is the relashionship between mysite.me and www.mysite.com ? a typo ? The www version of your site should be resolved by your DNS. If it is not the case, fix this first.
    – krisFR
    Apr 9, 2014 at 1:05
  • @krisFR I don't really understand DNS. Are you saying that I need to change a setting with my host provider?
    – user215636
    Apr 9, 2014 at 2:19
  • No, first i would like to understand the relashionship between mysite.me and www.mysite.com. Can you explain ?
    – krisFR
    Apr 9, 2014 at 2:21
  • I don't really know how to explain the difference. I didn't know the www version wasn't working until I tried it on a whim. What should I go read to understand what's going on?
    – user215636
    Apr 9, 2014 at 3:01
  • Ho no please god help me !! ;) Let's say i will guess for you : you have http://mysite.me that works. Now you want http://www.mysite.me work also, is that right ?? assuming i am right, please post the results from these commands : nslookup mysite.me and nslookup www.mysite.me
    – krisFR
    Apr 9, 2014 at 3:10

4 Answers 4

1

from docs:

server {
    listen       80;
    server_name  example.org;
    return       301 http://www.example.org$request_uri;
}

server {
    listen       80;
    server_name  www.example.org;
    ...
}
0

Others have answers specific to Nginx, but as far as debugging goes, my bit of advice is to use curl with the -I option to see what the headers are for each domain. As the curl documentation for -I explains:

(HTTP) Include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more...

For example, doing a curl -I to google.com returns this:

curl -I google.com

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Wed, 09 Apr 2014 00:45:23 GMT
Expires: Fri, 09 May 2014 00:45:23 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic

The two key things you can get from this is the HTTP/1.1 response code which is a 301 Moved Permanently and the Location: which is connected to the HTTP/1.1 response code and sets the new location URL to be http://www.google.com/. Now, let’s do a curl -I to www.google.com like so:

curl -I www.google.com

HTTP/1.1 200 OK
Date: Wed, 09 Apr 2014 00:47:28 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=79631a82c2c7cf67:FF=0:TM=1397004448:LM=1397004448:S=z010eeEdNjG129tJ; expires=Fri, 08-Apr-2016 00:47:28 GMT; path=/; domain=.google.com
Set-Cookie: NID=67=e9j_a0lvogCKF-Mt-OdZKlRoMwaGJXis7yA0i0l1zKuh2mz_P5MUCsyzvPVhz9yBY2Wi04XzY2tn8UlqaT2i-rvVHGIAb1l1FwXm4wxvLtNTmSvREavLD6qDLeDBar2r; expires=Thu, 09-Oct-2014 00:47:28 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
Transfer-Encoding: chunked

Note the HTTP/1.1 response code is 200 OK, which means the URL is good & will load from that point on.

So in your case compare the header output for curl -I mysite.me to curl -I www.mysite.me and you can debug what’s happening.

2
  • when I use curl the response from www.mysite.me is: Could not resolve host: www.mysite.me
    – user215636
    Apr 9, 2014 at 2:15
  • Then you have a DNS issue. Apr 9, 2014 at 2:27
0

Check the output of dig mysite.com and dig www.mysite.com. If they both don't return the IP address of your server, then your DNS settings are wrong, and you need to create an appropriate A record entry for the missing domain.

After that, you can work with @Midimo example to get the server to show correct pages.

2
  • Why are you so sure it is a DNS issue & not a config issue? Apr 9, 2014 at 0:43
  • 1
    I looked at the sudo commands part in the original question, and that hinted also to DNS problems. It could also be server DNS resolver problems. Apr 9, 2014 at 0:50
0

I had a DNS issue. I added a catchall route ( * ) to the CNAME record and it works now. Here is my server block.

server {
 listen 80;
 server_name www.mysite.me;
 root /opt/www/mysite.me/_site;
 return 301 $scheme://mysite.me$request_uri;
}
server {
 listen 80;
 server_name  mysite.me;

 root /opt/www/mysite.me/_site;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .