nginx,每个子目录有一个单独的根

时间:2016-09-14 17:57:12

标签: php nginx webserver fastcgi

我尝试设置nginx,因此每条路径都有自己的根目录。这在大多数情况下是有效的,但是对于php-fpm来说是一个投掷405的。

目前正在尝试:

    location ^~ /foo {
        alias /www/foo;
        #index  index.php;
        try_files $uri /www/foo/index.php$request_uri;
        access_log /var/log/nginx/foo.log main;
        error_log /var/log/nginx/foo.log error;
   }

    location ^~ /bar {
        alias /www/bar;
        #index  index.php;
        try_files $uri /www2/bar/index.php$request_uri;
        access_log /var/log/nginx/bar.log main;
        error_log /var/log/nginx/bar.log error;
   }

    location ~ \.php {
        set $php_root /usr/local/deploy/baz/current/web;
        if ($request_uri ~* /foo ) {
            set $php_root /www/foo/current/web;
        }
        if ($request_uri ~* /bar ) {
            set $php_root /www2/bar/current/web;
        }

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
        fastcgi_param  DOCUMENT_ROOT    $php_root;
        include        fastcgi_params;
} 

1 个答案:

答案 0 :(得分:0)

Alias不会将位置路径附加到文件路径。检查逻辑流程。如果服务器根位于/var/www/publicfoo位于/var/www/foo/public,则bar位于/var/www/bar/public。然后,这将是简单的配置:

server {
root /var/www/public;
...
location /foo {
     root /var/www/foo/public;
     }
location ~ /foo/.+\.php$ {
    fastcgi_param  SCRIPT_FILENAME /var/www/foo/public$fastcgi_script_name;
    # rest of fastcgi
}

location /bar {
    root /var/www/bar/public;
}

location ~ /bar/.+\.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param      SCRIPT_FILENAME/var/www/bar/public$fastcgi_script_name;
    # rest of fastcgi
}
} # ends server

如果你想在一个指令中使用PHP,那么:

server {
...
root /var/www/public;
...
location /foo {
     root /var/www/foo/public;
     }
location /bar {
     root /var/www/bar/public;
     }

location ~ \.php$ {
    set $php_root /var/www/public;
    if ($request_uri ~* /foo) {
        set $php_root /var/www/foo/public;
    }
    if ($request_uri ~* /bar) {
        set $php_root /var/www/bar/public;
    }
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
        }
...
 } # server block ends

alias的使用 - http://nginx.org/en/docs/http/ngx_http_core_module.html#alias Nginx修饰符 - http://nginx.org/en/docs/http/ngx_http_core_module.html#location

如果您需要更多路径,则必须进行符号链接。