하위 폴더에서 Laravel을 실행하기 위해 nginx 및 fastcgi를 설정하는 방법은 무엇입니까?

하위 폴더에서 Laravel을 실행하기 위해 nginx 및 fastcgi를 설정하는 방법은 무엇입니까?

단일 도메인의 하위 폴더 아래에 여러 개의 Laravel 앱을 설정하고 있습니다(소셜 네트워크 앱, https 필요, 단일 인증서 필요).

/app1/HTTP 요청을 Laravel 설치에 매핑하기 위해 nginx 구성을 작성하는 방법을 알 수 없습니다 ./var/www/other_folder/public/

다음은 구성 테스트의 최신 반복입니다. 디버그 로깅이 생략되었습니다. Laravel 루트 위치 /app1/은 작동하지만 경로 매핑( /app1/api/method/)은 작동하지 않습니다. nginx가 요청을 단계별로 처리하는 방법을 디버깅하는 방법(디버그 로그는 설명이 되지 않음)이나 Laravel의 하위 폴더를 매핑하는 방법에 대한 힌트를 제공 /app1/...해주세요 index.php.

server {
    listen      80;
    server_name  apps.mydomain.com;
    root    /var/www/apps.mydomain.com/docs;

    location /  {
        index   index.html index.php;
    }

    location /app1    {
        alias   /var/www/other_folder/public;
        index   index.php   index.html;
        try_files   $uri  $uri/ /app1/index.php$is_args$args /app1/index.php?$query_string;
    }

    location ~ /app1/.+\.php$ {
        rewrite ^/app1/(.*)$  /$1  break;
        include /etc/nginx/fastcgi.conf;
        fastcgi_param    SCRIPT_FILENAME    /var/www/other_folder/public$fastcgi_script_name;

        fastcgi_index  index.php;
        fastcgi_pass php;

        #   Database access parameters
        fastcgi_param   DB_HOST "localhost";
        fastcgi_param   DB_USER "apps";
        fastcgi_param   DB_PASS "xxxxxxxx";
        fastcgi_param   DB_NAME "app1";
    }

    # Other locations skipped
    include /etc/nginx/global/php.conf; # other php scripts
}

답변1

위치의 루트를 변경해야 한다고 생각합니다. Nginx 문서에 따르면 루트 지시문에는 "location"이라는 컨텍스트도 있습니다.

구문: 루트 경로;

컨텍스트: http, 서버, 위치(위치에 있는 경우)

따라서 다음과 같은 작업을 수행할 수 있어야 합니다.

server {
    listen      80;
    server_name  apps.mydomain.com;
    root    /var/www/apps.mydomain.com/docs;

    location /  {
        index   index.html index.php;
    }

    location /app1    {
        root   /var/www/other_folder/public;
        rewrite ^/app1/(.*)$  /$1  break;
        index   index.php   index.html;
        try_files   $uri  $uri/ /index.php$is_args$args /index.php?$query_string;
    }

    location ~ /app1/.+\.php$ {
        root   /var/www/other_folder/public;
        rewrite ^/app1/(.*)$  /$1  break;
        include /etc/nginx/fastcgi.conf;
        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;

        fastcgi_index  index.php;
        fastcgi_pass php;

        #   Database access parameters
        fastcgi_param   DB_HOST "localhost";
        fastcgi_param   DB_USER "apps";
        fastcgi_param   DB_PASS "xxxxxxxx";
        fastcgi_param   DB_NAME "app1";
    }

    # Other locations skipped
    include /etc/nginx/global/php.conf; # other php scripts
}

내가 아는 한, 별칭 지시문은 URL을 다른 URL로 다시 매핑하고 처리가 계속되며 소스 파일이 있는 디렉터리를 정의하지 않습니다. PHP 위치를 다시 작성해야 하는지 확실하지 않습니다.

답변2

이 코드는 테스트되지 않았습니다. 그러나 당신은 그것이 /app1/ 에서는 작동하지만 /app1/api/method/ 에서는 작동하지 않는다고 말하고 있습니다. 나는 당신의 문제가 이 부분에 있다고 생각합니다.

location /app1    {
    alias   /var/www/other_folder/public;
    index   index.php   index.html;
    try_files   $uri  $uri/ /app1/index.php$is_args$args /app1/index.php?$query_string;
}

따라서 이는 기본적으로 을 시도하고 url, 일치하지 않으면 디렉터리로 일치시키려고 시도하고 url/, 파일과 디렉터리가 존재하지 않으면 으로 일치시키려고 시도하고, /app1/index.php?someargs존재하지 않으면 일치하도록 지시합니다. 그것은/app1/index.php?$query_string

가장 먼저,별칭 및 try_files함께 작동하지 않으므로 대신 루트를 사용할 수 있습니다. /app1 위치 블록을 다음으로 변경하여 문제가 해결되는지 확인하세요.

location /app1    {
    root   /var/www/other_folder/public;
    index   index.php   index.html;
    try_files $uri $uri/ /index.php$is_args$args;
}

둘째, $query_string은 $args와 동일합니다. 유일한 차이점은읽기 전용.

내가 말했듯이 이것은 테스트되지 않았지만 작동한다면 좋을 것입니다. 그렇지 않은 경우에도 여전히 다른 아이디어가 있습니다.

관련 정보