Monday, February 26, 2018

redirect-http-https-on AWS ELB

Apache

The rewrite rule for an Apache backend is similar to the following in .htaccess:




RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

 
 
Nginx

The rewrite rule for an Nginx backend in the ngnix.conf file is similar to the following:
Note: Applies to versions nginx/1.10.3 (Ubuntu) and nginx/1.12.1 (Amazon Linux).


server {
      listen         80;
      server_name    www.example.org;
      if ($http_x_forwarded_proto = 'http') {            
  return 301 https://$server_name$request_uri$http_x_forwarded_proto;
        }
}
Ec2-instance-AutoEIP-Assign-for-whitelistip

#!/bin/sh
# Region in Which instance is running
EC2_REGION='us-east-1'
AWS_ACCESS_KEY='XXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY='XXXXXXXXXXXXXXXXXXX'

#Instance ID captured through Instance meta data
InstanceID=$(/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id)

#Query free Elastic IP and write all allocated ID  aws ec2 describe-addresses --query 'Addresses[?AssociationId==null]' --output text | perl -lne 'print if /(\w+\-){1}\w+/'

aws ec2 describe-addresses --query 'Addresses[?AssociationId==null]' --output text | perl -lne 'print $& if /(\w+\-){1}\w+/' > /opt/EIP/output.txt
Allocate_ID=$(head -n 1 /opt/EIP/output.txt)

#Assigning Elastic IP to Instance
aws ec2 associate-address --instance-id $InstanceID --allocation-id $Allocate_ID