3

I have an nginx config that is similar to this:

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

  listen 0.0.0.0:80;

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

server {
  listen 0.0.0.0:443 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!

marked as duplicate by Michael Hampton Oct 27 '14 at 23:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $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
0

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

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