Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx redirect multiple servers to SSL

Tags:

nginx

ssl

I have this code. I just want each of the server_name in the list to redirect to its own name https. But, if I do http://beta.example.com, it redirects to https://api.example.com (or whatever the first item in the list is)

server {
    listen         80;
    server_name    api.example.com beta.example.com apibeta.example.com nodebeta.example.com app.example.com;
    return         301 https://$server_name$request_uri;
}
like image 424
user1387717 Avatar asked Feb 08 '16 14:02

user1387717


1 Answers

You should be able to use the $host variable instead:

server {
    listen         80;
    server_name    api.example.com beta.example.com apibeta.example.com nodebeta.example.com app.example.com;
    return         301 https://$host$request_uri;
}
like image 125
StephenKing Avatar answered Sep 27 '22 19:09

StephenKing