如何使用Nginx别名从其他目录加载PHP?

时间:2014-11-24 11:45:43

标签: nginx

我想在网络服务器上创建一个子目录system,为人们提供另一个文件夹的后端,但是我遇到了一些困难。

服务器配置应将system翻译为index.php的{​​{1}},基本上/srv/www/xxx/backend/web应该是另一个目录索引的别名。

我的配置如下:

system

我尝试过多次迭代(包括使用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; } ),但即使我能得到:

root

给我:

return 200 $document_root$fastcgi_script_name;

我已经对这个文件进行了vi以确保它在我取出返回时能够正常工作给我一个404.我相信我错过了一些非常简单的东西。

有人可以帮我理解错误吗?

2 个答案:

答案 0 :(得分:3)

由于位置php是嵌套的,/index.php URI在此处未解析,但在配置的最后一个块中。由于nginx别名中的long standing bug不能与try_files一起使用,因此您需要使用root/rewrite couple instead。所以你可以用以下方法解决这个问题:

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

答案 1 :(得分:1)

要完成接受的答案,我添加了一些部分以使静态文件正常工作:

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

最后一个位置解决了静态文件问题,即文件无法从此处加载。