I want to redirect all the HTTP request to https request on ELB. I have two EC2 instances. I am using nginx for the server. I have tried a rewriting the nginx conf files without any success. I would love some advice on it.
相关问题
- Stop .htaccess redirect with query string
- JQ: Select when attribute value exists in a bash a
- Can ServiceStack JsonServiceClient send a get requ
- UrlEncodeUnicode and browser navigation errors
- How can I prevent my Shiny App from disconnecting
It may not be the solution you might be looking for, but another option could be to use AWS CloudFront in addition to ELB. CloudFront gives the option to redirect all incoming HTTP traffic to HTTPS.
I had the same problem, in my situation HTTPS was handled entirely by ELB and I didn't know my source domain ahead of time so I ended up doing something like:
And then of course pointing the ELB 'https' to the instance port 80 and then the 'http' route to the instance port 81.
The htaccess solutions above caused ELB health check to fail. I had some trouble finding the solution until I discovered an article online in which someone had the same issues I had. His solution was to add this to the beginning of the htaccess file instead:
Source: Redirecting HTTP to HTTPS with AWS and ELB
ELB sets
X-Forwarded-Proto
header, you can use it to detect if original request was to HTTP and redirect to HTTPS then.You can try this in your
server
conf:Take a look at ELB docs.
Based on @Ulli's answer If you want to configure it using Terraform, here is an example >
Source
The Amazon Elastic Load Balancer (ELB) supports a HTTP header called X-FORWARDED-PROTO. All the HTTPS requests going through the ELB will have the value of X-FORWARDED-PROTO equal to “HTTPS”. For the HTTP requests, you can force HTTPS by adding following simple rewrite rule. For me it works fine!
Apache
You can add following lines in your .htaccess file:
Or if you use vhost.conf for managing multiple domains in same EC2 web server then you can add following to the vhost.conf (add it to the domain you want to use https for it):
IIS
Install IIS Url-Rewrite module, using the configuration GUI add these settings:
Read more here