【问题标题】:Deploy express server that serves react behind nginx部署服务于 nginx 后面的快速服务器
【发布时间】:2018-07-03 16:31:34
【问题描述】:

我在一个 ec2 实例上有一个快速服务器,它有一个 api (/api) 和一个客户端(所有不是 /api 在反应中处理的东西。)

转到http://ip.address:3000 工作正常。它显示了应用程序,一切正常。

但是去https://ip.address(由nginx转发)不能正常工作。它正确加载了我的 index.html,但在所有 /static/bundle.js/static/bundle.css 文件上加载了 404。

nginx

    # redirect to node for the dynamic stuff
    location / {
            proxy_pass https://localhost:8003/index.html;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;

            proxy_hide_header X-Powered-By;
    }

表达

let router: express.Router = express.Router();
router.use(express.static(path.join(__dirname, "/build")));

const api: ApiRouter.Api = new ApiRouter.Api();
router.use("/api", api.router.bind(api.router));

//Catch all for react - client side routing
router.get("*", function(req, res) {
  res.sendFile(path.resolve(__dirname, "/build/index.html"));
});

【问题讨论】:

    标签: reactjs express nginx


    【解决方案1】:

    如果你告诉 Nginx

    proxy_pass https://localhost:8003/index.html;

    然后,与该位置块匹配的每个请求都将其 URL 路径的匹配部分替换为您添加到 proxy_pass URL 的任何路径。

    您的位置块只是/ 因此,如果我请求www.yoursite.com/static/bundle.js,那么Nginx 将匹配第一个/,将其替换为您的路径并添加其余部分。所以它会尝试服务www.yoursite.com/index.htmlstatic/bundle.js

    你也应该通过 Nginx 代理 /api,如果静态文件来自上游代理,你也应该在 Nginx 中启用代理缓存

    【讨论】:

    • 但是去ip.address/api 工作只是不是静态内容。或者我理解错了
    • 很难在没有看到完整的 Nginx 配置的情况下发表评论。你有/api 的另一个位置块吗?您还有哪些其他指令?它在监听什么端口等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    相关资源
    最近更新 更多