如何从根目录和用户目录提供 PHP 服务?

如何从根目录和用户目录提供 PHP 服务?

我尝试了多种组合,但肯定是漏掉了什么。我得到的最接近的结果是使用以下配置文件,我在 /var/www/html 和 /home/user/www 中获取静态内容,但 php 仅在服务器根目录中工作,我在 userdir 上获得 404。

server {
        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;
        include snippets/self-signed.conf;
        include snippets/ssl-params.conf;

        root /var/www/html;

        index index.html index.htm index.php;

        server_name 172.24.50.59;

        location / {
                try_files $uri $uri/ =404;
                autoindex on;
        }

        location ~ ^/~(.+?)(/.+\.php)$ {
                alias /home/$1/www$2;

                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

        }

        location ~ ^/~(.+?)(/.*)?$ {
                alias /home/$1/www$2;
                autoindex on;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }
        location ~ /\. {
                deny all;
        }       
}

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

        server_name 172.24.50.59;

        return 301 https://$server_name$request_uri;
}

任何帮助均感激不尽。

编辑:我研究了一下,但仍然无法让它工作。fastcgi-php.conf说:

fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name =404;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;

无论fastcgi.conf我是否设置SCRIPT_FILENAME$document_root$fastcgi_script_name,或者$request_filename在使用 php 文件时仍然出现 404 错误/home/user/www

/etc/nginx/fastcgi.conf:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

答案1

使用 时alias, 的值$document_root$fastcgi_script_name不指向请求的文件。您可以使用$request_filename,这将适用于root和两种alias情况。

此外,try_files使用该语句$fastcgi_script_name将始终失败并强制出现 404 响应。

try_files $fastcgi_script_name =404andfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name语句替换为:

if (!-f $request_filename) { return 404; }
fastcgi_param SCRIPT_FILENAME $request_filename;

以上适用于和root情况alias

相关内容