
Eu tenho dois diretórios dos quais preciso servir ativos estáticos:
/srv/web
: ativos estáticos que incluem imagens, JavaScript, HTML, etc./srv/php
: Scripts PHP dinâmicos junto com alguns ativos estáticos.
Estou usando o NGINX e configurei assim:
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;
}
}
Estou no Ubuntu 14.04, a versão do pacote PHP FPM é 5.5.9, a versão do pacote NGINX é 1.4.6.
O objetivo simples aqui é servir arquivos estáticos primeiro /srv/web
, na falta disso /srv/php
, na falta disso, retorne 404. Todos os arquivos que terminam em \.php$
serão solicitados ao PHP FPM pelo soquete Unix, e isso está funcionando.
O problema que estou enfrentando atualmente é que a index
diretiva server
não está funcionando conforme planejado. Eu tenho um index.html
arquivo em /srv/web
, e quando o faço
curl -is http://localhost/
Eu recebo um 404.
Esta é a maneira ideal de configurar um site NGINX com várias pastas de sistema de arquivos para servir junto com o PHP? Alguma idéia de por que meu índice estático não está funcionando?
Atualizar
De acordo com a resposta do AD7six abaixo, atualizei minha configuração para ficar assim:
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;
}
}
Minha listagem de diretório é assim:
/srv/
|-- php
| |-- demo.php
| |-- index.php
| `-- phpstatic.txt
`-- web
|-- static.html
`-- subdir
`-- index.html
3 directories, 5 files
Obter arquivos estáticos e arquivos PHP funciona conforme planejado e obter /subdir/
seu índice funciona bem, mas se eu GET /
, recebo um 403 proibido e o nginx reclama da listagem de diretórios:
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"
Não sei por que isso está falhando, mas pelo menos parece progresso.
Responder1
Múltiplas raízes não funcionarão assim
Com esta configuração:
server {
# root /;
index index.php index.html index.htm;
try_files /srv/web/$uri /srv/php/$uri =404;
Não há processamento de solicitação que use a diretiva de índice, conforme escrito, a solicitação deve corresponder a um arquivo. Usar o log de depuração confirma isso:
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"
Usando uma try_files
diretiva que procura diretórios como este:
try_files /srv/web/$uri /srv/web/uri/ /srv/php/$uri /srv/php/$uri/ =404;
também não funciona:
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"
Observe que o "root" está confuso, try_files
espera um URL e não um caminho de arquivo. Sugiro que não continuemos a tentar utilizar uma solução deste tipo - especialmentenãodefinindo a raiz como /
epotencialmentepermitindo acesso a qualquer arquivo no servidor.
Use dois blocos de localização
Em vez disso, mantenha as coisas simples. Esta configuração servirá todo o conteúdo estático:
root /srv/web;
index index.html;
try_files $uri $uri/;
Esta configuração serve todo o conteúdo php:
root /srv/php;
index index.php;
try_files $uri $uri/;
Basta colocá-los juntos:
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
}
Uma pegadinha é que com esse tipo de configuração uma solicitação que corresponde a uma pasta na /srv/web
qualnãohave an index.html
file gerará um erro 403 (já que a solicitação é para um diretório e não há arquivo de índice de diretório). Para permitir que essas solicitações também sejam tratadas pelo php - é necessário redirecionar os erros 403 para o manipulador do php.