Oleg Shevtsov
Oleg Shevtsov

Reputation: 73

Configure nginx with style apache2 userdir

I need just configure my nginx server with style apache2 user_dir

I have my config file this sections:

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

  location ~ ^/~(.+?)(/.*)?$ {
             alias /home/$1/public_html$2;
            index  index.php index.html index.htm;
            autoindex on;
        }

This partially works, I see directories, index.html, but index.php does not work.

Which configuration should be correct? Thank you.

Upvotes: 1

Views: 1131

Answers (1)

Richard Smith
Richard Smith

Reputation: 49752

To run PHP from a different root (i.e. /home/$1/public_html$2) you will need to use a nested location block.

For example:

location ~ ^/~(.+?)(/.*)?$ {
    alias /home/$1/public_html$2;
    index  index.php index.html index.htm;
    autoindex on;

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        include        ...;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        fastcgi_pass   unix:/run/php/php7.0-fpm.sock;
    }
}

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

Notice that $request_filename is used as the path to the aliased file. The order of the locations will need to be reversed (as illustrated above), so that the correct location processed the URIs which end with .php. See this document for more.

You should add the appropriate include statement. The snippets/fastcgi-php.conf file may be suitable, otherwise use include fastcgi_params;.

Upvotes: 2

Related Questions