You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查看NGINX的$document_root值及解决虚拟主机指向错误根目录问题

查询Nginx $document_root 取值的方法
  • 方法1:通过自定义Nginx返回直接查看
    临时在lottaleben.conf的server块中添加如下测试配置:

    location = /test-root {
        default_type text/plain;
        return 200 "document_root: $document_root\n匹配server_name: $server_name\n请求Host头: $host";
    }
    

    执行nginx -s reload重载配置后,访问http://lottaleben.test/test-root即可直接看到对应变量的取值。

  • 方法2:通过自定义日志持久化记录
    在nginx.conf的http块中新增自定义日志格式:

    log_format debug_root '$time_local $host $server_name $document_root $request_uri $status';
    access_log logs/debug_root.log debug_root;
    

    重载配置后发起访问,打开Nginx日志目录下的debug_root.log即可查看所有请求对应的$document_root取值。

  • 方法3:通过PHP输出验证
    在站点目录下新建test.php文件,写入以下内容:

    <?php
    echo "SCRIPT_FILENAME: " . $_SERVER['SCRIPT_FILENAME'];
    ?>
    

    结合你已配置的SCRIPT_FILENAME $document_root$fastcgi_script_name规则,输出的路径前缀就是当前的$document_root取值。

站点匹配异常的排查方案

你当前访问lottaleben.test落到localhost虚拟主机的问题,按以下顺序排查即可:

  1. 先执行nginx -T(注意是大写T),该命令会打印所有已加载的配置文件全文,检查输出中是否包含lottaleben.conf的内容,若没有则说明你include sites/*.conf的相对路径配置错误,需确认sites目录的位置是否和Nginx运行目录匹配。
  2. 确认配置修改后已重载生效,执行nginx -s reload即可,若配置有语法错误会直接抛出提示,修正后再重载。
  3. 验证Host解析是否生效,在cmd中执行ping lottaleben.test,确认返回的IP是127.0.0.1,若不对可执行ipconfig /flushdns清空本地DNS缓存后重试。
  4. 若上述步骤都正常,可通过curl命令验证请求:curl -v http://lottaleben.test,查看请求头中的Host字段是否为lottaleben.test,确保请求头符合匹配规则。

内容的提问来源于stack exchange,提问作者adahox_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 01:54:07