0

I am super new to Nginx and CF and I have deployed my website to example.com and it is working perfectly and it redirects to https as well. But I want to redirect to www all the time. Right now if I type in example.com it doesn't redirect to www.

I have also set up Cloudflare for my website.

After looking for solutions and found one in Digital Ocean which said to replace this:

server {

        if ($host = www.example.com) {
                return 301 https://$host$request_uri;
        } # managed by Certbot


        if ($host = example.com) {
                return 301 https://$host$request_uri;
        } # managed by Certbot

        server_name example.com www.example.com;
        listen 80;
        return 404; # managed by Certbot

}

to this:

server {

        if ($host = www.example.com) {
                return 301 https://www.example.com$request_uri;
        } # managed by Certbot


        if ($host = example.com) {
                return 301 https://www.example.com$request_uri;
        } # managed by Certbot

        server_name example.com www.example.com;
        listen 80;
        return 404; # managed by Certbot

}

But it isn't working. What is wrong here?

Here's the full nginx config for the website:

server {

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

    location / {
        proxy_pass http://localhost:3000; 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $http_cf_connecting_ip;
        proxy_set_header X-Forwarded-For $http_x_forwarded_for;
        proxy_cache_bypass $http_upgrade;
    }

    real_ip_header CF-Connecting-IP;

}

server {

        if ($host = www.example.com) {
                return 301 https://www.example.com$request_uri;
        } # managed by Certbot


        if ($host = example.com) {
                return 301 https://www.example.com$request_uri;
        } # managed by Certbot

        server_name example.com www.example.com;
        listen 80;
        return 404; # managed by Certbot

}
Zak
  • 101

2 Answers2

0

Certbot unfortunately implements its redirects on nginx in problematic way.

Redirecting from www to no www in nginx reverse proxy is a similar situation, please refer to the answer for the configuration you should use.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

You mention the client gets redirected to HTTPS, from what it looks like they are getting TLS encryption from Cloudflare, your Nginx is not configured for it.

This block will only respond to example.com:

server {

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

  server_name example.com; # Removed the www subdomain

  location / {
    proxy_pass http://localhost:3000; 
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $http_cf_connecting_ip;
    proxy_set_header X-Forwarded-For $http_x_forwarded_for;
    proxy_cache_bypass $http_upgrade;
  }

  real_ip_header CF-Connecting-IP;

}

This block will remove the www making a 301 permanent redirect to https://example.com:

server { 

  server_name www.example.com;
  listen 80;
  return 301 https://example.com$request_uri;

}

Now you can set up everything on a single block, however, I would suggest you set TLS up and use the top block to start listening for TLS connections on another port, usually 443.

Marco Zink
  • 126
  • 7