1

php -S localhost:8000用作我的开发服务器。最后,我将使用 nginx。但是我的php服务器有问题。

root
|
|---/app
    |
    |---/index.html
    |---/scripts
    |   |
    |   |---/main.css
    |
    |---/styles
        |
        |---/main.js

在我的app/index.html我有:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="styles/main.css" type="text/css" />
</head>
<body>

  <script src="scripts/main.js"></script>
</body>
</html>

当我在浏览器中打开页面时,localhost:8000/app找不到 css 和 javascript。浏览器正在寻找localhost:8000/styles相应的。localhost:8000/scripts而不是localhost:8000/app/styles相应的。localhost:8000/app/scripts. 当我在没有服务器或使用 nginx 的情况下直接打开文件时,可以正确找到文件。所以php服务器改变了一些东西。这里发生了什么?

我在 Chrome 和 Firefox 中尝试过。两种浏览器中的行为相同。localhost/app当我用( )打开网站时,nginx一切都按预期工作。当我用localhost:8000/app( php -S localhost:8000) 打开网站时,找不到脚本和样式。它是具有相同文件根目录的相同文件。不同的行为从何而来?

4

2 回答 2

2

在检查了关于PHP: Built-in web server的官方文档后,它清楚地指出:

URI 请求是从 PHP 启动的当前工作目录提供的,除非 -t 选项用于指定显式文档根目录。

于 2016-07-03T15:26:23.100 回答
0

这是 PHP 内置 Web 服务器的一个令人讨厌的行为。幸运的是,有一个解决方法:只需启动您的应用程序,包括index.html在 URI 中。打开浏览器并转到

http://localhost:8000/app/index.html
于 2021-05-08T20:04:49.527 回答