간단한 PHP 앱을 제공하도록 nGinx를 어떻게 구성해야합니까 (라우팅 문제)? listen 3000;

작은 PHP 앱을 제공하도록 nGinx 서버 (Debian Wheezy 상자)를 구성하려고합니다. 라우팅에 문제가 있습니다.

내가 무엇이 필요 하나:

/-PHP는 허용되지 않고 .html 파일 만

/ api-/ api / method1, / api / method2 등 모든 것이 /api/index.php로 이동합니다.

그게 다야.

내가 지금 무엇을 :

server {
    listen 3000;

    root /home/my_user/php/my_app;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            try_files /index.html =404;
    }
    location /api {
            try_files /index.php =404;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            # With php5-cgi alone:
            #fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

작동하는 것 :

/는 /home/my_user/php/my_app/index.html을 제공합니다. 괜찮습니다.

/ api는 404를 제공하지만 괜찮지 않습니다 (/api/index.php 파일로 이동해야 함). / api / foo도 마찬가지입니다. (예, /api/index.php 파일이 존재하며 api / 서브 디렉토리처럼 모든 사람이 읽을 수 있습니다.)

/api/index.php는 404를 제공합니다 (그러나 다른 것은 확실하지 않습니다, 무슨 일이 일어나고 있는지).

내가 찾은 nginx / php 튜토리얼에 따르면 모든 것이 정상이어야합니다. 그렇지 않습니다.

내 nginx를 어떻게 구성해야합니까?



답변

구성을 단순화 할 수 있습니다.

server {
    listen 3000;

    root /home/my_user/php/my_app;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location /api/ {
        try_files /api/index.php =404;

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
    }
}

첫째, 정적 파일을 제공하는 것은 기본 nginx 동작이며, 특별한 블록이 필요하지 않습니다. index지시문은 nginx를 검색하기에 충분합니다 index.html.

에 대한 요청이지도하기 /api/something/api/index.php당신과 같이 전체 경로를 사용해야 try_files에서 파일로 구조 경로 root지시를. 또한 fastcgi 지시문은이 위치에 있어야합니다. try_files파일을 찾은 경우 현재 컨텍스트 (예 : inside)에서 처리 되기 때문 location /api/입니다.

“PHP를 허용하지location ~ .*\.php$ 않기 때문에 필요가 없습니다 .