大家
我有一个使用Laravel 5.4(最新版本)构建的网站,我每天都会在我的日志上多次收到“SuspiciousOperationException:Invalid Host error”。即使它似乎没有影响我的网站的功能,我担心它可能是一些错误配置的症状 - 并且,至少,它正在污染我的日志文件,使错误跟踪更难。
我试图找出自己造成这种情况的原因,但我缺乏经验表明......我对可能导致这种情况的原因有所了解,但我没有确认我的理论,我也不知道如何解决这个问题。
错误消息如下:
[2017-08-03 23:47:03] production.ERROR: Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException: Invalid Host "*.example.com". in /var/www/laravel/vendor/symfony/http-foundation/Request.php:1283
堆栈跟踪:
#0 /var/www/laravel/vendor/symfony/http-foundation/Request.php(1088): Symfony\Component\HttpFoundation\Request->getHost()
#1 /var/www/laravel/vendor/symfony/http-foundation/Request.php(1118): Symfony\Component\HttpFoundation\Request->getHttpHost()
#2 /var/www/laravel/vendor/symfony/http-foundation/Request.php(1815): Symfony\Component\HttpFoundation\Request->getSchemeAndHttpHost()
#3 /var/www/laravel/vendor/symfony/http-foundation/Request.php(1102): Symfony\Component\HttpFoundation\Request->prepareRequestUri()
#4 /var/www/laravel/vendor/symfony/http-foundation/Request.php(1867): Symfony\Component\HttpFoundation\Request->getRequestUri()
#5 /var/www/laravel/vendor/symfony/http-foundation/Request.php(985): Symfony\Component\HttpFoundation\Request->prepareBaseUrl()
#6 /var/www/laravel/vendor/symfony/http-foundation/Request.php(1933): Symfony\Component\HttpFoundation\Request->getBaseUrl()
#7 /var/www/laravel/vendor/symfony/http-foundation/Request.php(945): Symfony\Component\HttpFoundation\Request->preparePathInfo()
#8 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Http/Request.php(138): Symfony\Component\HttpFoundation\Request->getPathInfo()
#9 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Matching/UriValidator.php(19): Illuminate\Http\Request->path()
#10 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php(259): Illuminate\Routing\Matching\UriValidator->matches(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#11 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php(193): Illuminate\Routing\Route->matches(Object(Illuminate\Http\Request), true)
#12 [internal function]: Illuminate\Routing\RouteCollection->Illuminate\Routing\{closure}(Object(Illuminate\Routing\Route), 'api/user')
#13 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Support/Arr.php(175): call_user_func(Object(Closure), Object(Illuminate\Routing\Route), 'api/user')
#14 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php(194): Illuminate\Support\Arr::first(Array, Object(Closure))
#15 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php(164): Illuminate\Routing\RouteCollection->matchAgainstRoutes(Array, Object(Illuminate\Http\Request))
#16 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(548): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
#17 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(527): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#18 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(513): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#19 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#20 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#21 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#22 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#23 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#24 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#25 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#27 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#28 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#31 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#34 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#37 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#39 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#40 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#41 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#42 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#43 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#44 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#45 /var/www/laravel/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#46 {main}
我认为可能与之相关的配置数据如下:
.ENV:
APP_URL=example.com
(我尝试过以下几个其他值:)
http://www.example.com
http://*.example.com
配置/应用程式:
'url' => env('APP_URL', 'example.dev')
我的网站适用于http://example.com和http://www.example.com;我注意到他们之间没有分享会话,但其他一切都是一样的。
非常感谢任何解决此问题的帮助。先感谢您!
编辑:修改域名
答案 0 :(得分:2)
我遇到了同样的问题,但对我来说,从我的nginx server_name
指令中删除通配符并不是一个选择。 (我的网站取决于动态子域路由)。
当server_name中存在通配符时,问题与nginx未正确设置的PHP变量$_SERVER['SERVER_NAME']
有关。要修复它,您必须在nginx配置中添加以下内容:
fastcgi_param SERVER_NAME $host
例如:
location ~ \.php$ {
...
fastcgi_param SERVER_NAME $host
...
}
答案 1 :(得分:0)
我终于找到了造成这种情况的原因:这是nginx的配置。
我已将服务器的server_name指令配置为$ctrl.registerInfo = registerInfo;
- 这是指令的有效通配符名称,如http://nginx.org/en/docs/http/server_names.html所示,但似乎与Laravel不兼容。我没有抛出这个异常,因为几天前我将值改为*.example.com
,所以这绝对可以解决问题。