【问题标题】:Nginx Virtual Directory using the same root gives 404 error使用相同根目录的 Nginx 虚拟目录会出现 404 错误
【发布时间】:2018-11-15 20:33:03
【问题描述】:

我是 Nginx 的新手,我正在尝试从 Apache 导入旧站点。我想摆脱虚拟目录,但不幸的是我做不到。

虚拟目录指向主站点的同一个根。代码中有检测虚拟目录并根据该信息加载数据的逻辑,这就是它存在的原因。

这是我试图开始工作的配置:

server {
    listen 80;

    large_client_header_buffers 4 32k;

    server_name site.domain.com;

    access_log /var/log/sites/site.access.log;
    error_log /var/log/sites/site.error.log error;


    location / {
        root /var/www/php/site;
        index index.php;
        include /etc/nginx/conf.d/php.inc;
    }

    location /sitea {
        root /var/www/php/site;
        index index.php;
        include /etc/nginx/conf.d/php.inc;
    }

    location ~ /\.ht {
        deny all;
    }
}

php.inc 的内容:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

我已经尝试了谷歌提供的所有方法来让它工作,但无论我做什么,我都会继续收到以下错误:

2018/11/15 20:28:32 [debug] 5056#5056: *1 http script var: "/sitea/index.php"
2018/11/15 20:28:32 [debug] 5056#5056: *1 trying to use file: "/sitea/index.php" "/var/www/php/site/sitea/index.php"
2018/11/15 20:28:32 [debug] 5056#5056: *1 trying to use file: "=404" "/var/www/php/site=404"
2018/11/15 20:28:32 [debug] 5056#5056: *1 http finalize request: 404, "/sitea/index.php?requestType=home" a:1, c:1
2018/11/15 20:28:32 [debug] 5056#5056: *1 http special response: 404, "/sitea/index.php?requestType=home"
2018/11/15 20:28:32 [debug] 5056#5056: *1 http set discard body
2018/11/15 20:28:32 [debug] 5056#5056: *1 xslt filter header
2018/11/15 20:28:32 [debug] 5056#5056: *1 HTTP/1.1 404 Not Found

对正确方向的任何帮助将不胜感激。

注意:使用 aliasroot 也可以做同样的事情

使用别名我得到以下信息:

2018/11/15 20:37:38 [错误] 5189#5189: *1 FastCGI 在标准错误中发送: 从上游读取响应标头时出现“主脚本未知”, 客户端:#.#.#.#,服务器:site.domain.com,请求:“GET /sitea/index.php?requestType=home HTTP/1.1",上游: “fastcgi://unix:/run/php/php7.0-fpm.sock:”,主机:“site.domain.com”

可能的解决方案

我不确定这是否是正确的做法,但它确实有效。

如果我为站点加载的主文件夹中的虚拟目录创建了符号链接。我宁愿在 Nginx 中这样做,但如果我必须走这条路,我会的。想法?

【问题讨论】:

  • URI为/sitea/index.php的文件在什么位置?
  • /var/www/php/site 谢谢

标签: nginx


【解决方案1】:

root 指令通过其值与当前 URI 的简单连接来构造文件的路径。因此,您的第二个位置块正在寻找位于 /var/www/php/site/sitea/index.php 的文件。

前缀位置中的alias 指令将用alias 值替换前缀文本。请参阅this document 了解更多信息。

location /sitea {
    alias /var/www/php/site;
    ...
}

所以上面的位置块将在/var/www/php/site/index.php 处寻找URI /sitea/index.php

rootalias 指令都将一个名为 $request_filename 的变量设置为文件路径。

在您的 PHP 块中,您使用 $document_root$fastcgi_script_name 通知 PHP-FPM 文件的路径。这适用于root,但不适用于alias

fastcgi_param SCRIPT_FILENAME $request_filename;

对于不处理路径信息(例如您的)的 PHP 块,上述内容适用于 rootalias

【讨论】:

  • 感谢您的详细解释。使用aliasfastcgi_param SCRIPT_FILENAME $request_filename 它克服了那个错误。该站点没有加载,但这可能是代码中的其他内容。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 2020-12-27
  • 2013-11-12
  • 1970-01-01
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多