F
Filamentβ€’2w ago
developer

file upload 401 unauthorized

Laravel 11.9, PHP 8.3, and Livewire 3.5, I'm experiencing difficulties with file upload. Every time I try to upload a file, whether it's an image or a video, I encounter a "401 Unauthorized" error, which only appears in the production environment. I've attempted multiple solutions from Google, but sadly, none have been effective, including some changes to Nginx configurations and other adjustments. 🚫 forceScheme https not working
if (app()->isProduction()) {
\URL::forceScheme('https');
request()->server->set('HTTPS', request()->header('X-Forwarded-Proto', 'https') == 'https' ? 'on' : 'off');
}
if (app()->isProduction()) {
\URL::forceScheme('https');
request()->server->set('HTTPS', request()->header('X-Forwarded-Proto', 'https') == 'https' ? 'on' : 'off');
}
🚫 nginx config also not working
I set strict https in nginx.conf proxy_set_header X-Forwarded-Proto https; or correctly set up nginx ssl settings
I set strict https in nginx.conf proxy_set_header X-Forwarded-Proto https; or correctly set up nginx ssl settings
🚫 Trusted proxies
->withMiddleware(function (Middleware $middleware) {
$middleware->trustProxies(at: '*');
})
->withMiddleware(function (Middleware $middleware) {
$middleware->trustProxies(at: '*');
})
I've already tried all the fixes from here: https://github.com/filamentphp/filament/discussions/9243 https://github.com/livewire/livewire/discussions/3084 this is so frustrating :/ but it works on my machine πŸ˜…
Solution:
Ok, also try added before the location .php etc location / { try_files $uri $uri/ /index.php?$query_string; }...
Jump to solution
23 Replies
toeknee
toekneeβ€’2w ago
Hang on a second, where is the disk for the file uploads?
developer
developerOPβ€’2w ago
im using ->disk('public') also tried disk('local') same error
toeknee
toekneeβ€’2w ago
Ok was just checking it wasn't an S3 issue. Ensure you have set if (App::environment('production', 'development')) { URL::forceScheme('https'); } in the AppServiceProvider too
developer
developerOPβ€’2w ago
php artisan storage:link done βœ…
toeknee
toekneeβ€’2w ago
ensure you link the storage ensure your app_url matches the accessing url too.
developer
developerOPβ€’2w ago
yes, we already tried it .env app_url matches βœ… but fileUpload still failing We are using CloudFlare, and I read that it could be the problem. We haven't disabled it yet because it might break other sites hosted on our VPS
toeknee
toekneeβ€’2w ago
Strange, I use cloudflare with apps deployed through ploi and forge. All works fine here.
developer
developerOPβ€’2w ago
i'll share the nginx config, gimme a minute
developer
developerOPβ€’2w ago
awcodes
awcodesβ€’2w ago
do you have a directory at storage/livewire-tmp, I'm thinking this might be a permissions issue on the server were the app (server user) can't create directories.
developer
developerOPβ€’2w ago
not sure, lemme check
awcodes
awcodesβ€’2w ago
i could be wrong, but it everything else is setup correctly then that's the only thing coming to mind
toeknee
toekneeβ€’2w ago
You seem to be missing quite a bit in the fast-cgi compared to what I use i.e.
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_buffers 16 16k;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
include fastcgi_params;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_buffers 16 16k;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
include fastcgi_params;
}
SCRIPT_FILENAME and pass missing could cause it and then public check chmod permissions
developer
developerOPβ€’2w ago
ok, i'll try both things, i'll be right back directory was already set & got all the correct permissions sadly issue persists
toeknee
toekneeβ€’2w ago
Does the file upload at all?
developer
developerOPβ€’2w ago
no
Solution
toeknee
toekneeβ€’2w ago
Ok, also try added before the location .php etc location / { try_files $uri $uri/ /index.php?$query_string; }
developer
developerOPβ€’2w ago
FileUpload::make('_thumbnail_image_id')
->label('Product Image')
->extraInputAttributes([
'data-id' => '_thumbnail_image_id',
// 'data-url' => URL::signedRoute('livewire.upload-file', ['expires' => now()->addMinutes(30)])
])
->image()
->visibility('public')
->disk('local') // Ensure correct disk, i also using the 'public' one
->directory('product')
->columnSpan('full'),
FileUpload::make('_thumbnail_image_id')
->label('Product Image')
->extraInputAttributes([
'data-id' => '_thumbnail_image_id',
// 'data-url' => URL::signedRoute('livewire.upload-file', ['expires' => now()->addMinutes(30)])
])
->image()
->visibility('public')
->disk('local') // Ensure correct disk, i also using the 'public' one
->directory('product')
->columnSpan('full'),
lemme try it works!!!! working!!!
toeknee
toekneeβ€’2w ago
Welcomes!
developer
developerOPβ€’2w ago
thank you so muchhhhhhhh β™₯
toeknee
toekneeβ€’2w ago
it was because of try_files $uri $uri/ /index.php?q=$uri&$args; you had a duplicated $uri and the uri/args is a bit messy
developer
developerOPβ€’2w ago
I don’t have much experience with Nginx, so I wouldn’t pick up on it. Thanks a lot once more! πŸ”₯
toeknee
toekneeβ€’2w ago
No problem

Did you find this page helpful?