Skip to content

Instantly share code, notes, and snippets.

@tunght13488
Last active May 16, 2019 15:32
Show Gist options
  • Save tunght13488/b955302dcb45afb1cfa9 to your computer and use it in GitHub Desktop.
Save tunght13488/b955302dcb45afb1cfa9 to your computer and use it in GitHub Desktop.
htaccess IP whitelisted rule behind load balancer
# See: http://www.jonathanquail.com/blog/2012/05/09/restricting-access-to-servers-behind-an-elastic-load-balancer/
# Extract client IP
#SetEnvIf REMOTE_ADDR "(.+)" CLIENTIP=$1
# If request is forwarded, use forwarded IP instead
SetEnvIf X-Forwarded-For "^([0-9.]+)" CLIENTIP=$1
# If request is not forwarded, set a flag
SetEnvIf X-Forwarded-For "^$" is_not_forwarded
# Whitelist
SetEnvIf CLIENTIP "173.55.53.87" allowed_in
SetEnvIf CLIENTIP "72.67.47.197" allowed_in
# Rules
Order deny,allow
Deny from all
# Allow whitelisted forwarded IP
Allow from env=allowed_in
# Allow request which is not forwarded (healthcheck)
Allow from env=is_not_forwarded
# Remove comment from the following line to let everyone in
#Allow from all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment