【问题标题】:How to serve HTML from multiple JS, CSS directory in nginx如何在 nginx 中从多个 JS、CSS 目录提供 HTML
【发布时间】:2018-12-05 04:06:55
【问题描述】:

我的目录结构是-

project
|----theme
|    |----css
|    |----images
|    |----js
|
|----pages
|    |----css
|    |----images
|    |----js
|    |----index.html
|
|----libraries
|    |----lib1
|    |     |----lib1.js
|    |    
|    |----lib2
|    |     |----lib2.js

我的 nginx/sites-available 配置是-

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  root /home/ubuntu/project/pages;
  index index.html;
  server_name mydomain.com www.mydomain.com;

  location ~* \.(js|jpg|png|css)$ {
    root /home/ubuntu/project;   -----(A)
  }
}

我在 pages/index.html 中引用了“主题”和“库”中的文件。 但是控制台在尝试加载这些文件时会输出 404 错误。 如何正确提供这些文件?

如果我在 (A) 中为 root 编辑并附加“/pages”,则会提供“pages” JS,但随后又找不到“theme”和“libraries”中的文件(在控制台中显示 404 错误)。

【问题讨论】:

  • 你喜欢什么网址?给出 404 的那些
  • URL 的格式为 - example.com/js/customjs/script_file.js 。该文件以“pages/js/customjs/script_file.js”的形式存在(文件名以蛇形大小写)。

标签: javascript nginx hosting


【解决方案1】:

您需要使用try_files 来尝试不同的位置

location ~* \.(js|jpg|png|css)$ {
  root /home/ubuntu/project;
  try_files $uri $uri/ /pages$uri /theme$uri =404;
}

【讨论】:

  • 有什么问题?
  • 根据我的结构,该问题无法访问主题和库目录中的文件。
猜你喜欢
  • 2017-05-10
  • 2014-07-09
  • 1970-01-01
  • 2011-04-10
  • 2015-12-03
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
相关资源
最近更新 更多