mickzer
mickzer

Reputation: 6338

Redirecting HTTP requests to ELB to HTTPS for multiple domains

I have a question very similar to this post: Redirecting EC2 elb from http to https

I'm using NGINX and I want to redirect all HTTP requests to my servers to HTTPS. Which as answered in the above post can be achieved with:

if ($http_x_forwarded_proto = 'http') {
    return 301 https://example.com$request_uri;
}

However, I want to expand on this:
-If the user goes to http://example.com, I want them to be redirected to https://example.com.
-If they go to http://www.example.com I want them to be redirected to https://www.example.com.
-If the user goes to http://some-sub-domain.example.com, I want them to be redirected to https://some-sub-domain.example.com

The reason I want to achieve this is that I am serving multiple domains from the same servers. Each of my clients will have their own subdomain however my application is served from the same servers.

Any help or guidance is greatly appreciated!! :)

Upvotes: 0

Views: 1682

Answers (1)

mickzer
mickzer

Reputation: 6338

Turns out this is really simple and can be done with the use of the NGINX $host variable:

if ($http_x_forwarded_proto = 'http') {
    return 301 https://$host$request_uri;
}

Upvotes: 2

Related Questions