nettt's avatar
Level 15

Laravel behind reverse proxy

I build an application with the TALL stack and now i want to publish it on the website of a client. Normally we do this on a (sub)domain but the client want (for SEO) to put it behind an own url on their website.

so my application is hosted on https://applicationname.mydomain.com and it is visible with a reverse proxy at www.clientdomain.com/en/applicationname/. All my static asset files are working correct but all my routes don't and generate a 404 page.

I tried URL::forceRootUrl('https://www.clientdomain.com/en/applicationname'); in my web.php routes file but that doesn't do anything.

What can I do to fix this problem?

0 likes
5 replies
rodrigo.pedra's avatar

Is your app to be the only thing running on https://www.clientdomain.com/ ?

Or does your client have a regular site and expects your app to run from a subfolder?

If your app is the only thing running on that domain, install it as it would be served from the root path, wrap your routes in a Route::group(...) and add /en/applicationname as a prefix. This should work.

If your app is not the only thing running in that domain, try clearing the app's routes cache.

php artisan route:cache

Unfortunately serving an app from a sub-path is not supported by Laravel.

There are a lot of issues open in Laravel's GitHub issue tracker regarding this setup. Most issues complain apps breaking after caching their routes, but there are issues with URL signing and other URL related tasks (URL generated in notifications/queues jobs, etc.).

The official instance from Laravel maintainers is that issues related to serving an app from a sub-path are not considered a bug.

From the docs:

https://laravel.com/docs/8.x/installation#directory-configuration

Laravel should always be served out of the root of the "web directory" configured for your web server. You should not attempt to serve a Laravel application out of a subdirectory of the "web directory".

Some GitHub issues related to this:

https://github.com/laravel/framework/issues/32236

https://github.com/laravel/framework/issues/32082

https://github.com/laravel/framework/issues/32832

https://github.com/laravel/framework/issues/34362

https://github.com/laravel/framework/issues/34977

https://github.com/laravel/framework/issues/35241

There are lot of arguments around saying a subdirectory in the domain is different then a subdirectory in the "root of the "web directory", but if you read through the issues you will see this is regarded as unsupported.

Clearing the route cache, as already suggested, seems to solve most use cases, but be aware other URL generation related problems can arise.

Hope it clears things up.

nettt's avatar
Level 15

Thank you for the reaction but it has nothing to do with installing laravel in a subdirectory. The applications is installed on my own domain and with a reverse proxy it's made available on the clients site.

The site from my client is a WordPress site and for search engine optimalisation reasons they want that my application is virtually running in their subdir/path (for example https://www.clientdomain.com/en/the-application/*). I can get the homeroute and all images running trough that proxy but all other routes are giving a 404.

It looks like Laravel doesn't understand the route mapping and when I do dd(url()->current()); in the web.php file it's giving me //test back when I go to the URL https://www.clientdomain.com/en/the-application/ on my own I got a full url.

1 like
rodrigo.pedra's avatar
Level 56

You're welcome =)

In some parts from my original response I said

Unfortunately serving an app from a sub-path is not supported by Laravel.

I didn't mean installing, I meant serving.

Actually you could install Laravel in any path within your server, as long you configure your web server to serve the app from the domain's root path.

What is not supported is serving a Laravel app using a sub-path within a domain as its base path.

It boils down to how Laravel handles a request URL. And how it then uses the request URL to generate/manage subsequent URL within an app.

When Laravel build the Request object from a HTTP request, which is used as the base for the UrlGenerator object, it is very opinionated on how to handle URL's paths, and therefore assumes the path is being served from a domain's root path.

If your read through the issues I linked, you will find people presenting arguments similar to yours, but official instance is that issues related to such setup are not considered bugs.

I understand it sounds as something they should support, but since Laravel 7 (in a minor update, onwards, as reported on those issues) several problems related to such setup started arising in Laravel's GitHub issue tracker, and the official instance is: this setup is not supported, and issues related to that are not considered bugs.

I apologize if my first response wasn't clear enough, I am not a native English speaker, but unfortunately this is the how things are at the moment. Maybe they will add support for it in the future.

You can try opening an issue within their repository if you think your scenario is different than what was already discussed on the linked issues.

Also, some comments in those issues report workarounds that might help you in your use-case.

Another option is using Laravel version 6, which some users in those issues reported as the last version where this setup didn't bring issues.

Hope it is clearer. Good luck.

1 like
nettt's avatar
Level 15

Thank you for explaining this to me! We will make the application visible on a subdomain.

1 like
rodrigo.pedra's avatar

You're welcome!

I know it is not the outcome you expected, but things are the way they are. Glad you got it working.

Have a nice day =)

Please or to participate in this conversation.