I have an nginx config that is similar to this:

server {
  server_name my-english-site.com my-french-site.com;


  rewrite ^ https://$server_name$request_uri? permanent;

server {
  listen ssl;
  server_name my-english-site.com my-french-site.com;

When someone goes to http://my-french-site.com, it redirects to https://my-english-site.com, because the rewrite directive uses the first server in the $server_name directive.

I tried replacing $server_name with $host, expecting it to use the value of the Host request header. But it still redirects to the English URL.

How can I get non-HTTPS requests redirected to the corresponding HTTPS URLs?

Thank you!

  • $host is the correct way to do it, however it may be empty if the client sends no HOST header. – gparent Oct 27 '14 at 19:45
  • Ok, you're right. I just tried this again, and it works. Not sure why it didn't work for me the first time. Thanks! – TL-Eugene Oct 27 '14 at 19:56
  • It's probably that you were doing your tests with permanent redirects. – gparent Oct 27 '14 at 19:58

Apparently using the $host variable is the way to do it (thanks @gparent). I just tested it again and it works.

| improve this answer | |

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