여러 정적 파일 디렉터리, 단일 PHP FPM 서버

여러 정적 파일 디렉터리, 단일 PHP FPM 서버

정적 자산을 제공하는 데 필요한 두 개의 디렉터리가 있습니다.

  1. /srv/web: 이미지, JavaScript, HTML 등을 포함하는 정적 자산
  2. /srv/php: 일부 정적 자산과 함께 동적 PHP 스크립트.

저는 NGINX를 사용하고 있으며 다음과 같이 구성했습니다.

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$Unix 소켓을 통해 PHP FPM에서 요청되며 이는 작동합니다.

현재 겪고 있는 문제는 의 index지시문이 server계획대로 작동하지 않는다는 것입니다. index.html에 파일이 있는데 파일이 /srv/web있으면

curl -is http://localhost/

404가 나옵니다.

이것이 PHP와 함께 제공되는 여러 파일 시스템 폴더가 있는 NGINX 사이트를 설정하는 가장 이상적인 방법입니까? 내 정적 인덱스가 작동하지 않는 이유에 대한 아이디어가 있습니까?


업데이트

아래 AD7six의 답변에 따라 구성을 다음과 같이 업데이트했습니다.

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"

이것이 왜 실패하는지 잘 모르겠지만 적어도 진전이 있는 것 같은 냄새가 납니다.

답변1

여러 루트는 그렇게 작동하지 않습니다

이 구성을 사용하면 다음과 같습니다.

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

작성된 요청이 파일과 일치해야 하므로 index 지시문을 사용하는 요청 처리가 없습니다. 디버그 로그를 사용하여 다음을 확인합니다.

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을 예상합니다. 이런 종류의 솔루션을 계속 사용하지 말 것을 제안합니다. 특히~ 아니다루트를 다음과 같이 /설정잠재적으로서버의 모든 파일에 대한 액세스를 허용합니다.

두 개의 위치 블록 사용

대신 일을 단순하게 유지하십시오. 이 구성은 모든 정적 콘텐츠를 제공합니다.

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

이 구성은 모든 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.html403 오류가 발생합니다(요청이 디렉터리에 대한 것이고 디렉터리 인덱스 파일이 없기 때문입니다). 이러한 요청을 PHP에서도 처리할 수 있도록 하려면 403 오류를 PHP 처리기로 리디렉션해야 합니다.

관련 정보