简体   繁体   中英

Laravel deployed with DigitalOcean App - HTTP issue

I deployed a Laravel 9 app (with Jetstream/Livewire) to DigitalOcean using their "App" service from GitHub. Deployment was smooth and CI/CD works fine.

When viewing the application in the browser, I noticed that the assets (CSS and JS) are being served with a HTTP URL. None of the modern browsers like this (called "mixed content"). So I configured Vite ( /vite.config.js ) to compile the assets using HTTPS. Now they work.

However, Laravel itself insists on using HTTP when building URL's within the Blade templates ( url() and route() ). For instance, on the login page, the login form action is http://mywebsite.com .

I have tried:

  1. Editing AppServiceProvider.php and adding \Illuminate\Support\Facades\URL::forceScheme('https'); to the boot() method
  2. Setting proxies to '*' in TrustProxies middleware
  3. Adding all of the CloudFlare IP's to the proxies property of TrustProxies middleware
  4. Setting APP_URL and ASSET_URL to https://mywebsite.com in.env
  5. Clearing the caches after changing the settings by php artisan optimize:clear

But none of this has helped and the forms (and other URL's) are generated under the HTTP scheme. I am guessing that the reverse proxy setup is confusing Laravel. What are the right Laravel settings to help it play nicely with DigitalOcean App service (which uses Heroku and CloudFlare? for deployment)?

Turns out, forceScheme() should be added as

\URL::forceScheme('https');

and not as

\Illuminate\Support\Facades\URL::forceScheme('https');

Because it lives in the Illuminate\Routing\UrlGenerator class. Some answer in the inte.nets has mislead me... Don't let is mislead you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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