Running Laravel behind Traefik as reverse proxy, with a Path Prefix (eg /api/ => Laravel).
Laravel is served by Nginx and Php-fpm.
Laravel use Symfony HTTP foundation to generate route URL.
Symfony is not seeing correctly the base path, and generate URL without /api/ prefix.
As a dirty workaround, I fixed it by doing as the 1st line on index.php:
$_SERVER['SCRIPT_NAME'] = '/api/' . $_SERVER['SCRIPT_NAME'];
How can I force the full URL or the base path?
This was fixed by Symfony team, for Symfony 5.2:
my original issue: https://github.com/symfony/symfony/issues/36809
the PR: https://github.com/symfony/symfony/pull/37734
Related
i have a setup which contain a domain, say:
https://my-apps-xyz.io => this is just a random/imaginary domain. An example.
I have setup in Kubernetes virtual service several apps to have the same domain but redirect in their respective deployment through the path.
So,
https://my-apps-xyz.io/app1 => app1 deployment/pod
https://my-apps-xyz.io/app2 => app2 deployment/pod
I have already successfully setup a redirection to 2 Nuxt.js apps. The third one, i want to temporarily configure for a Laravel App which is using Nginx as a reverse proxy.
How can i make/configure so the path based url successfully maps/routes in the Laravel app, is it more convenient to configure on Nginx level or Laravel app.
I tried already in Laravel
public function map()
{
Route::prefix('app3')->group(function () {
$this->mapApiRoutes();
});
//
}
But, the issue with this approach is that the bundled app.js and main.css are mapped into
https://my-apps-xyz.io/js/app.js
https://my-apps-xyz.io/css/main.css
instead of
https://my-apps-xyz.io/app3/js/app.js
https://my-apps-xyz.io/app3/css/main.css
Which ofcourse gives 404 error. How can i encompass both the web routes and api routes in Laravel to be prefixed with that path. Or better, if it's better to be done solely on nginx level, i welcome suggestions.
I already tried something like this in nginx default.conf file
location /app3/ {
root /var/www/html/app3/public;
try_files $uri $uri/ /index.php?$query_string;
}
Thanks in advance.
Hello … l am finish Building website using Laravel and jQuery and bootstrap it's working good in local but when I upload to Heroku the file jQuery and bootstrap not working … it's work in local using http but in Heroku its need https its not working but when write http substitute of https it's working good like local and display Not Secure .. now any body know how can i allow website using https in Heroku or How can selection this problem
You should closely read all of Heroku's guide to getting started with Laravel.
The section titled "Trusting the Load Balancer" will resolve your issues.
Because of this:
This means that requests received by a dyno will have the last router’s IP address in the REMOTE_ADDR environment variable, and the internal request will always be made using the HTTP protocol, even if the original request was made over HTTPS.
Laravel sees HTTP requests coming in to the application, so it serves HTTP URLs for your various routes and asset URLs. As far as it knows, you're browsing via HTTP. The fix is to trust Heroku's "forwarded for" headers in your app's App\Http\Middleware\TrustProxies middleware:
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;
class TrustProxies extends Middleware
{
protected $proxies = '*';
protected $headers = Request:: HEADER_X_FORWARDED_AWS_ELB;
}
Had this issue myself awhile back, there are a few options and some are more heavy handed than others. If you want to gauruntee that everything is always https no exceptions first update your APP_URL to 'https://example.com' then in the boot method of your AppServiceProvider add Url::forceScheme('https');
The less heavy handed option is to find all of the places you use the asset() helper and change it to secure_asset instead. The asset helper should use your APP_URL to know the request is https but in my experience I couldn't rely on that so use secure_asset to make sure
I'm using Laravel 5.4 and have a question.
Is it possible to generate an https URL of the specific URL? I have a URL, e.g: login, and want to open it in https, not HTTP.
how can I force Laravel to open url('login') in https mode?
There is helper function secure_url(). The secure_url function generates a fully qualified HTTPS URL to the given path for e.g
$url = secure_url('user/profile');
If you want only some links in https, you can try this (force a group of routes to https):
Route::group(['scheme' => 'https'], function () {
// Route::get(...)->name(...);
});
Similar to this question: How to force Laravel Project to use HTTPS for all routes?
I try to run my local copy of my yii2 site with https.
I use this in config to force http url to https
'on beforeRequest' => function ($event) {
if(!Yii::$app->request->isSecureConnection){
$url = Yii::$app->request->getAbsoluteUrl();
$url = str_replace('http:', 'https:', $url);
Yii::$app->getResponse()->redirect($url);
Yii::$app->end();
}
},
The only url I can reach is the home page i.e. a bare url such as
example.ext
Other URLs give
Not Found The requested URL /site/index was not found on this server.
When removing the 'onbeforerequest' in the config, I can reach every http URL.
Question: why https URLs become unreachable?
Eventually I made out that there was no url rewrite for pretty url in the virtualhost litening to 443 port.
Adding the recommended rewrite rule in it solved the problem.
#stfsngue: Thank you for comment
Do you see any particular reason for preferring .htacces to 'onbeforeRequest' to force https?
At work we have a single staging server with a staging domain, something like https://staging.example.com. We recently decided to switch from Apache to NGINX on a new server and we're having issues with our Laravel routing.
All of our laravel apps sit in sub-directories on the staging server, like so.
https://staging.example.com/app1/public
https://staging.example.com/app2/public
I've tried configuring the NGINX conf file as specified in the Laravel docs but get a 404 when accessing any 2nd level route, i.e. https://staging.example.com/app1/public/a/b
Using something like the below config, I can access all the routes in an app.
location #laravel {
rewrite /app1/public/(.*)$ /app1/public/index.php?$1;
}
location / {
try_files $uri $uri/ #laravel;
}
However, we have many apps hosted on this server and we don't want to have to update an NGINX conf file every time we want to add an app to the server.
Is there a way of constructing a rewrite to apply to any sub-directory and keep Laravel's routing system working?
Note: I've also tried this rewrite rewrite (.*)/(.*)$ $1/index.php?$2 and that doesn't work for 2nd level routes.
Your first capture is probably too greedy, you should limit it by using:
rewrite ^(/[^/]+/[^/]+)/(.*)$ $1/index.php?$2 last;
See this useful resource on regular expressions.