【问题标题】:Why doesn't Apache allow access to files outside the web root from within index.html?为什么 Apache 不允许从 index.html 中访问 Web 根目录之外的文件?
【发布时间】:2018-08-04 19:39:36
【问题描述】:

我的项目有以下组织,但我无法从我的 HTML 代码中访问相关的 CSS 和 JavaScript 文件,除非我创建从文件到我的 Web 根目录的符号链接。

这是 Apache 的正常行为还是问题出在其他地方?

如果我删除符号链接并更正 src 属性中的路径,这些外部文件的内容将无法访问。

userName@hostName:/var/www/test$ tree
.
├── css
│   └── style.css
├── html
│   ├── code.js -> ../js/code.js
│   ├── index.html
│   ├── jquery-3.2.1.js -> ../libs/jquery-3.2.1.js
│   └── style.css -> ../css/style.css
├── js
│   └── code.js
└── libs
    └── jquery-3.2.1.js

4 directories, 7 files

userName@hostName:/var/www/test$ less html/index.html

<!doctype HTML>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" type="text/css" href="./style.css">
      <title>Test</title>
   </head><!-- head -->
   <body>
     <h1>LOCAL TESTING SITE..</h1>
   </body><!-- body -->
    <script src="./code.js"></script>
</html><!-- html -->

html/index.html (END)

^ 作品

userName@hostName:/var/www/test$ tree
.
├── css
│   └── style.css
├── html
│   ├── index.html
├── js
│   └── code.js
└── libs
    └── jquery-3.2.1.js

4 directories, 4 files

userName@hostName:/var/www/test$ less html/index.html

<!doctype HTML>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" type="text/css" href="../css/style.css">
      <title>Test</title>
   </head><!-- head -->
   <body>
     <h1>LOCAL TESTING SITE..</h1>
   </body><!-- body -->
    <script src="../js/code.js"></script>
</html><!-- html -->

html/index.html (END)

^ 不起作用

userName@hostName:/var/www/test$ tree -p
.
├── [drwxrwxr-x]  css
│   └── [-rw-rw-r--]  style.css
├── [drwxrwxr-x]  html
│   ├── [lrwxrwxrwx]  code.js -> ../js/code.js
│   ├── [-rw-rw-r--]  index.html
│   ├── [lrwxrwxrwx]  jquery-3.2.1.js -> ../libs/jquery-3.2.1.js
│   └── [lrwxrwxrwx]  style.css -> ../css/style.css
├── [drwxrwxr-x]  js
│   └── [-rw-rw-r--]  code.js
└── [drwxrwxr-x]  libs
    └── [-rw-rw-r--]  jquery-3.2.1.js

4 directories, 7 files

^ 文件权限

userName@hostName:/etc/apache2$ less sites-available/test.local.conf

<VirtualHost *:80>
        ServerAdmin myEmail@email.com
        DocumentRoot /var/www/test/html
        ServerName test.local
        ErrorLog ${APACHE_LOG_DIR}/test.local.error.log
        CustomLog ${APACHE_LOG_DIR}/test.local.access.log combined
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

sites-available/test.local.conf (END)

^ 虚拟主机配置

userName@hostName:/var/www/test$ uname -v

#35~16.04.1-Ubuntu


userName@hostName:/var/www/test$ apache2 -v

Server version: Apache/2.4.18 (Ubuntu)

^ 系统信息

【问题讨论】:

  • Stack Overflow 是一个编程和开发问题的网站。这个问题似乎离题了,因为它与编程或开发无关。请参阅帮助中心的What topics can I ask about here。也许Web Applications Stack ExchangeWebmaster Stack ExchangeUnix & Linux Stack Exchange 会是一个更好的提问地点。
  • 更糟糕的是 - 我没有得到我需要的信息。怎么会有人如此讨厌另一个人,并恶意拒绝我的问题呢?这到底是什么意思?就像我要受到惩罚一样?太恶心了!
  • 您将收获您所播种的先生们
  • 我不相信 Stack Overflow 上的任何人讨厌你。

标签: linux apache web file-permissions configuration-files


【解决方案1】:

出于安全原因,Apache 不会(默认情况下)允许 HTTP 请求访问网站根目录之外(上方)的文件。

你的文档根目录设置为:DocumentRoot /var/www/test/html,所以所有文件(包括非HTML文件)都需要在这个目录下。不要仅仅因为目录名为html 而误以为它应该只包含 HTML 文件。

工作目录结构

userName@hostName:/var/www/test/html$ tree
.
├── css
│   └── style.css
├── js
│   └── code.js
├── libs
│   └── jquery-3.2.1.js
└── index.html

3 directories, 4 files

userName@hostName:/var/www/test/html$ less index.html

<!doctype HTML>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" type="text/css" href="css/style.css">
      <title>Test</title>
   </head><!-- head -->
   <body>
     <h1>LOCAL TESTING SITE..</h1>
   </body><!-- body -->
   <script src="js/code.js"></script>
</html><!-- html -->

index.html (END)

您会看到所有文件都存储在/html 目录或其子目录中。 JS 和 CSS 文件的 HTML 链接已更新,以引用正确的文件位置。

如果 Apache 允许访问根目录之外(以上)的文件,您将遇到主要安全问题。

例如,假设某人有一个执行以下操作的 HTML 文件:

<!doctype HTML>
<html>
   <head>
      <title>Test</title>
   </head>
   <body>
     <a href="../../../var/private/supersecret.conf">link to secret config file</a>
   </body>
</html>

这将允许用户访问他们想要在服务器上的任何随机文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 2017-12-29
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多