简体   繁体   中英

nginx with a separate root per subdirectory

I'm trying to setup nginx so each path has it's own root directory. This is working for the most part, however POST to php-fpm a throwing a 405.

Currently trying:

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

Alias does not append location path to the file path. Check the logical flow. If server root is at /var/www/public , foo is located at /var/www/foo/public , bar is located at /var/www/bar/public . Then, this will be the easy config :

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

If you want to use PHP in one directive then :

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

Usage of alias -- http://nginx.org/en/docs/http/ngx_http_core_module.html#alias Nginx modifier -- http://nginx.org/en/docs/http/ngx_http_core_module.html#location

If you need more number of path, then you have to symlink.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM