Nginx - 특정 URL의 루트 폴더를 변경하면 404 오류가 발생합니다.

Nginx - 특정 URL의 루트 폴더를 변경하면 404 오류가 발생합니다.

URL을 통해 누구나 얻을 수 있는 위치 블록을 정렬하려고 합니다 mydomain.com/game/admin. 콘텐츠를 가져오는 nginx 서버가 디렉토리에 있는지 확인하세요 /var/www/html/my-cakephp-app/. 내 애플리케이션은 cakephp 프레임워크를 사용하여 빌드되었으며 해당 디렉토리 구조는 아래와 같습니다.

  • /var/www/html/my-cakephp-app/
    • 관리자
      • 구성
      • 콘솔
      • 제어 장치
      • 보다
      • webroot(앱 진입점 index.php 파일이 이 디렉터리에 있음)

또한 디렉토리에 정적 html/css 웹사이트가 있습니다 /var/www/html. 따라서 mydomain.comURL이 있는 사람은 누구나 해당 웹사이트를 볼 수 있습니다.

현재 nginx 서버 블록은 다음과 같습니다.

server {
    listen 80;
    listen [::]:80;

    root /var/www/html;

    index index.html index.htm index.php;

    server_name mydomain.com;

    location / {
        try_files $uri $uri/ =404;
    }
    
    location /game/admin {
            return 301 /game/admin/;
    }

    location /game/admin/ {

                root /var/www/html/my-cakephp-app/admin/webroot;
                try_files $uri $uri/ /game/admin/index.php$is_args$args;
  
                location ~* \.php(/|$) {
                  include snippets/fastcgi-php.conf;
                  fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               } 

    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

이 설정을 사용하면 내 정적 웹사이트가 제대로 작동합니다. 그러나 cakephp 응용 프로그램은 브라우저에서 404 찾을 수 없음 오류를 표시합니다. nginx/error.log에 오류가 없습니다.

그러나 아래의 nginx 구성으로 실행하면 내 응용 프로그램이 제대로 작동합니다. 하지만 html/css 사이트를 없애야 합니다. html/css 앱을 wordpress 사이트로 업그레이드할 계획입니다. 그래서 나는 부모로서 워드프레스 사이트를 운영할 수 있는 능력을 가져야 합니다.

server {
    listen 80;
    server_name mydomain.com;
    root /var/www/html/my-cakephp-app/admin/webroot;
    
    index index.html index.htm index.php;
 
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
        autoindex on;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
    
    location ~ /\.ht {
        deny all;
    }
}

첫 번째 서버 블록에서 내가 무엇을 잘못했는지 생각할 수 없었습니다. 어떤 제안이라도 도움이 될 것입니다.

답변1

두 가지 주요 문제는 다음과 같습니다.

  • 수정자를 사용하지 않는 한 외부 location ~ \.php$블록이 블록보다 우선합니다 (참조:location /game/admin/^~이 문서자세한 내용은)
  • 지시문 root은 간단한 연결을 통해 파일에 대한 경로를 생성하므로 컨트롤러는 다음 위치에 있을 것으로 예상됩니다 /var/www/html/my-cakephp-app/admin/webroot/game/admin/index.php(참조이 문서자세한 내용은)

한 가지 옵션은 디렉터리 구조가 URI 구조와 일치하도록 프로젝트를 이동하는 것입니다. 이는 외부 블록이 두 프로젝트를 모두 실행할 수 있는 경우를 /var/www/html/game/admin가리키는 기호 링크를 사용하여 달성할 수 있습니다 ./var/www/html/my-cakephp-app/admin/webrootlocation ~ \.php$


또 다른 옵션은 alias지시어입니다. 보다이 문서자세한 내용은.

location ^~ /game/admin {
    alias /var/www/html/my-cakephp-app/admin/webroot;

    if (!-e $request_filename) { rewrite ^ /game/admin/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

$document_root$fastcgi_script_name에서는 작동하지 않으므로 대신 alias사용해야 $request_filename합니다.

나는 다음과 같은 이유로 aliasand 를 함께 사용하는 것을 피합니다.try_files이 문제. 보다이 주의지시문 의 사용에 대해 if.

관련 정보