Join the community today
Register Now

www to non-www redirect not working

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by Veer, Sep 3, 2021.

  1. Veer

    Veer New Member

    10
    0
    1
    Oct 23, 2019
    Ratings:
    +1
    Local Time:
    6:57 AM
    1.19.1
    10.3.23
    • CentOS Version: CentOS - 7.8.2003 x86_64
    • Centmin Mod Version Installed: 123.09beta01
    • Nginx Version Installed: 1.19.1
    • PHP Version Installed: 7.2.x

    I tried following the guide: Nginx Vhost & NSD DNS Setup - CentminMod.com LEMP Nginx web stack for CentOS
    but end up with this error in browser
    Code:
    This page isn’t working right now
    www.domain.com redirected you too many times.
    To fix this issue, try clearing your cookies.
    ERR_TOO_MANY_REDIRECTS
    
    This is my domain.com.ssl.conf file right now, If anyone can point out what is wrong here, it'd be great help :)


    Code:
    # Centmin Mod Getting Started Guide
    # must read http://centminmod.com/getstarted.html
    # For HTTP/2 SSL Setup
    # read http://centminmod.com/nginx_configure_https_ssl_spdy.html
    
    # redirect from www to non-www  forced SSL
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
    server {
           listen   80;
           server_name mydomain.com www.mydomain.com;
           return 302 https://mydomain.com$request_uri;
    }
    
    server {
      listen 443 ssl http2;
      server_name www.mydomain.com;
    
      ssl_dhparam /usr/local/nginx/conf/ssl/mydomain.com/dhparam.pem;
      ssl_certificate      /usr/local/nginx/conf/ssl/mydomain.com/mydomain.com.crt;
      ssl_certificate_key  /usr/local/nginx/conf/ssl/mydomain.com/mydomain.com.key;
      include /usr/local/nginx/conf/ssl_include.conf;
     
      return 301 https://mydomain.com$request_uri;
    }
    
    server {
      listen 443 ssl http2;
      server_name mydomain.com;
     
      ssl_dhparam /usr/local/nginx/conf/ssl/mydomain.com/dhparam.pem;
      ssl_certificate      /usr/local/nginx/conf/ssl/mydomain.com/mydomain.com.crt;
      ssl_certificate_key  /usr/local/nginx/conf/ssl/mydomain.com/mydomain.com.key;
      include /usr/local/nginx/conf/ssl_include.conf;
     
    
      # cloudflare authenticated origin pull cert community.centminmod.com/threads/13847/
      #ssl_client_certificate /usr/local/nginx/conf/ssl/cloudflare/mydomain.com/origin.crt;
      #ssl_verify_client on;
      http2_max_field_size 16k;
      http2_max_header_size 32k;
      http2_max_requests 50000;
      # mozilla recommended
      ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS;
      ssl_prefer_server_ciphers   on;
      #add_header Alternate-Protocol  443:npn-spdy/3;
    
      # before enabling HSTS line below read centminmod.com/nginx_domain_dns_setup.html#hsts
      #add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
      #add_header X-Frame-Options SAMEORIGIN;
      add_header X-Xss-Protection "1; mode=block" always;
      add_header X-Content-Type-Options "nosniff" always;
      #add_header Referrer-Policy "strict-origin-when-cross-origin";
      #add_header Feature-Policy "accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'";
      #spdy_headers_comp 5;
      ssl_buffer_size 1369;
      ssl_session_tickets on;
     
      # enable ocsp stapling
      #resolver 8.8.8.8 8.8.4.4 1.1.1.1 1.0.0.1 valid=10m;
      #resolver_timeout 10s;
      #ssl_stapling on;
      #ssl_stapling_verify on;
      #ssl_trusted_certificate /usr/local/nginx/conf/ssl/mydomain.com/mydomain.com-trusted.crt;
    
    # ngx_pagespeed & ngx_pagespeed handler
    #include /usr/local/nginx/conf/pagespeed.conf;
    #include /usr/local/nginx/conf/pagespeedhandler.conf;
    #include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/mydomain.com/log/access.log combined buffer=256k flush=5m;
      error_log /home/nginx/domains/mydomain.com/log/error.log;
    
      include /usr/local/nginx/conf/autoprotect/mydomain.com/autoprotect-mydomain.com.conf;
      root /home/nginx/domains/mydomain.com/public;
      # uncomment cloudflare.conf include if using cloudflare for
      # server and/or vhost site
      #include /usr/local/nginx/conf/cloudflare.conf;
      include /usr/local/nginx/conf/503include-main.conf;
    
      location / {
      include /usr/local/nginx/conf/503include-only.conf;
     
     
        ### Start WordPress
       
        location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
        }
       
        location ~ ^/(wp-login.php) {
            include /usr/local/nginx/conf/php.conf;
            auth_basic "Private";
            auth_basic_user_file /usr/local/nginx/conf/htpasswd_admin_php;
        }
       
        location ~ ^/(wp-login.php) {
            include /usr/local/nginx/conf/php.conf;
            auth_basic "Private";
            auth_basic_user_file /usr/local/nginx/conf/htpasswd_admin_php;
        }
       
        ### End WordPress
       
    
    # block common exploits, sql injections etc
    #include /usr/local/nginx/conf/block.conf;
    
      # Enables directory listings when index file not found
      #autoindex  on;
    
      # Shows file listing times as local time
      #autoindex_localtime on;
    
      # Wordpress Permalinks example
      #try_files $uri $uri/ /index.php?q=$uri&$args;
    
      }
    
      include /usr/local/nginx/conf/pre-staticfiles-local-mydomain.com.conf;
      include /usr/local/nginx/conf/pre-staticfiles-global.conf;
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
     
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    }
    
     
    Last edited: Sep 3, 2021
  2. Veer

    Veer New Member

    10
    0
    1
    Oct 23, 2019
    Ratings:
    +1
    Local Time:
    6:57 AM
    1.19.1
    10.3.23
    If I use the code below, it redirect non-www to www :| but I expect it to redirect all to non-www, as I have another domain config with this code and perfectly redirecting to non-www
    Code:
    server {
           listen   80;
           server_name mydomain.com www.mydomain.com;
           return 302 https://mydomain.com$request_uri;
    }
    
    server {
      listen 443 ssl http2;
      server_name mydomain.com www.mydomain.com;
    
      ssl_dhparam /usr/local/nginx/conf/ssl/mydomain.com/dhparam.pem;
      ssl_certificate      /usr/local/nginx/conf/ssl/mydomain.com/mydomain.com.crt;
      ssl_certificate_key  /usr/local/nginx/conf/ssl/mydomain.com/mydomain.com.key;
      include /usr/local/nginx/conf/ssl_include.conf;
     
  3. eva2000

    eva2000 Administrator Staff Member

    47,202
    10,670
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,575
    Local Time:
    11:57 AM
    Nginx 1.19.x
    MariaDB 5.5/10.x

    You can test in SSH via curl to check headers for location field (where the redirect goes) using the following commands:
    Code (Text):
    curl -I http://domain.com
    

    Code (Text):
    curl -I http://www.domain.com
    

    and
    Code (Text):
    curl -I https://domain.com
    

    Code (Text):
    curl -I https://www.domain.com
    

    for posting code or output from commands to keep the formatting, you might want to use CODE tags for code How to use forum BBCODE code tags :)
     
  4. Veer

    Veer New Member

    10
    0
    1
    Oct 23, 2019
    Ratings:
    +1
    Local Time:
    6:57 AM
    1.19.1
    10.3.23
    Code (Text):
    # curl -I http://domain.com
    
    HTTP/1.1 301 Moved Permanently
    Connection: keep-alive
    Cache-Control: max-age=3600
    Expires: Fri, 03 Sep 2021 12:01:01 GMT
    Location: https://domain.com/
    Report-To: {"endpoints":[{"url":""}],"group":"cf-nel","max_age":604800}
    NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
    Server: cloudflare
    CF-RAY: 688e7a4e8a4cd6b5-FRA
    alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400


    Code (Text):
    curl -I http://www.domain.com
    
    HTTP/1.1 301 Moved Permanently
    Connection: keep-alive
    Cache-Control: max-age=3600
    Expires: Fri, 03 Sep 2021 12:03:05 GMT
    Location: https://www.domain.com/
    Report-To: {"endpoints":[{"url":""}],"group":"cf-nel","max_age":604800}
    NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
    Server: cloudflare
    CF-RAY: 688e7d5079070eaf-FRA
    alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400


    This look strange, but the code doesn't point to location https://www.domain.com
    Code (Text):
    curl -I https://domain.com
    
    HTTP/1.1 302 Found
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    location: https://www.domain.com
    set-cookie: PHPSESSID=qnjuhm4l21t2nhabmn3lfdttvt; path=/
    pragma: no-cache
    x-powered-by: centminmod
    x-xss-protection: 1; mode=block
    x-content-type-options: nosniff
    x-page-speed: 1.13.35.2-0
    cache-control: max-age=0, no-cache, no-store, must-revalidate
    CF-Cache-Status: DYNAMIC
    Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    Report-To: {"endpoints":[{"url":""}],"group":"cf-nel","max_age":604800}
    NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
    Server: cloudflare
    CF-RAY: 688e7f189e655b86-FRA
    alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400


    Code (Text):
    curl -I https://www.domain.com
    
    HTTP/1.1 301 Moved Permanently
    Content-Type: text/html
    Connection: keep-alive
    location: https://domain.com/
    x-powered-by: centminmod
    CF-Cache-Status: DYNAMIC
    Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    Report-To: {"endpoints":[{"url":""}],"group":"cf-nel","max_age":604800}
    NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
    Server: cloudflare
    CF-RAY: 688e83526a004e67-FRA
    alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400


    I "purge everything" multiple times in cloudflare caching configuration. That doesn't do anything.
     
    Last edited: Sep 3, 2021
  5. eva2000

    eva2000 Administrator Staff Member

    47,202
    10,670
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,575
    Local Time:
    11:57 AM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    what is your intended default version the non-www or www version of your domain ? if it's non-www version then the config for your domain.com.ssl.conf in your 1st post should be the one. Does the non-ssl config at /usr/local/nginx/conf/conf.d/domain.com.conf exist? As that should be removed if you are doing https default.

    If https is default on nginx site, then ensure Cloudflare SSL mode used is switched from Flexible SSL to Full SSL mode too.
     
  6. Veer

    Veer New Member

    10
    0
    1
    Oct 23, 2019
    Ratings:
    +1
    Local Time:
    6:57 AM
    1.19.1
    10.3.23
    Can't remember exactly and no backup now.
    ok I deleted that file, no change yet.
    yes, that's what I've on Cloudflare.

    This domain was created last year but was not used, so I'm wondering why not delete it and recreate it in Centmin Mod Menu? Maybe this will help solve the problem.

    Code (Text):
    -------------------------------------------------------------
    Commands to remove domain.com
    
    pure-pw userdel domain
     rm -rf /usr/local/nginx/conf/conf.d/domain.com.conf
     rm -rf /usr/local/nginx/conf/conf.d/domain.com.ssl.conf
     rm -rf /usr/local/nginx/conf/ssl/domain.com/domain.com.crt
     rm -rf /usr/local/nginx/conf/ssl/domain.com/domain.com.key
     rm -rf /usr/local/nginx/conf/ssl/domain.com/domain.com.csr
     rm -rf /usr/local/nginx/conf/ssl/domain.com
     rm -rf /home/nginx/domains/domain.com
     rm -rf /root/.acme.sh/domain.com
     rm -rf /root/.acme.sh/domain.com_ecc
     rm -rf /usr/local/nginx/conf/pre-staticfiles-local-domain.com.conf
     service nginx restart
    -------------------------------------------------------------

    Do I need to do something in CloudFlare for this domain? or when I recreate the domain it'll work as it is?

    Also, is there a way to regenerate the standard config file for a specific domain?
     
    Last edited: Sep 5, 2021
  7. eva2000

    eva2000 Administrator Staff Member

    47,202
    10,670
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,575
    Local Time:
    11:57 AM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    so the domain has no data/files etc on the site you need ? if so then yes you can delete it as per below instructions and then recreate it too via centmin.sh menu option 22 for Wordpress site or centmin.sh menu option 2 for non-wordpress site

    To properly remove an Nginx vhost the instructions are on official site at How to delete Nginx vhost account for existing domain/subdomain ? as well as on each Nginx vhost creation's ending output too lists the commands.

    You also get a log file for each Nginx vhost created which also lists the commands in 123.09beta01 and higher example for http2.domain.com remove log at /root/centminlogs/centminmod_140218-021218_nginx_addvhost_nv-remove-cmds-http2.domain.com.log
    Code (Text):
    ls -lahrt /root/centminlogs/ | grep remove
    -rw-r--r--   1 root root 1.3K Feb 14 02:12 centminmod_140218-021218_nginx_addvhost_nv-remove-cmds-http2.domain.com.log
    
     
  8. eva2000

    eva2000 Administrator Staff Member

    47,202
    10,670
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,575
    Local Time:
    11:57 AM
    Nginx 1.19.x
    MariaDB 5.5/10.x