Laravel + Nginx: POST-запрос возвращает "Method Not Allowed", несмотря на правильный метод и тело запроса
После того как проблема с навигацией нжинкс была решена: https://stackoverflow.com/questions/79657722/nginx-try-files-directive-uses-an-incorrect-fallback-uri, она заработала корректно, по запросу к http://localhost/subscription перенаправляется в subscription_service. Однако теперь я столкнулся с новой проблемой при отправке POST-запросов.
Когда я отправляю запрос на POST эндпоинт, получаю следующую ошибку:
Method Not Allowed The GET method is not supported for route /. Supported methods: POST.
Он даже не доходит до начала метода create()
Но запрос на GET эндпоинты работают прекрасно, попадает в контроллер и логика обрабатывается вместе с респонсом.
Настройка проекта:
- Laravel-проект
- Middleware VerifyCsrfToken отключен для всех эндпоинтов
В public/index.php я проверил, что: - Метод запроса корректно определяется как POST
- Тело запроса успешно парсится (приходит объект, который я отправляю)
nginx.conf
server {
listen 80;
server_name localhost;
location /subscription {
alias /var/www/html/public;
index index.php index.html index.htm;
try_files $uri $uri/ @subscription;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass subscription_service:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
expires max;
log_not_found off;
}
}
location @subscription {
rewrite ^ /subscription/index.php last;
}
}
web.php
Route::get('/{user_id}', [SubscriptionController::class, 'get']);
Route::post('/', [SubscriptionController::class, 'create']);
SubscriptionController.php
public function create(CreateSubscriptionRequest $request): JsonResponse
{
dd(1);
$validated = $request->validated();
$subscription = new Subscription($validated);
$subscription->save();
return response()->json([
'message' => 'Subscription created successfully.',
'data' => $subscription,
], 201);
}
public function get(int $userId): JsonResponse
{
return response()->json(['error' => 'Not found'], 404);
}
CreateSubscriptionRequest.php
class CreateSubscriptionRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'user_id' => 'required|exists:users,id',
'type' => 'required|string|in:basic,premium,enterprise',
'start_date' => 'required|date',
'end_date' => 'required|date|after_or_equal:start_date',
];
}
}
cUrl
curl --location 'http://localhost/subscription' \
--header 'Content-Type: application/json' \
--header 'api-key: yFlMjSup.IbHOCjyRiTb8QOO9Ltsbr' \
--data '{
"user_id": 1,
"type": "pro",
"start_date": "2004-01-01",
"end_date": "2006-01-01"
}'