无法使用 SPA 和 API 配置基本 Nginx 服务器

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

我是 Nginx 新手,我正在尝试基本场景:为我的 Angular SPA 提供服务并公开在端口 8000 上的同一服务器上运行的 API。 这是 /etc/nginx/nginx.conf 文件中的片段

server {
    listen 80;
    location / {
        root /var/www/html;
        try_files $uri $uri/ /index.html;
        index index.html;
    }
    location /api/ {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        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;
        proxy_cache_bypass $http_upgrade;
    }
    error_log /var/log/nginx/error.log debug;
}

通过此配置,我可以在 http://{my-ip} 上看到我的 Angular 应用程序 但尝试访问 http://{my-ip}/api/{my-resourse-name} 上的 API 总是会导致 Nginx 404。

我已经通过发出curl 请求来确保我的API 确实在服务器上运行良好。而且我的防火墙已禁用(因此端口 80 可用)。 还尝试从

/
 中删除尾随 
location /api/

我需要尝试以下操作吗?

location /api {
    proxy_pass http://localhost:8000$request_uri;
}

有点不愿意在不知情的情况下做随机的事情。 看起来请求根本没有传递到

localhost:8000
。 任何帮助将不胜感激。

nginx nginx-reverse-proxy nginx-config
© www.soinside.com 2019 - 2024. All rights reserved.