【问题标题】:can not access route on nginx using express无法使用 express 访问 nginx 上的路由
【发布时间】:2019-04-03 09:00:41
【问题描述】:

我使用 React js 创建了一个站点,我对 Nginx 进行了一些更改以重写路由到文件,因此我不会收到 404 错误,最近我尝试使用 express,如果我为单页应用程序购买配置 nginx 使用: 位置 / { try_files $uri $uri/ /index.html; } 当我尝试访问 /api 时,我会从我的应用程序中获取 notFound 页面,如果我不配置它,我可以访问我的快速服务器,但是当我尝试直接访问除主页之外的 url 时,站点显示 notFound 由 nginx ,

所以我正在为 nginx 寻找一些异常配置, 我想重写索引html的所有路由,除了任何以/api/*开头的url

请拯救我的生命 :) 我在互联网上找不到解决方案 亲切的问候

【问题讨论】:

  • 您好,欢迎来到 StackOverflow :) 看来您的问题不是很详细,因此不容易提供帮助。您可能想阅读stackoverflow.com/help/how-to-ask 并以一种对可以帮助您的人来说清楚的方式提出您的问题!

标签: reactjs express nginx


【解决方案1】:

虽然不清楚你有什么样的架构,但我会假设 React 应用程序运行在某个端口上,例如3000 和后端服务器 /api 在端口 5000 上。

这是一个非常基本的 nginx 配置示例,它将所有 /api 请求路由到服务器(端口 5000),其余请求路由到 React 应用程序。

upstream client {
    server client:3000;
}

upstream api {
    server api:5000;
}

server {
    listen 80;

    location / {
        proxy_pass http://client;
        // try_files $uri $uri/ /index.html; // to redirect to the index page
    }

    location /api {
        proxy_set_header X-Forwarded-For $remote_addr;
        rewrite /api/(.*) /$1 break;
        proxy_pass http://api;
    }    
}

【讨论】:

    猜你喜欢
    • 2016-06-22
    • 2021-12-28
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    相关资源
    最近更新 更多