首页 > 解决方案 > 获取 https://myurl.com/static/js/main.c6e1854c.chunk.js net::ERR_ABORTED 404(未找到)

问题描述

我正在使用 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 中的内容 - 没有加载,因为它缺少子目录路径

标签: reactjsnginxhttp-status-code-404

解决方案


解决了问题。我只需要:

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

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


推荐阅读