ya22y
ya22y

Reputation: 36

Session Issues when running laravel on nginx reverse proxy

I am using *nginx* as reverse proxy to redirect requests from host server to *LXD* container which runs *laravel 11* app, all runs smoothly, the only issue is that validation messages aren't displayed at all, to track that i am dumping error message and then i triggered validation error, messages are dumped when running app in localhost so its not a code issue.

**testing on localhost**

[![validation errors not displayed](https://i.sstatic.net/JfK7BwC2.png)](https://i.sstatic.net/JfK7BwC2.png)

**testing on server**

[![validation errors displayed](https://i.sstatic.net/C9dD4vrk.png)](https://i.sstatic.net/C9dD4vrk.png)

I extended the size of buffers for both *proxy* and *fastcgi*, however the problem persists, i might be configuring the whole thing wrong maybe.

**nginx conf in LXD**

server {
   listen 80;
   server_name lxd-ip;
   root /var/www/public;
   index index.php;
   charset utf-8;

   location / {
       try_files $uri $uri/ /index.php?$query_string;
   }

   location = /favicon.ico {
       access_log off;
       log_not_found off;
   }

   location = /robots.txt {
       access_log off;
       log_not_found off;
   }

   error_page 404 /index.php;

   location ~ \\.php$ {
       fastcgi_buffers 20 128k;
       fastcgi_buffer_size 256k;
       fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
       fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       include fastcgi_params;
   }

   location ~ /\\.(?!well-known).\* {
       deny all;
   }
}

**nginx conf in HOST**

server {
   listen 80;
   server_name admin.exemple.com www.exemple.com;
   return 301 https://$server_name$request_uri;
}

server {
   listen 443 ssl;
   ssl_certificate /etc/letsencrypt/live/exemple.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/exemple.com/privkey.pem;

   server_name admin.exemple.com;

   location / {
       proxy_pass http://lxd-ip:80;
       proxy_http_version 1.1;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Proto $scheme;
       proxy_cookie_path ~^/.\* /;
       proxy_cookie_domain admin.exemple.com admin.exemple.com;
       proxy_cache_bypass $http_upgrade;
       proxy_buffer_size 256k;
       proxy_buffers 20 128k;
       proxy_busy_buffers_size 256k;
   }

   location ~ \.php$ {
       fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
       fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       include fastcgi_params;
       fastcgi_buffers 20 128k;
       fastcgi_buffer_size 256k;
       client_max_body_size 10M;
   }
}

this is how usually i configure laravel session

SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_SECURE_COOKIE=true
SESSION_DOMAIN="admin.exemple.com"
SANCTUM_STATEFUL_DOMAINS="https://admin.exemple.com"
ALLOWED_ORIGINS=https://exemple.com

Upvotes: 0

Views: 357

Answers (1)

ya22y
ya22y

Reputation: 36

I needed to change how laravel handles session to a more server based driver, in my case i used database as session driver.

inside .env set SESSION_DRIVER to database

Create session table php artisan session:table then php artisan migrate

Upvotes: 0

Related Questions