www in domain not working in nginx

DOA picture DOA · Mar 31, 2012 · Viewed 13.3k times · Source

I'm new to using nginx, well, new to using anything that's not cpanel... I'm having problems getting domains to work using nginx when you include www. in the url.

www.mydomain.com > not work 404
mydomain.com > works

I'm not sure if I have made mistake with named config files, or the server config for nginx. I'm kinda learning in a hurry and I'm not going to be surprised if I made some error with basic configuration. I run latest nginx & php-fpm, apart from my domain issue it works.

I'm (trying?) to run subdomains, they work, but using www. will result in a 404. I use nameservers etc from my main .org server domain. I'm going to post all that is relevant below in the hope someone here can spot the errors I am making/or made.

etc/hosts 
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1       localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
184.xxx.xxx.146 server.servername.org servername.org 

named.conf

   ... 
   view "localhost_resolver" {
/* This view sets up named to be a localhost resolver ( caching only nameserver ).
 * If all you want is a caching-only nameserver, then you need only define this view:
 */
   # match-clients         { 127.0.0.0/24; };
   # match-destinations    { localhost; };
   match-clients      { any; };
   match-destinations { any; };
   recursion no;

        zone "servername.org" {
                type master;
                file "/var/named/servername.org.db";
        };

// optional - we act as the slave (secondary) for the delegated domain
zone "mydomain.com" IN {
  type slave;
  file "/var/named/mydomain.com.db";
  masters {10.10.0.24;};
}; 
allow-notify { 184.xxx.xxx.146; };
};

mydomain.com.db

$TTL    86400
mydomain.com.  IN      SOA     ns1.servername.org.      server.servername.org.        (
                                2002012013; Serial
                                1H      ; Refresh (change 1H to 6H in 3 days or so)
                                1800    ; Retry (change to 1H in 3 days)
                                2W      ; Expire
                                1D ); Minimum
mydomain.com.          IN      NS      ns1.servername.org.
mydomain.com.          IN      NS      ns2.servername.org.
ns1.servername.org.              IN      A       184.xxx.xxx.147
ns2.servername.org.             IN      A       184.xxx.xxx.148
mail.servername.org.             IN      A       184.xxx.xxx.146
mydomain.com.          IN      A       184.xxx.xxx.146
mydomain.com.          IN      MX      0       mail.servername.org.
@                               A       184.xxx.xxx.146
www                            A       184.xxx.xxx.146

nginx.conf uses include /etc/nginx/sites-enabled/*; and the nginx "mydomain.com" config

server {
    server_name www.mydomain.com;
    rewrite ^(.*) http://mydomain.com$1 permanent;
}
server {
listen 80;
server_name mydomain.com www.mydomain.com;

   # access_log /srv/www/mydomain.com/logs/access.log;
    error_log /srv/www/mydomain.com/logs/error.log;
    root /srv/www/mydomain.com/public_html;
    set $noadmin 1;

    location / {
        try_files $uri $uri/ /index.php?$args;
        index index.html index.htm index.php;
    }

    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

     location ~ \.flv$ {
            flv;
            root /srv/www/mydomain.com/public_html;
     }

     location ~ \.mp4$ {
            root /srv/www/mydomain.com/public_html;
            mp4;
     }

     # use fastcgi for all php files
        location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/mydomain.com/public_html$fastcgi_script_name;
        include fastcgi_params;
   }

# deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }
}

I can access subdomains, so my horrible attempt at this seems to be kind of working, I am stuck on why www.mydomain.com will not connect, while http://mydomain.com will. I am reading/learning more as I go along, I do not want to make changes until I have understanding of what the changes do. I may end up breaking more then the URLs.

Answer

edigu picture edigu · Apr 1, 2012

You are rewriting www.domain.com on first few lines of nginx.conf. If i'm not wrong, rewriting and redirecting are different things. Try this on first server block;

server {
    server_name  www.mydomain.com;
    return       301 http://mydomain.com$request_uri;
}

and change

server_name mydomain.com www.mydomain.com

to

server_name mydomain.com

in second server block.