2

I have been trying to figure out for a while how to redirect the requests from the non-WWW domain to the WWW domain. This is the HTTPS.conf file:

server {
    server_name domain.com *.domain.com;
    return 302 https://$host$request_uri;
}


 server {
    listen 443 ssl default_server;
    server_name www.domain.com;

    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
}

Based on the configuration it should be working. As all non WWW domains should get redirected, but they don't. I have changed the 301 to 302 for testing purposes. These are the errors I get when I run nginx -t

nginx: [warn] conflicting server name "domain.com" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "domain.com" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "*.domain.com" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "www.domain.com" on 0.0.0.0:443, ignored

This is the HTTP.conf file where the certificate is managed.

server {
    server_name www.domain.com domain.com;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed b$
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

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


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

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

I am sure that the issue is with the last few if lines that are generated by certbot. But if I change them/remove the domain.com from the port80 server block I end with "SSL certificate not valid". Probably because of the redirections. There are just too many factors here for me to be able to figure out the solution alone. I dont have issues with http to https redirect.

BoKKeR
  • 21
  • 2
  • 3
    Possible duplicate of [In Nginx, how can I rewrite all http requests to https while maintaining sub-domain?](https://serverfault.com/questions/67316/in-nginx-how-can-i-rewrite-all-http-requests-to-https-while-maintaining-sub-dom) – bodgit Jan 17 '19 at 12:59
  • Totally different question/answer, I dont have issues with http/https redirect in the way the other post has. – BoKKeR Jan 17 '19 at 13:57

2 Answers2

0

Could this be the answer? I've used this a few times for my own server. https://stackoverflow.com/questions/10294481/how-to-redirect-a-url-in-nginx

Jake t
  • 127
  • 2
  • 8
0

I have found that the answer was a little more complicated than I thought initially. The main issue was that I had both HTTP.conf and HTTPS.conf. I removed HTTPS.conf as all the https requests were handled in http.conf. I moved the redirect. I edited the http.conf and ended up with this:

server {
    server_name domain.com www.domain.com;
    return 301 https://www.$host$request_uri;
}

server {
    server_name www.domain.com;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed b$
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

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


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

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

It does not seem to be working on my main PC (even with incognito) but it works on redirect inspection tools and other devices.

BoKKeR
  • 21
  • 2