# UserDir

**URL:** <https://www.ruby-forum.com/t/userdir/216325>\
**Category:** NGINX\
**Created:** [February 1, 2012, 5:32pm UTC](https://www.ruby-forum.com/t/userdir/216325 "2012-02-01T17:32:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 1, 2012, 5:32pm UTC](https://www.ruby-forum.com/t/userdir/216325/1 "2012-02-01T17:32:03Z")

</div>

I’m having trouble getting UserDir to work. I think it has something to  
do  
with the PHP fastcgi options. Can someone please take a look at my  
configuration and double check what I have? Any help is appreciated.  
Thanks.

> **[user nobody;worker\_processes 4;worker\_rlimit\_nofile 8192;events{ - Pastebin.com](https://pastebin.com/3wRTVXJW)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

---

<div class="post-metadata">

**Author:** ![Brad\_Huntington](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brad\_Huntington](https://www.ruby-forum.com/u/Brad_Huntington)\
**Post date:** [February 1, 2012, 6:31pm UTC](https://www.ruby-forum.com/t/userdir/216325/2 "2012-02-01T18:31:30Z")

</div>

On Wed, Feb 01, 2012 at 11:31:14AM -0500, Brad Huntington wrote:

Hi there,

> I’m having trouble getting UserDir to work. I think it has something to do  
> with the PHP fastcgi options. Can someone please take a look at my  
> configuration and double check what I have? Any help is appreciated. Thanks.

What’s the trouble you’re seeing?

GET /~user/file fails? Or GET /file.php fails? Or GET /~user/file.php  
fails?

> [user nobody;worker\_processes 4;worker\_rlimit\_nofile 8192;events{ - Pastebin.com](http://pastebin.com/3wRTVXJW)

In nginx, each request is handled by exactly one location{} block. Only  
the configuration in, or inherited into, that location{} matters.

You have one regex location for .php urls, and one regex location for  
“userdir” urls. A single request won’t match both of those.

If you can describe your desired url-\>file mapping, it will likely be  
easier to ensure that the nginx.conf matches that.

Good luck,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Brad\_Huntington](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brad\_Huntington](https://www.ruby-forum.com/u/Brad_Huntington)\
**Post date:** [February 16, 2012, 10:58pm UTC](https://www.ruby-forum.com/t/userdir/216325/3 "2012-02-16T22:58:16Z")

</div>

Hello,  
I have a very similar problem. I want to use userdirs as in apache and  
as described here: [http://wiki.nginx.org/UserDir](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?

Posted at Nginx Forum:

> **[Re: UserDir](https://forum.nginx.org/read.php?2,221861,222506#msg-222506)**
>
> I'm having trouble getting UserDir to work. I think it has something to do with the PHP fastcgi options. Can someone please take a look at my configuration and double check what I have? Any help is appreciated. Thanks. http://pastebin.com/3wRTVXJW...

---

<div class="post-metadata">

**Author:** ![Brad\_Huntington](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brad\_Huntington](https://www.ruby-forum.com/u/Brad_Huntington)\
**Post date:** [February 1, 2012, 6:27pm UTC](https://www.ruby-forum.com/t/userdir/216325/4 "2012-02-01T18:27:47Z")

</div>

On Wed, Feb 1, 2012 at 11:31 PM, Brad Huntington  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m having trouble getting UserDir to work. I think it has something to do  
> with the PHP fastcgi options. Can someone please take a look at my  
> configuration and double check what I have? Any help is appreciated. Thanks.
> 
> [user nobody;worker\_processes 4;worker\_rlimit\_nofile 8192;events{ - Pastebin.com](http://pastebin.com/3wRTVXJW)

> <https://pastebin.com/6KHDwhvx>

note that using this config, anyone can read (and possibly write)  
others’ files with little effort thanks to php process running as  
single user.

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![Brad\_Huntington](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brad\_Huntington](https://www.ruby-forum.com/u/Brad_Huntington)\
**Post date:** [February 17, 2012, 10:40am UTC](https://www.ruby-forum.com/t/userdir/216325/5 "2012-02-17T10:40:21Z")

</div>

Thank you,  
even though I understand it only partially it works.  
But directory listing doesn’t work anymore (e.g.  
[http://localhost/~white\_gecko/](http://localhost/~white_gecko/)) I get 404.

Posted at Nginx Forum:

> **[Re: UserDir](https://forum.nginx.org/read.php?2,221861,222538#msg-222538)**
>
> I'm having trouble getting UserDir to work. I think it has something to do with the PHP fastcgi options. Can someone please take a look at my configuration and double check what I have? Any help is appreciated. Thanks. http://pastebin.com/3wRTVXJW...

---

<div class="post-metadata">

**Author:** ![Brad\_Huntington](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brad\_Huntington](https://www.ruby-forum.com/u/Brad_Huntington)\
**Post date:** [June 29, 2012, 4:24am UTC](https://www.ruby-forum.com/t/userdir/216325/6 "2012-06-29T04:24:41Z")

</div>

Try this → [http://pastie.org/4169064](http://pastie.org/4169064)

Posted at Nginx Forum:

> **[Re: UserDir](https://forum.nginx.org/read.php?2,221861,228080#msg-228080)**
>
> I'm having trouble getting UserDir to work. I think it has something to do with the PHP fastcgi options. Can someone please take a look at my configuration and double check what I have? Any help is appreciated. Thanks. http://pastebin.com/3wRTVXJW...

---

<div class="post-metadata">

**Author:** ![Brad\_Huntington](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brad\_Huntington](https://www.ruby-forum.com/u/Brad_Huntington)\
**Post date:** [February 17, 2012, 12:37am UTC](https://www.ruby-forum.com/t/userdir/216325/7 "2012-02-17T00:37:58Z")

</div>

On 02-16 16:57, white\_gecko wrote:

> ```
> 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)| |
