15

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.

DOA
  • 362
  • 1
  • 2
  • 10

2 Answers2

14

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.

edigu
  • 9,878
  • 5
  • 57
  • 80
  • Thanks :) removing the www from the second server block did the trick for me, the rewrite at the top is so anyone using www. gets sent to http// instead, and that is also working now, although I found I can make that more efficient. Thanks for your help! http://wiki.nginx.org/Pitfalls#Taxing_Rewrites – DOA Apr 02 '12 at 05:14
  • sometimes adding more server_name directives may require you to increase `http{ server_names_hash_bucket_size NN }` where NN is a number >= 32 – nurettin May 24 '15 at 05:41
3

my solution and it works for me

server {
    listen 80;
    server_name yourdomainname.com www.yourdomainname.com;
    return 301 https://$server_name$request_uri;
}

write the previous code inside that file --> yourdomainname.conf

nginx

Community
  • 1
  • 1