nginx配置/文件夹运行路径与/

nginx配置/文件夹运行路径与/,nginx,subdirectory,Nginx,Subdirectory,我在主网站以外的其他路径上运行/文件夹时遇到问题 该部分的我的nginx.conf如下所示: location ~ \.php$ { try_files $uri = 404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param SCRIPT_FILENAME $request_filename;

我在主网站以外的其他路径上运行/文件夹时遇到问题

该部分的我的nginx.conf如下所示:

        location ~ \.php$ {
            try_files               $uri = 404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param       SCRIPT_FILENAME $request_filename;
            fastcgi_pass            unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index           index.php;
           include                 fastcgi_params;
            include                 fastcgi.conf;
    }


location ~ /folder {
        alias /srv/http/folder;
    try_files $uri $uri/ @folder;

    location ~ \.php$ {
                fastcgi_param SCRIPT_FILENAME $request_filename;
                fastcgi_pass   unix:/var/run/php/php7.2-fpm.sock;
                include        fastcgi.conf;
        }
}

location @folder {
    rewrite /folder/(.*)$ /folder/index.php?/$1 last;
}
在error.log中,我可以看到以下内容:

2020/06/03 09:05:26 [error] 25966#25966: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.21.2.46, server: example.com, request: "GET /folder/xxx_v6.15.11/Resources/images/redcaplogo.gif HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "example.com"

有没有关于如何解决此问题的建议?

正则表达式位置从头到尾都是匹配的,并且第一个找到的匹配由nginx处理。当您收到对
/folder/script.php
的请求时,它将由第一个位置块处理。尝试交换这两个位置。另外,为什么不在第二个位置块中包含
fastcgi_参数

更新

我做了一些调试,让我们看看您的代码(假设位置块已经交换):

如果我们得到一个请求
/folder/some/path
(并且
文件夹中没有
some/path
文件或目录),在嵌套位置内,内部nginx变量将具有以下值:

  • $request\u filename
    :空字符串(th
    文件夹
    目录中没有真正的文件或文件夹
    /some/path
  • $uri
    /folder/check.php
  • $document\u root
    /srv/http/folder
如果我们得到一个请求
/folder/some/path/script.php
(并且有一个同名的真实php脚本),在嵌套位置内,内部nginx变量将具有以下值:

  • $request_filename
    /srv/http/folder/some/path/script.php
  • $uri
    /folder/some/path/check.php
  • $document\u root
    /srv/http/folder
此外,当您从文件夹中收到对静态资源的请求时,例如
/folder/some/path/static.css
try\u files$uri…
指令将在
/srv/http/folder
目录中搜索
文件夹/some/path/static.css
文件,这将导致检查是否存在
/srv/http/folder/folder/some/path/static.css
文件

获取
文件夹
目录内文件子路径的可能解决方案之一:

location ~ ^/folder(?<subpath>.*) {
    alias /srv/http/folder;
    try_files $subpath $subpath/ @folder;

    location ~ \.php$ {
        fastcgi_param  SCRIPT_FILENAME $document_root$subpath;
        fastcgi_pass   unix:/var/run/php/php7.2-fpm.sock;
        include        fastcgi_params;
        include        fastcgi.conf;
    }
}

location ~ \.php$ {
    try_files               $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param           SCRIPT_FILENAME $request_filename;
    fastcgi_pass            unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_index           index.php;
    include                 fastcgi_params;
    include                 fastcgi.conf;
}

location @folder {
    rewrite ^/folder/(.*)$ /folder/index.php?/$1 last;
}
作为nginx文档:

当位置与指令值的最后一部分匹配时:

location /images/ {
  alias /data/w3/images/;
}
最好改用root指令:

location /images/ {
  root /data/w3;
}

我已经切换了位置顺序,并将factsgi_param添加到location~/文件夹中,但错误是相同的。@zo6015真是一个有趣的错误!找到了问题的原因,请查看更新的答案。由于错误已消除,情况会更好,但css、js和图像等静态对象仍未加载。@Zo6015更新了答案,请检查。我无法为静态对象使用位置,因为我有多个/文件夹类型的目标位置,每个位置都有不同的静态对象路径。如果我把它放在location~/文件夹中,nginx会抱怨
nginx:[emerg]location”/images/“在location/文件夹之外”
location /images/ {
  alias /data/w3/images/;
}
location /images/ {
  root /data/w3;
}