Столкнулся с проблемой маршрутизации запросов через Nginx. Конфигурация Nginx проверена и синтаксически правильна, однако запросы к /api/category возвращают ошибку 404.
Система
Windows 11
Nginx 1.27.2
Backend: Node.js + Express
логи nginx
127.0.0.1 - - [13/Oct/2024:18:12:39 +0300] "OPTIONS /api/category HTTP/1.1" 204 0 "http://localhost:5173/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0" "-"
127.0.0.1 - - [13/Oct/2024:18:12:41 +0300] "GET /api/category HTTP/1.1" 404 5 "http://localhost:5173/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0" "-"
Конфигурация Nginx
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
error_log logs/error.log debug;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
location /api/auth/ {
proxy_pass http://localhost:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/business/ {
proxy_pass http://localhost:5001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/category/ {
proxy_pass http://localhost:5001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
access_log /var/log/nginx/category_access.log main;
error_log /var/log/nginx/category_error.log debug;
}
location / {
proxy_pass http://localhost:5173;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
Конфигурация бэкенда (Express)
// index.js
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const businessRouter = require("./routes/business");
const categoryRouter = require("./routes/category");
dotenv.config();
const app = express();
const PORT = process.env.PORT || 5001;
app.use(bodyParser.json());
mongoose
.connect(process.env.DB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB connected"))
.catch((err) => console.log(err));
app.use("/api/business", businessRouter);
app.use("/api/category", categoryRouter);
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Роутинг
// routes/category.js
const express = require('express');
const router = express.Router();
const { createCategory, getAllCategories } = require('../controllers/categoryController');
router.post('/create', createCategory);
router.get('/', getAllCategories);
module.exports = router;
Проблема
Запросы к
localhost:5001/api/category напрямую работают нормально, но через Nginx возвращают ошибку 404.
Пробовал перезагружать Nginx и проверять логи, но ничего подозрительного не нашел.