解決 ngrok 的 Domain 錯誤問題

ycs77_Lucas發表於2020-09-22

在 AppServiceProvider 中增加以下程式碼:

app/Providers/AppServiceProvider.php

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->setHttpHost();
    }

    /**
     * Fix ngrok URL domain is incorrect bug.
     *
     * @return void
     */
    protected function setHttpHost()
    {
        if (empty(request()->getTrustedProxies())) {
            if (request()->server->has('HTTP_X_ORIGINAL_HOST')) {
                request()->server->set('HTTP_HOST', request()->server->get('HTTP_X_ORIGINAL_HOST'));
                request()->headers->set('HOST', request()->server->get('HTTP_X_ORIGINAL_HOST'));
            }
        } else {
            if (request()->server->has('HTTP_X_ORIGINAL_HOST')) {
                request()->server->set('HTTP_X_FORWARDED_HOST', request()->server->get('HTTP_X_ORIGINAL_HOST'));
                request()->headers->set('X_FORWARDED_HOST', request()->server->get('HTTP_X_ORIGINAL_HOST'));
            }
        }
    }
}

然後配置 APP_URL 即可看到正確的 Domain。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章