UserDir

Sven Ludwig nginx at abraumhal.de
Thu Feb 16 23:37:20 UTC 2012


On 02-16 16:57, white_gecko wrote:
> I have a very similar problem. I want to use userdirs as in apache and
> as described here: http://wiki.nginx.org/UserDir and use php with
> fast-cgi in these userdirs. I've tried it with this:
> 
>         location ~ ^/~(.+?)(/.*)?$ {
>             alias /home/$1/public_html$2;
>             index  index.html index.htm;
>             autoindex on;
>             location ~ \.php$ {
>                 fastcgi_pass 127.0.0.1:9000;
>                 fastcgi_index index.php;
>                 include fastcgi_params;
>             }
>         }
> 
> But it get empty pages, when I try to call a php-file.
> How can I get php files with match this location to be forwarded to
> fast-cgi?

If you take a look into the fastcgi_params file you'll notice that the
following or something similar is written:

fastcgi_param   SCRIPT_FILENAME         $request_filename;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;

$request_filename in your case is something like this:

/~white_geko/some/where/is/my/script.php

$fastcgi_script_name:

/~white_geko/some/where/is/my/script.php

so, this is not the correct path to find this file on the filesystem, so we
map it:

^/~([^/]+)(/.*)\.php

$1 is the username

$2 is the filename + relative path

so, we set the document_root to your public_html directory, which is somehow
correct if your php script is placed in your public_html directory.

root /home/$1/public_html;

next we set the correct script_name which is relative to the document_root. We
add an .php, because it is not included in our match($2)

fastcgi_param   SCRIPT_NAME             $2.php;

After this all php-fpm needs to know is where is the file located.

fastcgi_param   SCRIPT_FILENAME         $document_root$2.php;

when i sniffed the traffic between nginx <-> php-fpm i noticed that
fastcgi_param only adds more parameters to the communication. it does not
replace the old values. so i dropped the include of the params out and added
all variables right in this location.

The complete config section, try this:

    location ~ ^/~([^/]+)(/.*)\.php$ {
        root /home/$1/public_html;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param   SCRIPT_NAME             $2.php;
        fastcgi_param   SCRIPT_FILENAME         $document_root$2.php;
        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   REQUEST_URI             $request_uri;
        fastcgi_param   DOCUMENT_URI            $document_uri;
        fastcgi_param   DOCUMENT_ROOT           $document_root;
        fastcgi_param   SERVER_PROTOCOL         $server_protocol;

        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;
    }

    location ~ ^/~([^/]+)(/.*)?$ {
        if ( !-d /home/$1/public_html ) {
            return 404;
        }
        index  index.html index.htm index.php;
        autoindex on;
    }

Hope that works for you :)

bye MUH!

-- 
;__;___,
)..(_=_)
(oo)| |



More information about the nginx mailing list