here's the situation: you have a webpage running on a couple of docker containers which are all behind a nginx proxy manager reverse proxy and now you want to use cloudflare to protect your site.
here's what you have to do to achieve that:
I have created a script to automatically write a include file, which can be included in the host configuration on nginx proxy manager under Advanced –> Custom Nginx Configuration. add
include /data/nginx/custom/cloudflare[.]conf;
in the text field and make sure to adjust the path in the following script to point to the same location (note that the path in the custom config is what the path is inside your nginx proxy manager container, and in the update script it will be the path of the host server).
here is the script that generates the include:
#!/bin/bash newconf=/tmp/cloudflare.conf liveconf=/opt/proxy/data/nginx/custom/cloudflare.conf echo "#Cloudflare" > $newconf; for i in $(curl https://www.cloudflare.com/ips-v4 2>/dev/null); do echo "set_real_ip_from $i;" >> $newconf; done for i in $(curl https://www.cloudflare.com/ips-v6 2>/dev/null); do echo "set_real_ip_from $i;" >> $newconf; done echo "real_ip_header X-Forwarded-For;" >> $newconf; echo "real_ip_recursive on;" >> $newconf; if ! diff -q $liveconf $newconf ; then echo "cloudflare ip list has changed, reloading nginx proxy manager"; cp $newconf $liveconf cd /opt/proxy docker-compose exec nxapp nginx -s reload fi
adjust the $liveconf path and the name of the nginx proxy manager app for docker-compose to reload “nxapp” in my example. if you are not using docker-compose, use some other method to run the reload command in your docker container here.
add custom config to nginx proxy manager host config
ssl_verify_client on; ssl_client_certificate /data/nginx/cloudflare.pem;
download cloudflare certificate from https://developers.cloudflare.com/ssl/static/authenticated_origin_pull_ca.pem and save it as /data/nginx/cloudflare.pem
finally enable authenticated origin pulls in cloudflare admin console under “SSL/TLS –> Origin Server”