Sammaye
Sammaye

Reputation: 43884

How to use Nginx alias to load PHP from other directory?

I want to make a sub directory my on web server, system that serves a backend from another folder for people, but I am having some difficulty.

The server configuration should translate system as index.php of the /srv/www/xxx/backend/web, essentially system should alias to the index of another directory.

I have a configuration like:

location /system {
    alias /srv/www/xxx/backend/web;
    rewrite ^(.*) /index.php?r=$1;
    return 200 $document_root$fastcgi_script_name;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        try_files $uri =404;

        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

location / {
    rewrite /(.*) /index.php?r=$1;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    try_files $uri =404;

    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

I have tried numerous iterations (including using root), however even though I can get:

return 200 $document_root$fastcgi_script_name;

to give me:

/srv/www/xxx/backend/web/index.php

and I have vi'ed into this file to make sure it works when I take out the return wget gives me a 404. I am sure I am missing something really simple.

Can someone help me understand what is wrong?

Upvotes: 1

Views: 3511

Answers (2)

Sammaye
Sammaye

Reputation: 43884

To complete the accepted answer, I add some parts to get static files working:

location ~ ^/system(.*) {
    root /srv/www/xxx/backend/web;
    rewrite ^/system/(.*)$ /$1 break;
    try_files $uri /system/index.php?r=$1&$args;

    location ~ \.php$ {
        rewrite ^/system/(.*)$ /$1 break;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        try_files $uri =404;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ (.*\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|mp4|ogg|woff|ttf))$ {
        rewrite ^/system/(.*)$ /$1 break;
        try_files $uri =404;
    }
}

The last location solves the static files problem whereby files would not load from this place.

Upvotes: 1

Xavier Lucas
Xavier Lucas

Reputation: 2642

As location php is nested the /index.php URI is not resolved here but in the last block of your configuration. Due to a long standing bug in nginx alias doesn't work with try_files so you need to use the root/rewrite couple instead. So you can fix this with :

location /system {

    root /srv/www/xxx/backend/web;
    rewrite ^/system/(.*)$ /$1 break;
    try_files $uri /system/index.php?r=$uri;

    location ~ \.php$ {
        rewrite ^/system/(.*)$ /$1 break;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        try_files $uri =404;

        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

location / {
    rewrite /(.*) /index.php?r=$1;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    try_files $uri =404;

    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Upvotes: 3

Related Questions