Learn about Centmin Mod LEMP Stack today
Register Now

Nginx Use www instead of non-www?

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by joshuah, Jul 3, 2017.

  1. joshuah

    joshuah Member

    121
    14
    18
    Apr 3, 2017
    Ratings:
    +17
    Local Time:
    9:29 AM
    Is it possible to use www.domain.tld instead of domain.tld? If so, any easy way to do that?

    Also, is it possible to add it to the installer option 22 etc.?

     
  2. eva2000

    eva2000 Administrator Staff Member

    53,229
    12,116
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,654
    Local Time:
    9:29 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    When you type domain.com, Centmin Mod sets up both www and non-www versions to work
     
  3. ZcatK

    ZcatK New Member

    7
    2
    3
    Jul 3, 2017
    Ratings:
    +2
    Local Time:
    2:29 AM
    1.13.2
    10
    Since Centmin Mod sets up both www and non-www versions, I beleive you would simply setup your DNS to point to the version you prefer. Something like:

    A Record: www.yourdomain.com ServerIP
    CNAME: yourdomain.com www
     
  4. eva2000

    eva2000 Administrator Staff Member

    53,229
    12,116
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,654
    Local Time:
    9:29 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    DNS A records for both www and non-www domains and just additional server {} context above your existing server {} context and in existing server {} context remove www domain from server_name directive
    Code (Text):
    # redirect from www to non-www
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
    server {
      listen   80;
      server_name www.domain.com;
      return 302 http://domain.com.com$request_uri;
    }
    
    server {
      listen   80;
      server_name domain.com;
    
    ... SNIPPED...
    }
    

    then once all tested working change 302 temp redirect to 301 permanment redirect.

    see How to Create NGINX Rewrite Rules | NGINX
     
    Last edited: Jul 4, 2017
  5. ZcatK

    ZcatK New Member

    7
    2
    3
    Jul 3, 2017
    Ratings:
    +2
    Local Time:
    2:29 AM
    1.13.2
    10
    Ignore everything below. The answer is yes it is the SSL conf and the current code already redirects to non-www. I removed the CNAME in DNS, added a www A record, and modified the .conf from a 302 to 301 redirect. Everything works perfectly.

    Is that the SSL config for the domain?

    Current:
    Code:
    #x# HTTPS-DEFAULT
     server {
    
       server_name domain.com www.domain.com;
       return 302 https://$server_name$request_uri;
       include /usr/local/nginx/conf/staticfiles.conf;
     }
    
    server {
      listen 443 ssl http2;
      server_name domain.com www.domain.com;
    
      include /usr/local/nginx/conf/ssl/domain.com/domain.com.crt.key.conf;
      include /usr/local/nginx/conf/ssl_include.conf;
    
    If I want non-www should I then modify the code to the following:
    Code:
    #x# HTTPS-DEFAULT
     server {
    
       server_name domain.com;
       return 302 https://www.domain.com.com$request_uri;
       include /usr/local/nginx/conf/staticfiles.conf;
     }
    
    server {
      listen 443 ssl http2;
      server_name domain.com;
    
      include /usr/local/nginx/conf/ssl/domain.com/domain.com.crt.key.conf;
      include /usr/local/nginx/conf/ssl_include.conf;
    
    Is this better/faster than using a DNS CNAME?
     
    Last edited: Jul 4, 2017
  6. eva2000

    eva2000 Administrator Staff Member

    53,229
    12,116
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,654
    Local Time:
    9:29 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    for http to https redirect see Nginx Vhost & NSD DNS Setup - CentminMod.com LEMP Nginx web stack for CentOS

    so for non-www redirect add one more server context
    Code (Text):
    #x# HTTPS-DEFAULT
     # redirect http://www.domain.com to https://domain.com
     server {
    
       server_name www.domain.com;
       return 302 https://domain.com.com$request_uri;
       include /usr/local/nginx/conf/staticfiles.conf;
     }
     # redirect http://domain.com to https://domain.com
     server {
    
       server_name domain.com;
       return 302 https://domain.com.com$request_uri;
       include /usr/local/nginx/conf/staticfiles.conf;
     }
    
    server {
      listen 443 ssl http2;
      server_name domain.com;
    
      include /usr/local/nginx/conf/ssl/domain.com/domain.com.crt.key.conf;
      include /usr/local/nginx/conf/ssl_include.conf;

    Not faster or slower than CNAME, it's nginx way of forcing https