简体   繁体   English

Nginx + PHP-FPM:在别名位置提供 php 个文件

[英]Nginx + PHP-FPM: serving php files in location with alias

I just started to work with nginx, before I always used apache2 when hosting any kind of website or application and it was pretty easy to deploy it.我刚开始使用 nginx,之前我在托管任何类型的网站或应用程序时总是使用 apache2,部署它非常容易。 My goal was to deploy nextcloud, but not as maybe usual in the root of a server.我的目标是部署 nextcloud,但不像往常一样部署在服务器的根目录中。 As I just rented a VPS and got a generic subdomain without any control of subdomains or DNS entries in general.因为我刚刚租了一个 VPS 并获得了一个通用子域,而没有任何子域控制或一般的 DNS 条目。

I didn't modify my nginx.conf, but added a new file with a server directive like the following:我没有修改我的 nginx.conf,而是添加了一个带有服务器指令的新文件,如下所示:

server {
        listen 80;
        listen [::]:80;

        server_name example.com;
        root /srv/www/html;

        include snippets/nextcloud-phpfpm.conf;

        location /cloud {
                alias /srv/www/nextcloud;

                include snippets/nextcloud-phpfpm.conf;
                #location ~* (.+\.php)$ {
                        #fastcgi_pass unix:/var/run/php8.1-fpm-nextcloud.sock;
                        ...
                #}
        }

        #try_files $uri /index.html;
}

The included nextcloud-phpfpm.conf:包含的 nextcloud-phpfpm.conf:

location ~* \.php/*$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php8.1-fpm-homecloud.sock;
        #include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
}

The problem is, that php-fpm just responded with "File not found", without any further hints or information, so debugging was pretty hard.问题是,php-fpm 只是响应“找不到文件”,没有任何进一步的提示或信息,因此调试非常困难。

Of course, I used google, stackoverflow in the first place to find a solution to my problem.当然,我首先使用google,stackoverflow来找到解决我问题的方法。 The most often advised solution was to correctly set the SCRIPT_FILENAME parameter for fastcgi with something like $document_root$fastcgi_script_name .最常被建议的解决方案是为 fastcgi 正确设置 SCRIPT_FILENAME 参数,例如$document_root$fastcgi_script_name After some time a found out, that is - at least in my case - crap and doesn't work, in fact it just produces a much more faulty file path than before.一段时间后发现,至少在我的情况下是废话并且不起作用,事实上它只是产生比以前更错误的文件路径。

It was quite a tough way to get to a solution that I found on my own, and I want to share it for everyone.这是我自己找到的解决方案的艰难途径,我想与大家分享。

Nearly all solutions that I found suggested something like我发现的几乎所有解决方案都建议类似

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

to properly set the PHP parameter so that PHP-FPM can properly find the file.正确设置 PHP 参数,以便 PHP-FPM 可以正确找到该文件。 But that didn't just work.但这不仅奏效。

After some depressing time of debugging and trying things out, I found out that - although $document_root provided the correct path of /srv/www/nextcloud - $fastcgi_script_name did not provide what I expected.经过一段令人沮丧的调试和尝试之后,我发现 - 尽管$document_root提供了/srv/www/nextcloud的正确路径 - $fastcgi_script_name 没有提供我所期望的。 I expected, that it is info.php when I call it with <url>/cloud/info.php , but in fact it return everything after the actual domain like cloud/info.php .我预计,当我用<url>/cloud/info.php调用它时它是info.php ,但实际上它返回实际域之后的所有内容,如cloud/info.php That, in the end, leads again to an incorrect SCRIPT_FILENAME ;这最终会再次导致不正确的SCRIPT_FILENAME

There are now two solutions for this special problem这个特殊问题现在有两种解决方案

Solution 1解决方案 1

I found out, that the $fastcgi_script_name can be modified based on the URI path.我发现,可以根据 URI 路径修改$fastcgi_script_name As mentioned in the nginx docs , the fastcgi_split_path_info directive can be used to specify a regex with two capture groups, the first is then stored in $fastcgi_script_name and the second in $fastcgi_path_info .nginx 文档中所述, fastcgi_split_path_info指令可用于指定具有两个捕获组的正则表达式,然后第一个存储在$fastcgi_script_name中,第二个存储在 $ $fastcgi_path_info中。

The approach is then to change the first capturing group in such way, that the /cloud is not included anymore.然后,方法是以这样的方式更改第一个捕获组,即不再包含/cloud

before (as suggested in Nextcloud docs for nginx):之前(如 Nginx 的 Nextcloud 文档中所建议):

fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;

after:后:

fastcgi_split_path_info ^/cloud(/.+?\.php)(\/.*|)$;

I expect this to work also for most cases, as only a part at the beginning is removed the the remaining path is kept.我希望这也适用于大多数情况,因为只删除了开头的一部分,而保留了剩余的路径。

Solution 2方案二

I also found another variable of nginx which may be suitable in some cases, but is expected to fail with something like index.php/apps etc.我还发现另一个变量 nginx 在某些情况下可能适用,但预计会因index.php/apps等而失败。

fastcgi_param SCRIPT_FILENAME $request_filename

This parameter contains the full real path to the script on the filesystem - at least in my case - and may also be used in combination with regex the make some modifications.此参数包含文件系统上脚本的完整真实路径 - 至少在我的情况下 - 并且也可以与正则表达式结合使用以进行一些修改。

Further thoughts进一步的想法

Nevertheless it my first-time experience with nginx is quite negative.尽管如此,我对 nginx 的第一次体验是相当消极的。 I wanted to get into nginx without any knowledge, and in my opinion the configuration syntax is much easier than apache2. But the devil seems to be in the details, making it quite a nightmare to the configure some easy stuff and, in particular, debug issues.我想在一无所知的情况下进入 nginx,在我看来,配置语法比 apache2 容易得多。但问题似乎在于细节,这使得配置一些简单的东西,尤其是调试变得非常噩梦问题。 Debugging output that can be activated with some configuration options in nginx and php-fpm just do not produce some useful output the debug such an issue, and when trying nginx for the first time, recompiling nginx (as suggested somewhere) just takes my motivation.调试 output 可以通过 nginx 和 php-fpm 中的一些配置选项激活,但不会产生一些有用的 output 调试这样的问题,当第一次尝试 nginx 时,重新编译 nginx(如某处建议)只是激发了我的动力。

Are there any comments on this?对此有何评论? Maybe other solutions or hints for further usage?也许其他解决方案或进一步使用的提示? At the moment, I just tried a basic setup like described in the question and may not cover any other quirks.目前,我只是尝试了问题中描述的基本设置,可能不会涵盖任何其他怪癖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM