简体   繁体   English

Laravel / Heroku,没有用于会话的此类文件或目录

[英]Laravel/Heroku, no such file or directory for sessions

I'm trying to run my application on heroku, but for some requests (only POST, GET are working fine), I've got the following error on my Ajax POST request: 我正在尝试在heroku上运行我的应用程序,但是对于某些请求(仅POST,GET运行正常),我的Ajax POST请求出现以下错误:

Error: Request failed with status code 419

When I check heroku logs, I get this error: 当我检查heroku日志时,出现此错误:

     production.ERROR: file_put_contents(/app/storage/framework/sessions/r9yIHf3WlKIzROWGPlVOk59rwr6tVyAeCLuJ9wWx): 
failed to open stream: No such file or directory
{"userId":1,"email":"xxx@gmail.com","exception":"[object] (ErrorException(code: 0): 
file_put_contents(/app/storage/framework/sessions/r9yIHf3WlKIzROWGPlVOk59rwr6tVyAeCLuJ9wWx): failed to open stream: No such file or directory at /app/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:122)

I've checked, the directory /storage/framework/sessions is existing. 我已经检查过,目录/storage/framework/sessions已经存在。

I've tried to run chmod 777 -R / (but still this issue) 我尝试运行chmod 777 -R /(但仍然是此问题)

I've tried the following commands: 我尝试了以下命令:

php artisan optimize
php artisan config:clear
php artisan cache:clear
php artisan config:cache
php artisan optimize:clear

A touch /app/storage/framework/sessions/r9yIHf3WlKIzROWGPlVOk59rwr6tVyAeCLuJ9wWx is not working either. touch /app/storage/framework/sessions/r9yIHf3WlKIzROWGPlVOk59rwr6tVyAeCLuJ9wWx也不起作用。

I've tried to remove the storage folder and create it again. 我试图删除存储文件夹并再次创建它。

Do you have an idea of what could go wrong? 您是否知道可能出什么问题?

Heroku's filesystem is temporary, and goes away any time your dynos are restarted - ie at least once daily (due to the automatic restarts Heroku does) and after every deploy or config change. Heroku的文件系统是临时的,并且在您的测功机重启时会消失-即每天至少一次(由于Heroku会自动重启)以及每次部署或配置更改后都会消失。

In addition, heroku run bash connects you to a brand new, separate server , so if you're creating/modifying files/folders/permissions in that, it'll literally all go away as soon as you close out of the session. 另外, heroku run bash将您连接到全新的独立服务器 ,因此,如果您在其中创建/修改文件/文件夹/权限,则在关闭会话后,它实际上将消失。 There's no effect on the actual webserver instances. 对实际的Web服务器实例没有任何影响。

This means you shouldn't rely on the filesystem for anything intended to be permanent - no file uploads, for example (they should go somewhere like AWS S3). 这意味着您不应该依赖文件系统来实现任何永久性的操作,例如,不上传文件(它们应该像AWS S3一样上传)。 Plus, if you're using more than one dyno, storing session data as files will mean the session only exists on one of the dynos - on the next pageview, there's a good chance it'll be gone, as the user has hit a different dyno. 另外,如果您使用多个dyno,则将会话数据存储为文件将意味着该会话仅存在于一个dyno上-在下一次综合浏览量下,很有可能它会消失,因为用户点击了不同的测功机。

Solution? 解? Store sessions using a different driver. 使用其他驱动程序存储会话。 I prefer the Redis driver, but you could use the database one if you wanted to keep complexity down a bit. 我更喜欢Redis驱动程序,但是如果您想降低复杂性,可以使用数据库驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM