vincent15000's avatar

Route not found only in production

Hello,

I work on an existing Laravel application (v7.24) and a web route was defined like this.

$proxy_url    = getenv('PROXY_URL');
$proxy_scheme = getenv('PROXY_SCHEME');

if(env('APP_ENV') != 'local') {
    if (!empty($proxy_url)) {
        URL::forceRootUrl($proxy_url);
    }

    if (!empty($proxy_scheme)) {
        URL::forceScheme($proxy_scheme);
    }
}

Route::get('/', function () {
    return view('welcome');
});

Route::get('store', function ()
{
    if (!request()->input('file')) {
        abort(404);
    }
    $filename = request()->input('file');
    $path = 'public/' . $filename;

    if (!Storage::exists($path)) {
        abort(404);
    }
    return Storage::response($path);
});

Here is an example of how the route is used from the frontend developed with nuxt / VueJS.

http://mysuperdomain.com/store?file=%2Fbup%2F9XrKJaJaKvDMo3k0mPPD6MvSNj3i89n801K8s9PT.jpg"

The other routes are API routes.

In development and in production, php artisan route:list shows that the route exists.

The route displays the image in development mode, but the route is not found in production mode (404 error). I have tested to change the abort(404) in the closure (for example with 405 error) and I also have a 404 error, in other words the route is not recognized in production mode.

I know that there are bad pratices in this code, I don't have written it myself, but is there any reason for which the route could be not recognized only in production mode ?

I specify that the routes are not cached in production on the demo server and the CORS problems seem to be solved.

I think that the problem is necessarily binded to the server configuration or perhaps any environment variable or something with the frontend or ... but I have no idea where I could search, at what part of the code I could look to find a solution.

If you have any idea, it would be great.

Thanks a lot.

Vincent

0 likes
7 replies
vincent15000's avatar

Can anybody help me to know what I should check in the code ?

Lopsum's avatar

👋 Maybe it's a problem with the storage_path. Is it correctly configured on your production server, with the symbolic link? Did you check that the image was present in the folder?

1 like
vincent15000's avatar

@Vable The problem is not that the image doesn't display, when I try to access the route from the browser, the server returns a Not found exception. I have added a dd('test') here.

Route::get('store', function ()
{
	dd('test');
    if (!request()->input('file')) {
        abort(404);
    }
    $filename = request()->input('file');
    $path = 'public/' . $filename;

    if (!Storage::exists($path)) {
        abort(404);
    }
    return Storage::response($path);
});

And it doesn't show test on the screen.

So I have two contradictory informations :

  • php artisan route:list shows the route

  • while accessing the route, the server answers Not found

I have checked and the symbolic link has been created.

I think that if the storage path were wrong, the route would be accessible but it would just say that the image is not found, not the route.

vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

Problem solved => the dev who has installed Laravel on the webserver has set a bad configuration.

APP_URL=https://domain.com/public

And the Apache server pointed to the root folder of the project instead of pointing to the public folder of the server.

From the beginning I thought about that, but I didn't have access to the demo server, so I have had to ask to have the right informations.

jlrdw's avatar

@vincent15000 APP_URL shouldn't point to public. You are supposed to point to public as document root with the server.

1 like
vincent15000's avatar

@jlrdw Thank you ;) ... yes I know that, but the other dev who has installed Laravel on the server seems to not know that. And as I didn't have access to the demo server, I can't check the configuration by myself and he was sure that the configuration was ok.

Please or to participate in this conversation.