托管时网站上不加载 Javascript/CSS 文件

标签 javascript html css nginx

当我尝试运行我的服务器(域是我的 IP 地址)时,它会加载基本 index.html,但我在我的目录中引用的所有文件(其他 CSS 文件、javascript 文件)都没有加载。
当我在 Chrome 浏览器上运行时,有趣的是,它会读取我的 index.html 文件目录中的所有 css/js 文件作为 index.html
在我的默认文件中(在/etc/nginx/sites-enabled[available]/default 中),我的代码如下所示:

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  root /usr/share/nginx/html;
  # Add index.php to the list if you are using PHP
  index index.html index.htm index.nginx-debian.html;
  server_name <ip-address-here>;
  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.

    try_files $uri /index.html;

    autoindex on; (I've tried with and w/o this)

    # proxy_pass http://localhost:8080;
    # proxy_http_version 1.1;
    # proxy_set_header Upgrade $http_upgrade;
    # proxy_set_header Connection 'upgrade';
    # proxy_set_header Host $host;
    # proxy_cache_bypass $http_upgrade;
  }
}
以前,我的文件位于名为/srv/www/xyz.me 的文件夹中。现在我搬到了/usr/share/nginx/html。搬家前后我的问题仍然存在。
这是我第一次使用 DigitalOcean。我已经按照教程来托管我的网站和类似的东西,但我不了解 Nginx 的来龙去脉。

最佳答案

所以我们可以尝试将以下内容添加到配置文件中:

location ~ \.css {
  add_header  Content-Type    text/css;
}
location ~ \.js {
  add_header  Content-Type    application/x-javascript;
}
我们也可以尝试将其添加到/etc/nginx/conf.d/default.conf

关于托管时网站上不加载 Javascript/CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73369967/

相关文章:

javascript无法获取数据索引的索引号

html - 奇怪的线突然出现?

html - 列表中的图像和文本内联

css - Tumblr 关注/仪表板按钮现在显示了吗?

html - 输入在 Internet Explorer 8 的文本区域中不起作用

CSS 网格列对齐

javascript - 我如何在 JavaScript 中按每第 n 个字符拆分字符串?

javascript - Next.js 与 pkg。语法要求启用以下解析器插件之一 : 'flow, typescript'

javascript - 从后到前显示列表

javascript - 如果未从根访问单页 Web 应用程序 (SPA),则保证 HTML 信息