【问题标题】:NginX : Serve static content and proxy pass to Node APINginX : 为 Node API 提供静态内容和代理传递
【发布时间】:2018-06-11 01:17:28
【问题描述】:

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

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,它始终为 react 应用提供服务

我也尝试添加

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

但没有区别

我做错了什么?

【问题讨论】:

    标签: node.js nginx


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2014-07-30
      • 2011-03-24
      • 2011-02-12
      • 2020-09-20
      相关资源
      最近更新 更多