Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alexbanutz10's avatar

Authentication in Laravel doesn’t work some time after the deploy in a specific environment

I have 2 web platforms built with Laravel that work perfectly fine both on the development environment as well as on local environemnt.

The problem lies in the fact that after I deploy these applications on a cloud infrastructure as a service provider, the applications work for approximatelly ~ 1 / 2 hours, sometimes even less, and then, on every login request it redirects me to back to the login page, even though the credentials on the login attempt are correct.

In between the login request and the redirect response I get blank view with the XSRF token stored in the session, but only for a few miliseconds.

For a clearer view of the problem I need to give you the full context so I will briefly document the infrastructure and the architecture on which the application runs

The applications use the following technologies:

  • Laravel (web platform built in the old fashioned monolith style) v10
  • MariaDB v10.7.5
  • Redis cache database v.6.2.7
  • Melisearch v0.27.2

For each one of these technologies there is a Docker container. The communication between them is facilitated by a traefik Docker network.

These are some of the environment configuration variables I use that I think might be relevant for the problem:

  • for one of the platforms are these:
CACHE_DRIVER: "redis"
QUEUE_CONNECTION: "redis"
SESSION_DRIVER: "redis"
  • for the other platfom are just the default ones:
CACHE_DRIVER: "file"
QUEUE_CONNECTION: "sync"
SESSION_DRIVER: "file"

The following things can be done to temporarily get the app working:

  • ssh-ing into the VPS and manually restarting the container
  • clearing the 2 cookies set in the browser’s storage (the XSRF cookie and the session cookie)

Has someone else encountered this wierd behaviour? If so what’s causing it and how could it be solved?

0 likes
1 reply
LaryAI's avatar
Level 58

Based on the information provided, it seems like the issue might be related to session management, particularly with the Redis session driver. Here are a few steps you can take to troubleshoot and potentially solve the problem:

  1. Session Lifetime: Ensure that the session lifetime is set correctly in your config/session.php file. If the lifetime is too short, sessions may expire too quickly.
'lifetime' => 120, // Session lifetime in minutes
  1. Redis Configuration: Verify that your Redis instance is configured correctly and is not running out of memory or evicting keys prematurely. Check the maxmemory-policy and maxmemory settings in your Redis configuration.

  2. Permissions and Ownership: Make sure that the Redis storage directory has the correct permissions and ownership if you're using persistence.

  3. Session Persistence: If you're using Redis for session management, ensure that the Redis server is persisting data correctly. You might want to configure Redis to use AOF (Append Only File) or RDB (Redis Database File) for data persistence.

  4. Environment Configuration: Double-check your environment variables to ensure they are being read correctly in the cloud environment. Sometimes, environment variables can be cached or not refreshed after deployment.

  5. Logging: Add logging to your authentication process to see if there are any errors or warnings that might indicate what's going wrong.

  6. Session Driver Fallback: As a temporary measure, you could switch the session driver to file to see if the issue persists. If the problem goes away, it's likely related to Redis.

  7. Cache and Session Prefix: If you're using the same Redis instance for multiple applications, ensure that you have different prefixes for cache and session to avoid collisions.

'prefix' => 'appname_session_', // In config/session.php
'prefix' => 'appname_cache_', // In config/cache.php
  1. Docker Networking: Ensure that the Docker network configuration allows for persistent connections between your Laravel application and the Redis container.

  2. Scaling Issues: If your application is under heavy load, the Redis instance might be unable to handle the number of connections. Consider scaling your Redis service or optimizing its performance.

  3. Session Cookie Domain: Check the session cookie domain configuration. If it's not set correctly, the browser might not send the cookie back to the server, causing a new session to start on each request.

'domain' => '.yourdomain.com', // In config/session.php
  1. Clear Configuration Cache: After deployment, clear the configuration cache to ensure that the latest configuration is being used.
php artisan config:clear
  1. Health Checks: Implement health checks for your services to ensure they are running correctly and restart them if they are not.

If none of these steps resolve the issue, you may need to provide more specific logs or error messages to further diagnose the problem.

Please or to participate in this conversation.