Skip to content

Instantly share code, notes, and snippets.

@fermayo
Created January 24, 2018 03:11
Show Gist options
  • Save fermayo/92a0629a4cd60dd45fde660176fa885d to your computer and use it in GitHub Desktop.
Save fermayo/92a0629a4cd60dd45fde660176fa885d to your computer and use it in GitHub Desktop.
Automatically configure nginx to trust AWS Cloudfront IPs present in X-Fowarded-For
FROM alpine:latest AS cloudfront
RUN apk --no-cache add curl jq
RUN curl https://ip-ranges.amazonaws.com/ip-ranges.json | \
jq -r '.prefixes[] | select(.service=="CLOUDFRONT") | .ip_prefix' | \
xargs -I '{}' echo 'set_real_ip_from {};' > /cloudfront.conf && \
echo 'real_ip_header X-Forwarded-For;' >> /cloudfront.conf && \
echo 'real_ip_recursive on;' >> /cloudfront.conf
FROM nginx:latest
COPY --from=cloudfront /cloudfront.conf /etc/nginx/conf.d/cloudfront.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment