SPA 和 API 之间的 NGINX 反向代理 - API 路由返回 404 的问题

问题描述 投票:0回答:1

我有以下设置:

NextJs SPA 在 localhost:3000 上运行

AspNetCore API 在本地主机上运行:55379

然后我在它们之间添加了 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。 我在这个设置中缺少什么?

nginx reverse-proxy
1个回答
0
投票

我尝试了一些设置和示例并设法让它工作!

这是我在 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.