Search code examples
laravelsessionnginxload-balancinglaravel-socialite

Laravel Socialite behind Nginx load balancer


I have a Laravel 7 APP with two instances behind an NGINX load balancer with SSL terminating at the load balancer, I've set up Trusted Proxies as described in the Laravel documentation which is working as expected and all traffic is using HTTPS. I have both Laravel instances using the same Redis server for session and a separate Redis server for cache, and both instances are using the same session domain in .env

Both Laravel servers work correctly if they are the only instance in the load balancer. However when both Laravel instances are added to the load balancer any Socialite login fails with an invalid state error.

HTTP 500 Internal Server Error

Laravel\Socialite\Two\InvalidStateException

AbstractProvider->user()

/app/Http/Controllers/Auth/LoginController.php (line 108)

// Get google user data
$google = Socialite::driver('google')->user();

I have the same issue with both Google and Facebook logins. If I try to login manually or register a new user I get 419 | Page Expired, but none of these issues occurs when the load balancer has only one instance or I don't us a load balancer.

Thanks,

Lee.


Solution

  • Ok so I've fixed the problem, I rebuilt the .env file and then cut and pasted into both servers, I then generated new keys and restarted the app servers and the load balancer.

    I'd gone through and checked both .env files several times, so either I missed something or the issue wasn't visible ?

    Either way it's working now.

    Thanks,

    Lee.