【问题标题】:GET https://myurl.com/static/js/main.c6e1854c.chunk.js net::ERR_ABORTED 404 (Not Found)获取 https://myurl.com/static/js/main.c6e1854c.chunk.js net::ERR_ABORTED 404(未找到)
【发布时间】:2020-03-01 22:45:33
【问题描述】:

我正在使用 react 和 nginx 来部署我的网站。问题是我在 url myurl.com/path 下部署了所有内容,但是当服务器尝试获取 js 文件时,它尝试从 root 获取 myurl.com 并且我看到了错误。

GET https://myurl.com/static/js/main.c6e1854c.chunk.js net::ERR_ABORTED 404 (Not Found)

如果我转到 https://myurl.com/path/static/js/main.c6e1854c.chunk.js - 我可以访问该文件。

我找不到应该添加路径以使服务器知道正确路径的位置。

有什么想法吗?

我的 nginx

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        try_files $uri /index.html;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

我怀疑问题不是 nginx,因为我可以看到部分网页已加载(其他一些脚本)。但是主 js 中的内容 - 没有加载,因为它缺少子目录 path

【问题讨论】:

  • 我没有使用过 React,但是使用 Angular 我必须将 try_files $uri $uri/ /index.html; 放在我的 nginx 文件中以进行路由。也许你可以适应它。欲了解更多信息,请查看angular.io/guide/deployment
  • 查看URL的不同之处:一个是myurl.com/static/...,另一个是myurl.com/path/...。当然可能是一个错字——在这种情况下,请修正它,因为它似乎是目前最明显的问题。
  • 所以你说你有一个反应应用程序,你想在一个子文件夹中托管,当你加载它时,HTML 是基于根目录而不是子文件夹的路径。对吗?
  • @EduardoPascualAseff,尝试了您的解决方案 - 无效。 @linux-fan,那里没有错字。 path 是我的路径名。 @ShawnC。是的。从根我托管不同的应用程序。我从 /path 发布了这个 react 应用程序。
  • 您需要设置您的 React 应用程序才能知道它位于子目录中,例如 skryvets.com/blog/2018/09/20/…

标签: reactjs nginx http-status-code-404


【解决方案1】:

已解决问题。我只需要:

  1. PUBLIC_URL=https://myurl.com/path添加到.env文件中;
  2. 通过添加前缀path/修改所有资产路径。

感谢大家的帮助和正确指导!

【讨论】:

    猜你喜欢
    • 2022-11-02
    • 1970-01-01
    • 2021-06-11
    • 2020-01-20
    • 1970-01-01
    • 2022-11-21
    • 2020-12-29
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多