-1

I have the following nginx server block for my domain name example.com. I want to redirect non www to www for the SEO.

Update

According to this answer I used the following server block. But when I test it, I got the following

nginx: [warn] conflicting server name "www.example.com" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

So, I have the doubt is it correct or not and Whether it actually redirects the non www to www, please.

/etc/nginx/sites-available/example.com

server {
    server_name www.example.com;
    rewrite ^(.*) https://www.example.com$1 permanent;
}

server {
        root /var/www/abc-company-website/public;

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

        server_name example.com; 

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }


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


        location ~ /\.ht {
                deny all;
        }

        #Cache-Control
        location ~* \.(?:ico|ttf|png|svg|jpg|jpeg|js)$
        {
            expires 7d;
            add_header Pragma public;
            add_header Cache-Control "public";
        }

    listen 443 ssl; # managed by Certbot
    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
}


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


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

How can I change the above server block to redirect, please?

S.Sakthybaalan
  • 499
  • 6
  • 20
  • 1
    Possible duplicate of [Nginx no-www to www and www to no-www](https://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www) – Don't Panic Nov 22 '19 at 09:15
  • There are so many duplicates ([example](https://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www), [example](https://stackoverflow.com/questions/47203222/nginx-redirect-non-www-https-to-https-www), [example](https://stackoverflow.com/questions/38398325/nginx-redirect-non-www-to-www-and-https-for-domain-com-and-subdomain)) of this on SO, please try to search before posting a new question. – Don't Panic Nov 22 '19 at 09:16
  • @Don'tPanic, I also viewed. But I'm so confused – S.Sakthybaalan Nov 22 '19 at 09:17
  • Well then you need to explain what you tried, and show why your question is different. Start simple - remove everything and use one of the known, working examples in those duplicates. – Don't Panic Nov 22 '19 at 09:19
  • The error is pretty clear - you have conflicting servers for `www.example.com` configured - you have 2 servers configured for `www.example.com`, and 2 servers configured for `example.com`. You are making this hard for yourself - as I said already: **Start simple - remove everything and use one of the known, working examples in those duplicates.** It works. Then slowly, piece by piece, add back your own code, testing at each step. – Don't Panic Nov 22 '19 at 10:52
  • Then how can I test this working? What should I want to do? – S.Sakthybaalan Nov 22 '19 at 10:58
  • Don't use ifs in nginx, it's better to set the server brackets with server_name that you want. – flaixman Nov 22 '19 at 12:46

1 Answers1

0

It's in the best practices of nginx not to use if (even more of a reason if you are using it for $host), it's better to use server brackets with different server_name.

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

This will send HTTP www and HTTP non-www to HTTPS www

If you have a cert for non-www set a server bracket and redirect to www:

server{
    listen 443 ssl;
    server_name example.com;
    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
    return 301 https://www.example.com$request_uri;
} 

And finally you can do whatever you want in the https www.example.com bracket:

server {

    listen 443 ssl; # managed by Certbot
    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
    server_name www.example.com; 

     # Do whatever you want to do here to get to your application

 }

It's better to read the documentation and best practices of nginx, and try to make clean configurations so the next one that comes can understand it on first sight :D

If you've got any question just ask it. (looks like you didn't understand the duplicates given in the comments, so I decided to explain it 1 by 1 for your case)

flaixman
  • 702
  • 6
  • 14