2017-08-14 56 views
0

我遇到了一个nginx问题。Nginx/PHP-FPM使用多个webroot

我想不同的两种情况:

  • 首先,如果请求URL匹配/ API /我想如果返回API/index.php文件

  • 否则(*)。 URL与它不匹配,必须返回public/index.php。

我已经尝试了几种解决方案,包括:

有人可以解释我如何实现这一目标?

THX :)

我的文件组织这样的:

/var/www/html 
| 
_ api 
| | 
| _ index.php 
| 
|_ public 
    | 
    _ index.php 
    | 
    _ js 
     | 
     _ index.js 

这里是我的服务器配置:片段的

server { 
      listen 80; 
      server_name _; 

      index index.php; 
      rewrite_log on; 


      location/{ 

        root /var/www/html/public; 
        try_files $uri $uri/ /index.php$is_args$args; 

        location ~ \.php { 

          include snippets/fastcgi-php.conf; 
          fastcgi_pass unix:/run/php/php7.1-fpm.sock; 
        } 

      } 

      location ^~ /api { 

        root /var/www/html/api; 
        try_files $uri $uri/ /index.php$is_args$args; 

        error_log /var/log/nginx/admin-error.log debug; 

        location ~ \.php { 

          include snippets/fastcgi-php.conf; 
          fastcgi_pass unix:/run/php/php7.1-fpm.sock; 
        } 
      } 
    } 

内容/ FastCGI的-php.conf:

# regex to split $uri to $fastcgi_script_name and $fastcgi_path 
fastcgi_split_path_info ^(.+\.php)(/.+)$; 

# Check that the PHP script exists before passing it 
try_files $fastcgi_script_name =404; 

# Bypass the fact that try_files resets $fastcgi_path_info 
# see: http://trac.nginx.org/nginx/ticket/321 
set $path_info $fastcgi_path_info; 
fastcgi_param PATH_INFO $path_info; 

fastcgi_index index.php; 
include fastcgi.conf; 

而他再次是通过管理的error.log文件:

2017/08/14 18:53:31 [debug] 20331#20331: *75 http cl:-1 max:1048576 
2017/08/14 18:53:31 [debug] 20331#20331: *75 rewrite phase: 3 
2017/08/14 18:53:31 [debug] 20331#20331: *75 post rewrite phase: 4 
2017/08/14 18:53:31 [debug] 20331#20331: *75 generic phase: 5 
2017/08/14 18:53:31 [debug] 20331#20331: *75 generic phase: 6 
2017/08/14 18:53:31 [debug] 20331#20331: *75 generic phase: 7 
2017/08/14 18:53:31 [debug] 20331#20331: *75 access phase: 8 
2017/08/14 18:53:31 [debug] 20331#20331: *75 access phase: 9 
2017/08/14 18:53:31 [debug] 20331#20331: *75 access phase: 10 
2017/08/14 18:53:31 [debug] 20331#20331: *75 post access phase: 11 
2017/08/14 18:53:31 [debug] 20331#20331: *75 try files phase: 12 
2017/08/14 18:53:31 [debug] 20331#20331: *75 http script var: "/api" 
2017/08/14 18:53:31 [debug] 20331#20331: *75 trying to use file: "/api" 
"/var/www/html/api/api" 
2017/08/14 18:53:31 [debug] 20331#20331: *75 http script var: "/api" 
2017/08/14 18:53:31 [debug] 20331#20331: *75 trying to use dir: "/api" 
"/var/www/html/api/api" 
2017/08/14 18:53:31 [debug] 20331#20331: *75 http script copy: "/index.php" 
2017/08/14 18:53:31 [debug] 20331#20331: *75 http script var: "" 
2017/08/14 18:53:31 [debug] 20331#20331: *75 trying to use file: 
"/index.php" "/var/www/html/api/index.php" 
2017/08/14 18:53:31 [debug] 20331#20331: *75 internal redirect: 
"/index.php?" 


nginx version: nginx/1.12.1 

回答

0

这种配置工程:)

server { 

    listen 80; 
    server_name _; 

    index index.php; 
    rewrite_log on; 

    root /var/www/html; 


    location/{ 

     try_files $uri /public/index.php$is_args$args; 
    } 

    location ^~ /api { 

     try_files $uri /api/index.php$is_args$args; 

     location ~ \.php { 

      include snippets/fastcgi-php.conf; 
      fastcgi_pass unix:/run/php/php7.1-fpm.sock; 
     } 

    } 

    location ~ \.php { 

     include snippets/fastcgi-php.conf; 
     fastcgi_pass unix:/run/php/php7.1-fpm.sock; 
    } 
}