【问题标题】:Nginx try_files loads blank page when passed with variable pathNginx try_files 在使用变量路径传递时加载空白页面
【发布时间】:2017-07-27 05:07:11
【问题描述】:

我正在设置 nginx 以使用 React SPA。当我不包含变量路径 (/rsvp/) 时,页面加载正常。但是当我尝试 (/rsvp/S0mEtH1NG) 时,它会下载索引、清单、css 文件......但它实际上并没有将这些东西应用到 index.html。我几乎只是看到我的 index.html 没有实际加载。

我的配置看起来像这样:

server {
    listen 80;
    server_name something.com;

    location / {
        root /srv/something.com/webroot;
        index index.html;
    }

    location /rsvp {
        alias /srv/rsvp/build;
        try_files $uri $uri/ /rsvp/index.html;
    }
}

如果我有try_files $uri $uri/ /index.html,它将加载根目录 (/) 的 index.html,但存在类似的问题,即它不使用任何 css。

感谢您的帮助,非常感谢!

【问题讨论】:

  • 您缺少;
  • 谢谢! (已修复,但只是手动输入配置)

标签: nginx single-page-application nginx-location


【解决方案1】:

有一个long standing issuealiastry_files。我会避免在同一个块中使用这些指令。试试:

location /rsvp {
    alias /srv/rsvp/build;
    if (!-e $request_filename) { rewrite ^ /rsvp/index.html? last; }
}

请参阅this caution 了解if 的使用。

【讨论】:

  • 嗯...仍然没有运气。在这两种情况下,我的 index.html 都会被加载,而在查看网络选项卡时,其他文件似乎可以正常下载。它只是不应用 javascript 或 css。您认为这可能不是 nginx 错误吗?当我导航到 /rsvp/ 时一切正常,而不是 /rsvp/*。
  • 是的,看起来这是 react 路由器的问题。继续并标记答案正确,但对于别名/try_files 知识。谢谢。
  • alias 指令不能在命名位置块内使用
  • 这是alias 指令的限制,所以这是我的情况,并不是答案有问题。不过,我对你的回答投了赞成票。
  • 在命名的location 中使用root 而不是alias。否则,将问题的详细信息和您的配置作为新问题发布。谢谢@AamirR
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 2011-08-11
  • 2011-05-25
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
相关资源
最近更新 更多