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

uqzxnwby  于 2023-11-17  发布在  Nginx
关注(0)|答案(1)|浏览(176)

我有以下设置:
在本地主机上运行的NextJs SPA:3000
本地主机上运行的AspNetCore API:55379
然后我在它们之间添加了NGINX反向代理以摆脱CORS问题,nginx.conf有以下设置

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. proxy_pass http://localhost:3000;
  6. }
  7. location ^~ /api/ {
  8. proxy_pass http://localhost:55379;
  9. }
  10. }

字符串
结果是NextJs应用程序正在从http://localhost正确提供服务,但是,API调用返回404
例如,使用以下URL从SPA调用登录路由

  1. http://localhost/api/user/Login


它应该代表

  1. http://localhost:55379/api/user/Login


但是这从来没有到达API,请求返回404。我在这个设置中遗漏了什么?

f0brbegy

f0brbegy1#

我玩了一些设置和例子,并设法让它工作!
下面是我在nginx.conf中的服务器设置,以供参考

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. proxy_pass http://localhost:3000;
  6. }
  7. location /api/ {
  8. proxy_pass http://localhost:55379;
  9. proxy_set_header Host $host;
  10. proxy_set_header X-Real-IP $remote_addr;
  11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12. proxy_set_header X-Forwarded-Host $server_name;
  13. proxy_set_header X-Forwarded-Proto $scheme;
  14. }
  15. access_log /Proxy/nginx-1.25.3/logs/access.log main;
  16. error_log /Proxy/nginx-1.25.3/logs/error.log debug;
  17. }

字符串

展开查看全部

相关问题