I am using the default Laravel Fly configuration and the IPs that I get from $request->ip() are not accurate. They are showing the IP of the Fly proxy. This is causing problems with throttling, as it’s based on IP, and sessions have incorrect IPs.
How can I configure my Laravel app to use the IP of the client that made the request?
I already have the TrustProxies middleware and this line in my Dockerfile: && sed -i 's/protected \$proxies/protected \$proxies = "*"/g' app/Http/Middleware/TrustProxies.php, but it’s still showing the IP of the proxy
I have this now at the top of my public/index.php:
if (isset($_SERVER["HTTP_FLY_CLIENT_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_FLY_CLIENT_IP"];
$_SERVER['HTTP_X_FORWARDED_FOR'] = $_SERVER["HTTP_FLY_CLIENT_IP"];
}
This seems to work. Just tried it on a toy project. Will try prod now. If you have any better suggestions still, I’d be all ear.
One think that I was thinking: can’t this be done in Nginx? If it can be done in Nginx, it would be possible to adjust the Fly Laravel configuration. This way it would work out of the box when running fly launch. It’s a bit annoying that I know have to remember to make this change to public/index.php:
It’s easy to forget.
It’s nasty.
It’s Fly specific. I kinda liked that I had to make zero changes to my Laravel app to make it work with Fly.