首页 > 解决方案 > NginX : 为 Node API 提供静态内容和代理传递

问题描述

我正在运行一个网站,该网站由端口 8080 上的 docker 中的 API 和包含前端反应应用程序的静态文件夹组成。

api位于/api

我正在尝试配置 NginX 以正确地提供此配置,但我无法弄清楚如何让两者都工作。

如果我default这样设置:

server {
    root /var/www/html;
        server_name DOMAIN; # managed by Certbot

    location @nodeapp {
        # Redirect to the api
        proxy_pass   http://localhost:8080;
    }

    location / {
        # Map all the routes to the index.html
        try_files $uri @nodeapp;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/DOMAIN/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/DOMAIN/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

然后一切都被重定向到我的 API,如果没有明确指定(例如转到https://host/index.html) ,则不会传递静态内容

如果我添加try_files $uri $uri/ @nodeapp;以将根文件夹作为目录提供服务,则索引将被提供,但我无法再访问 API,它始终为反应应用程序提供服务

我也尝试添加

location /api/ {
    proxy_pass   http://localhost:8080
}

但是没有区别

我究竟做错了什么 ?

标签: node.jsnginx

解决方案


我找到了答案,我使用的 React 应用程序是使用服务人员设置的,这会弄乱路由。我删除了它,它现在就像一个魅力!


推荐阅读