0
#default:
server { 
    listen 80 default_server;

    server_name _;

    return 404;
}

server { 
    listen 443 default_server;

    server_name _;

    ssl_certificate         /etc/nginx/ssl/dummy.crt;
    ssl_certificate_key     /etc/nginx/ssl/dummy.key;

    return 404;
}


#old-domain:
server { 
    listen 80;

    server_name old-domain.com;
    server_name www.old-domain.com;

    return 301 https://www.old-domain.com$request_uri;
}

server {
    listen 443;

    server_name old-domain.com;
    server_name www.old-domain.com;

    ssl_certificate         /etc/nginx/ssl/old-domain.crt;
    ssl_certificate_key     /etc/nginx/ssl/old-domain.key;

    #catch all
    return 301 https://new-domain.com;
}


#new-domain:
server { 
    listen 80;

    server_name new-domain.com;
    server_name www.new-domain.com;

    return 301 https://www.new-domain.com$request_uri;
}

server {
    listen 443;

    server_name new-domain.com;
    server_name www.new-domain.com;

    ssl_certificate         /etc/nginx/ssl/new-domain.crt;
    ssl_certificate_key     /etc/nginx/ssl/new-domain.key;

    ....
}

As, requested, here is the output of curl -I for the various requests:

curl -I http://old.domain.com

HTTP/1.1 301 Moved Permanently Server: nginx/1.16.1 Date: Mon, 11 Nov 2019 07:48:57 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive Location: https://www.old.domain.com/

curl -I https://old.domain.com

curl: (60) SSL certificate problem: self signed certificate More details here: https://curl.haxx.se/docs/sslcerts.html curl failed to verify the legitimacy of the server and therefore could not establish a secure connection to it. To learn more about this situation and how to fix it, please visit the web page mentioned above.

  • I have added a more detailed example of my config. – user3313446 2 days ago
  • Welcome to ServerFault. Please post an example curl request on what's happening and what's expected to happen. – Pothi Kalimuthu 2 days ago
  • I have added the curl output to the original question. – user3313446 yesterday
0

You are redirecting to a 'https' page (port 443 by default). I guess you also have a default 404 configuration for SSL. Just change:

 return 301 https://new-domain.com;

to

 return 301 http://new-domain.com;
New contributor
Piotr P. Karwasz is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Sorry. In my attempt to make my scenario generic I did not include the listen:443 and ssl certificate definitions. I also include redirects from :80 to :443 on the 2 domains, which both currently have valid certificates. It is as if the redirect to another domain clears the hostname, do I need to set this anywhere? – user3313446 2 days ago
  • If the redirect of the old domain works, you can always check where it redirects to. Use e.g. curl -I http://old-domain.com. – Piotr P. Karwasz 2 days ago
  • It goes to the default server, which returns a 404. – user3313446 2 days ago
  • So curl -I http://old-domain.com gives you HTTP/1.1 301 Moved Permanently and Location: https://new-domain.com, but curl -I https://new-domain.com gives you a 404? You should add the SSL part of your config. – Piotr P. Karwasz 2 days ago
  • curl -I http://old-domain.com gives HTTP/1.1 301 Moved Permanently, and Location: https://old-domain.com. curl -I https://old-domain.com returns curl: (60) SSL certificate problem: self signed certificate which was setup on the default_server as a catchall – user3313446 2 days ago
0

I think that the problem it's with the certificates. You need to add the ssl proper certificate to the server block that you are redirecting on the old domain. Make sure that the certificate on the old domain is correct or it won't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.