2

I'm having some trouble redirecting https requests to my Nginx server when the only the short hostname of the server is used. I tried all combination of examples out there but nothing seems to trigger a redirect/rewrite.

For instance, the server is called web01, and I want all requests to go to https://web01.domain.com. I have the http -> https rewrite working perfectly to the FQDN. However, if I try to go to https://web01, it will proceed try and load the page and obviously error on certificate mismatch. I want it to rewrite the request to the FQDN. How can I configure the rewrite/redirect?

What I have now:

server {
    listen *:80;
    rewrite ^ https://web01.domain.com$request_uri permanent;
}

server {
    listen 443 default ssl;
    server_name web01.domain.com;
}
2
  • You define the domain name in the server_name part of the server block, the rewrite is for path only, not protocol or domain.
    – Tim
    Jul 11, 2017 at 9:02
  • One can use protocol and domain name for the rewrite destination, at least with permanent and temporary (301/302) redirects. Jul 11, 2017 at 16:49

2 Answers 2

0

The issue here is that when you access the server with https://web01, your browser expects to get an SSL certificate for web01 common name.

However, if your certificate is for the full domain name web01.domain.com, you will get a certificate error, because the domains do not match.

You will need to get an SSL certificate for web01 common name and configure a corresponding virtual host in your nginx configuration.

2
server_name primary.tld secondary.tld;
if ($host != $server_name) {
    rewrite ^/(.*) $scheme://$server_name/$1 permanent;
}

This works great for me. Answer is from Matthijs Kooijman:

https://stackoverflow.com/questions/10531975/how-to-configure-nginx-to-auto-redirect-to-the-main-hostname

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .