Итак, я создаю прокси/шлюз, используя Laravel 9.x для маршрутизации запросов из приложения Frontend в API API.Example.com . Прокси размещается на другом домене proxy.domain.com , поэтому запрос маршрута IS Client -> Gateway (Proxy) -> API . Он работал нормально, пока я работал со всем этим на своем локальном ПК, но так как я перенес различные приложения в условия различных хостинговых средств, я получаю обнаружение 508 цикла , когда я нажимаю в Gatway (Proxy) либо через почтальон, либо через браузер
, что Proxy
public function proxy(Request $request, $any)
{
if ($request->hasHeader('X-FROM-GATEWAY')) {
return response()->json([
'message' => 'Loop detected: request already passed through gateway.',
], 508);
}
$backendBase = rtrim(config('services.backend.url'), '/');
$destinationURL = $backendBase . '/' . $any;
// Log::channel('gateway')->info('[GATEWAY] Incoming request: FWD', [
// 'method' => $request->method(),
// 'path' => $request->path(),
// 'url' => $destinationURL
// ]);
try {
$response = Http::withHeaders(array_merge(
$request->headers->all(),
[
'X-GATEWAY-TOKEN' => config('services.gateway.secret'),
'X-FROM-GATEWAY' => true
]
))->send($request->method(), $destinationURL, [
'query' => $request->query(),
'json' => $request->all(),
])->throw();
return response()->json($response->json(), $response->status());
} catch (RequestException $ex) {
// handle destination URL errors (4xx & 5xx)
// Log::channel('gateway')->error('[GATEWAY] Destination error', [
// 'url' => $destinationURL,
// 'status' => optional($ex->response)->status(),
// // 'body' => optional($ex->response)->body()
// ]);
return response()->json([
'url' => $destinationURL,
'message' => 'Destination returned an error',
'details' => optional($ex->response)->json() ?? $ex->getMessage(),
], optional($ex->response)->status() ?? 500);
} catch (Exception $e) {
// handle connection errors & timeouts
Log::channel('gateway')->critical('[GATEWAY] Failed to connect to destination URL', [
'url' => $destinationURL,
'error' => $e->getMessage()
]);
return response()->json([
'url' => $destinationURL,
'message' => 'Unable to reach the destination: ' . $destinationURL,
'error' => $e->getMessage()
], 502);
}
}
< /code>
Теперь этот блок < /p>
if ($request->hasHeader('X-FROM-GATEWAY')) {
return response()->json([
'message' => 'Loop detected: request already passed through gateway.',
], 508);
}
< /code>
это то, что я пытался добавить в проверку. Я также попытался снять журнал, думая, может быть, это проблема. Я не знаю, что делает цикл, потому что в журнале серверов есть журнал: 508 ошибка
Подробнее здесь: https://stackoverflow.com/questions/796 ... aravel-9-x