Trying to redirect non-www to www (Certbot, Nginx), but the opposite occurs. What's causing this?

April 17, 2018 304 views
Nginx Ubuntu Ubuntu 16.04
saocyan
By:
saocyan

I want to redirect all domain traffic to https://www.mysite.com.

Instead, everything is going to https://mysite.com.

I'm not sure what I'm doing wrong. Any help would be greatly appreciated!

server {

  root /var/www/mysite.com;
  index index.html index.php index.htm index.nginx-debian.html;

  server_name mysite.com www.mysite.com;

  location / {
    #try_files $uri $uri/ =404;
    try_files $uri $uri/ /index.php$is_args$args;
  }

  location = /favicon.ico { log_not_found off; access_log off; }
  location = /robots.txt { log_not_found off; access_log off; allow all; }
  location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
    expires max;
    log_not_found off;
  }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  }

  location ~ /\.ht {
    deny all;
  }



  listen [::]:443 ssl; # managed by Certbot
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {

  return 301 https:///www.mysite.com$request_uri;

  listen 80;
  listen [::]:80;

  server_name mysite.com www.mysite.com;
  return 404; # managed by Certbot

}
1 Answer

Your configuration should route all http traffic to https://www.mysite.com but will not redirect https://mysite.com to https://www.mysite.com unless you define a seperate server block for the non-www version.

That does not however, explain things redirecting from http to https://mysite.com but I have a guess about what may. By any chance are you running WordPress on this site? In addition to the redirects in your Nginx configuration, WordPress does it's own redirecting.

When you first configure WordPress it will use the URL used to set up it's default URL setting (this is under the main site settings in wp-admin). So if you first configured WordPress by going to http://mysite.com it will use mysite.com as it's base URL and redirect any requests for other URLs to that.

Note that you should check and address this WordPress setting if relevant before you add the non www to www redirect I mentioned. Doing so before updating your WordPress settings could cause a redirect loop between Nginx and WordPress.

  • Yup, it was Wordpress.

    I use Migrate DB to migrate my databases, and I assumed it was rewriting URLs to WWW/SSL but it wasn't for some reason.

    I ended up adding a build step to change http:// to https://www.

    Like you said, the site url was wrong, which was causing circular redirects and all sorts of other unpleasantness.

    Thanks for the tip!

    Here's the config I ended up with:

    # Redirect all HTTP traffic to HTTPS
    
    server {
    
      listen 80;
      server_name example.com www.example.com;
    
      return 301 https://www.example.com$request_uri;
    
    }
    
    server {
    
      listen 443 ssl;
      server_name example.com;
    
      return 301 https://www.example.com$request_uri;
    
    }
    
    server {
    
      listen 443 ssl;
      server_name www.example.com;
    
      root /var/www/example.com;
      index index.html index.php index.nginx-debian.html;
    
      include global/php.conf;
      include global/wordpress.conf;
    
    
      ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
      ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
      include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    }
    
Have another answer? Share your knowledge.