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

uqzxnwby  于 7个月前  发布在  Nginx
关注(0)|答案(1)|浏览(81)

我有以下设置:
在本地主机上运行的NextJs SPA: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。我在这个设置中遗漏了什么?

f0brbegy

f0brbegy1#

我玩了一些设置和例子,并设法让它工作!
下面是我在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;
}

字符串

相关问题