【问题标题】:relative path js and css not found in 404html nginx static web site在 404html nginx 静态网站中找不到相对路径 js 和 css
【发布时间】:2019-10-25 03:31:16
【问题描述】:

我想在 nginx 中创建静态网站。 我需要一个 404.html 但它有一些 js 和 css 链接,路径是 root,吹

<link href="css/custom.css" rel="stylesheet">
<link href="js/custom.js" rel="stylesheet">

nginx

error_page 404 /404.html;

当我访问像http://www.test.com/xxx 这样不存在的文件网址时,404.html 正确显示。

但是当我用http://www.test.com/xxx/xxx 访问url 时,我得到一个错误的页面,打开控制台,我发现我的css 和js 路径改变了http://www.test.com/xxx/custom.css

我看看我是否将 js 和 css 文件的相对路径更改为绝对路径。 但我需要相对路径。 当 404 和 js/css 使用相对时,我希望得到正确的 404.html 及其 js 和 css 文件以及任何 url。

如何配置nginx或其他?

【问题讨论】:

    标签: html nginx


    【解决方案1】:

    尝试在 nginx 配置中使用位置

    error_page 404 /404.html;
    location = /404.html {
               root /usr/share/nginx/html;
               internal;
    }
    

    【讨论】:

    • 谢谢,我试试。我的根不是 /usr/share/nginx/html,所以我将路径更改为我的根路径并重新启动我的 nginx,我的 404html 中的 js/css 仍然显示加载资源失败:服务器响应状态为 404 ()
    【解决方案2】:

    不要在 nginx 中改变

    nginx 还是

    error_page 404 /404.html;
    

    修改html

    css/custom.css
    

    /css/custom.css
    

    与js文件相同

    这解决了我的问题

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 2019-08-04
      • 2017-02-09
      • 1970-01-01
      • 2020-06-05
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      相关资源
      最近更新 更多