提问人:Jeekim 提问时间:11/11/2023 更新时间:11/11/2023 访问量:53
SPA 和 API 之间的 NGINX 反向代理 - API 路由返回 404 的问题
NGINX Reverse Proxy between SPA and API - issue with API routes returning 404
问:
我有以下设置:
下一个在 localhost:3000 上运行的 Js SPA
在 localhost:55379 上运行的 AspNetCore API
然后我在它们之间添加了 NGINX 反向代理以摆脱 CORS 问题,nginx.conf 具有以下设置
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:3000;
}
location ^~ /api/ {
proxy_pass http://localhost:55379;
}
}
结果是 NextJs 应用程序从 http://localhost 正确提供, 但是,API 调用返回 404
例如,使用以下 URL 从 SPA 调用登录路由
http://localhost/api/user/Login
哪个应该代理给
http://localhost:55379/api/user/Login
但这永远不会到达 API,请求返回 404。 我在此设置中缺少什么?
答:
0赞
Jeekim
11/11/2023
#1
我尝试了一些设置和示例,并设法让它工作!
以下是我在nginx.conf中的服务器设置,以供参考
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:3000;
}
location /api/ {
proxy_pass http://localhost:55379;
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-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
}
access_log /Proxy/nginx-1.25.3/logs/access.log main;
error_log /Proxy/nginx-1.25.3/logs/error.log debug;
}
评论