多个静态文件目录,单个PHP FPM服务器

我有两个目录,我需要提供静态资产:

  1. /srv/web :包含图片,JavaScript,HTML等的静态资源
  2. /srv/php :dynamicPHP脚本以及一些静态资产。

我正在使用NGINX并configuration它,如下所示:

 server { listen 80; server_name _; # root /; index index.php index.html index.htm; try_files /srv/web/$uri /srv/php/$uri =404; location ~ \.php$ { root /srv/php; try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /srv/php$fastcgi_script_name; include fastcgi_params; } } 

我在Ubuntu 14.04上,PHP FPM包版本是5.5.9,NGINX包版本是1.4.6。

这里的简单目标是首先为/srv/web提供静态文件,如果失败/srv/php ,则返回404.所有以\.php$结尾的\.php$将通过Unix套接字从PHP FPM请求,这是工作。

我目前遇到的问题是, server上的index指令没有按计划运行。 我在/srv/web有一个index.html文件,当我这样做

 curl -is http://localhost/ 

我得到一个404。

这是build立一个NGINX站点与多个文件系统文件夹一起服务于PHP的最理想的方式吗? 任何想法,为什么我的静态索引不工作?


更新

根据AD7six的回答,我已经更新了我的configuration,如下所示:

 server { listen 80; server_name _; # listen at all host names # serve static files first from /srv/web, then from /srv/php, and any dynamic PHP files from # FastCGI/FPM at the Unix socket. location / { root /srv/web; index index.html index.htm; try_files $uri $uri/ @php; } location @php { root /srv/php; index index.php; try_files $uri $uri/ =404; } location ~ \.php$ { root /srv/php; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /srv/php/$fastcgi_script_name; include fastcgi_params; } } 

我的目录列表如下所示:

 /srv/ |-- php | |-- demo.php | |-- index.php | `-- phpstatic.txt `-- web |-- static.html `-- subdir `-- index.html 3 directories, 5 files 

获取静态文件和PHP文件按计划工作,得到/subdir/索引工作正常,但如果我GET / ,我得到一个403禁止,并且nginx抱怨目录列表:

 2015/08/24 21:50:59 [error] 9159#0: *7 directory index of "/srv/web/" is forbidden, client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", host: "localhost" 

不知道为什么这是失败的,但至less闻起来像进步。

多根不会像那样工作

有了这个configuration:

 server { # root /; index index.php index.html index.htm; try_files /srv/web/$uri /srv/php/$uri =404; 

没有要使用索引指令的请求处理,因为写入请求必须匹配文件。 使用debugging日志证实这一点:

 2015/08/24 08:12:11 [debug] 17173#0: *26 try files phase: 13 2015/08/24 08:12:11 [debug] 17173#0: *26 http script copy: "/srv/web/" 2015/08/24 08:12:11 [debug] 17173#0: *26 http script var: "/" 2015/08/24 08:12:11 [debug] 17173#0: *26 trying to use file: "/srv/web//" "/srv/web//" 2015/08/24 08:12:11 [debug] 17173#0: *26 http script copy: "/srv/php/" 2015/08/24 08:12:11 [debug] 17173#0: *26 http script var: "/" 2015/08/24 08:12:11 [debug] 17173#0: *26 trying to use file: "/srv/php//" "/srv/php//" 2015/08/24 08:12:11 [debug] 17173#0: *26 trying to use file: "=404" "=404" 

使用try_files指令来查找像这样的目录:

 try_files /srv/web/$uri /srv/web/uri/ /srv/php/$uri /srv/php/$uri/ =404; 

也不起作用:

 2015/08/24 08:16:17 [debug] 17651#0: *33 http script copy: "/srv/web/" 2015/08/24 08:16:17 [debug] 17651#0: *33 http script var: "/srv/web//index.html" 2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use file: "/srv/web//srv/web//index.html" "/srv/web//srv/web//index.html" 2015/08/24 08:16:17 [debug] 17651#0: *33 http script copy: "/srv/web/" 2015/08/24 08:16:17 [debug] 17651#0: *33 http script var: "/srv/web//index.html" 2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use dir: "/srv/web//srv/web//index.html" "/srv/web//srv/web//index.html" 2015/08/24 08:16:17 [debug] 17651#0: *33 http script copy: "/srv/php/" 2015/08/24 08:16:17 [debug] 17651#0: *33 http script var: "/srv/web//index.html" 2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use file: "/srv/php//srv/web//index.html" "/srv/php//srv/web//index.html" 2015/08/24 08:16:17 [debug] 17651#0: *33 http script copy: "/srv/php/" 2015/08/24 08:16:17 [debug] 17651#0: *33 http script var: "/srv/web//index.html" 2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use dir: "/srv/php//srv/web//index.html" "/srv/php//srv/web//index.html" 2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use file: "=404" "=404" 

请注意,“根”是困惑的, try_files期望url不是文件path。 我build议不要继续尝试使用这种解决scheme – 特别是不要将根设置为/并且可能允许访问服务器上的任何文件。

使用两个位置块

相反,保持简单。 这个configuration将提供所有的静态内容:

  root /srv/web; index index.html; try_files $uri $uri/; 

这个configuration服务于所有的php内容:

  root /srv/php; index index.php; try_files $uri $uri/; 

把它们放在一起:

 location / { root /srv/web; index index.html; try_files $uri $uri/ @php; error_page 403 = @php; # see note below } location @php { root /srv/php; index index.php; try_files $uri $uri/ =404; } location ~ \.php$ { # as before } 

一个问题是,在这种设置下,匹配/srv/web没有 index.html文件的文件夹的请求将会引发403错误(因为请求是针对目录的,并且没有目录索引文件)。 为了允许这些请求也由php处理 – 有必要将403错误redirect到PHP处理程序。