简体   繁体   English

Laravel 使用 DigitalOcean App 部署 - HTTP 问题

[英]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.我使用来自 GitHub 的“App”服务将 Laravel 9 应用程序(使用 Jetstream/Livewire)部署到 DigitalOcean。部署很顺利,CI/CD 工作正常。

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").在浏览器中查看应用程序时,我注意到资产(CSS 和 JS)正在使用 HTTP URL 提供服务。现代浏览器都不喜欢这样(称为“混合内容”)。 So I configured Vite ( /vite.config.js ) to compile the assets using HTTPS. Now they work.所以我配置了 Vite ( /vite.config.js ) 使用 HTTPS 来编译资产。现在它们可以工作了。

However, Laravel itself insists on using HTTP when building URL's within the Blade templates ( url() and route() ).但是,Laravel 本身在 Blade 模板( url()route() )中构建 URL 时坚持使用 HTTP。 For instance, on the login page, the login form action is http://mywebsite.com .例如,在登录页面上,登录表单操作是http://mywebsite.com

I have tried:我试过了:

  1. Editing AppServiceProvider.php and adding \Illuminate\Support\Facades\URL::forceScheme('https');编辑 AppServiceProvider.php 并添加\Illuminate\Support\Facades\URL::forceScheme('https'); to the boot() methodboot()方法
  2. Setting proxies to '*' in TrustProxies middleware在 TrustProxies 中间件中将代理设置为'*'
  3. Adding all of the CloudFlare IP's to the proxies property of TrustProxies middleware将所有CloudFlare IP添加到 TrustProxies 中间件的代理属性
  4. Setting APP_URL and ASSET_URL to https://mywebsite.com in.envAPP_URLASSET_URL设置为https://mywebsite.com in.env
  5. Clearing the caches after changing the settings by php artisan optimize:clear通过php artisan optimize:clear更改设置后清除缓存

But none of this has helped and the forms (and other URL's) are generated under the HTTP scheme.但这些都没有帮助,forms(和其他 URL)是在 HTTP 方案下生成的。 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)?我猜反向代理设置令人困惑 Laravel。什么是正确的 Laravel 设置以帮助它与 DigitalOcean App 服务(使用 Heroku 和 CloudFlare?用于部署)很好地配合?

Turns out, forceScheme() should be added as事实证明, forceScheme()应该添加为

\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!因为它存在于Illuminate\Routing\UrlGenerator class 中。inte.nets 中的一些答案误导了我......不要让它误导你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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