【问题标题】:Is it possible to have web index outside public html?是否可以在公共 html 之外拥有网络索引?
【发布时间】:2021-12-14 18:39:06
【问题描述】:

假设一个网站有这样的文件夹结构

/index.php
/<public>
   dikpic.jpeg

当有人访问该网站时,我希望物理网络根指向/public, 喜欢mywebsite.com/dikpic.jpeg

(没有 URL 重写)。

这可以通过root /myuser/public; 命令实现。

但我也想从这个目录外加载 index.php 文件:

index /myuser/index.php;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~* \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

问题在于位置尝试文件块假定/ 是网络根目录:(

有什么办法吗?


我还应该指出,index.php 是网站上唯一的脚本。 所以我真的不在乎是否忽略了任何其他对 .php 文件的请求。 index.php 处理 url 重写的东西...

【问题讨论】:

  • 不是真的,对不起...
  • 为什么你的 index.php 需要在你的公共目录之外?你的公共目录是你的 web 根目录,如果你的 index.php 是 index.html 的 php 版本,那么公共目录是唯一明显的地方。
  • 因为服务器托管多个具有相同索引的网站,我想有一个文件,而不是复制到每个网站

标签: php http nginx


【解决方案1】:

您可以为此使用额外的 location 块,尽管这对我来说似乎不是一个优雅的解决方案:

location ~* \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

location = /index.php {
    fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    /full/path/to/your/index.php;
    fastcgi_param   SCRIPT_NAME        /index.php;
}

精确匹配位置优先于正则表达式匹配位置,因此第一个块将用于除/index.php 之外的任何 PHP 文件请求。您甚至不需要为第二个位置定义根,设置正确的SCRIPT_FILENAMEFastCGI 参数值就足够了。

更新

我没有注意到你问题的最后一句话,但如果你不关心任何其他 PHP 文件,那么只有第二个块 location = /index.php { ... } 就足够了。

【讨论】:

    【解决方案2】:

    您可以创建 index.php here is how to do that. 的符号链接

    因此,您的所有网站都会有一个 index.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多