代码之家  ›  专栏  ›  技术社区  ›  CodeLikeBeaker

使用相同根目录的nginx虚拟目录出现404错误

  •  2
  • CodeLikeBeaker  · 技术社区  · 6 年前

    我对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
    

    如能为正确的方向提供帮助,我们将不胜感激。

    注意:使用 alias VS root

    通过Alias,我可以得到以下信息:

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

    可能的解决方案

    我不确定这是否是正确的方法,但它起作用了。

    如果我在主文件夹中为虚拟目录创建了一个符号链接,那么站点将被加载。我宁愿在nginx做这个,但是如果我必须走这条路,我会的。思想?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Richard Smith    6 年前

    这个 root 指令通过其值与当前URI的简单连接来构造文件的路径。所以你的第二个定位块在 /var/www/php/site/sitea/index.php .

    这个 alias 前缀位置中的指令将用 别名 价值。见 this document 更多。

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

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

    两者都是 别名 指令设置一个名为 $request_filename 到文件的路径。

    在php块中,使用 $document_root$fastcgi_script_name 通知php-fpm文件的路径。这与 但不能 别名 .

    fastcgi_param SCRIPT_FILENAME $request_filename;
    

    以上两种方法都适用 别名 对于不处理路径信息的PHP块(如您的)。