programing

Nginx로 PHP를 실행할 때 파일을 찾을 수 없음

procenter 2021. 1. 17. 12:10
반응형

Nginx로 PHP를 실행할 때 파일을 찾을 수 없음


최근에 최신 버전의 Nginx를 설치했는데 PHP를 실행하는 데 어려움을 겪고있는 것 같습니다.

도메인에 사용중인 구성 파일은 다음과 같습니다.

server {
listen       80;
server_name  localhost;

location / {
    root   /usr/share/nginx/html;
    index  index.php;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

}

다음은 오류 로그 파일에 표시되는 오류입니다.

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

다른 * fastcgi_param *을 시도하십시오.

fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;

"파일을 찾을 수 없음"문제가있어서 "루트"정의를 "서버"대괄호로 옮겨 모든 위치에 대한 기본값을 제공했습니다. 자신의 루트 위치를 지정하여 언제든지이를 재정의 할 수 있습니다.

server {
    root /usr/share/nginx/www;
    location / {
            #root /usr/share/nginx/www;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

또는 두 위치 모두에 루트를 정의 할 수 있습니다.


아마 대답하기에는 너무 늦었지만 이것은 정말 성가신 오류이기 때문에 몇 가지 사항이 있습니다. 다음 솔루션은 Mac OS X Yosemite에서 작동했습니다.

  1. 가지고 있다면 최고입니다

fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;

  1. 빠른 cgi 매개 변수가 포함 된 포함은 해당 행 위에 있어야합니다.

  2. 실행중인 PHP 파일 (해당 파일 포함)까지 모든 디렉토리에 a+x권한이 있어야합니다.

sudo chmod a+x /Users/
sudo chmod a+x /Users/oleg/
sudo chmod a+x /Users/oleg/www/
sudo chmod a+x /Users/oleg/www/a.php

나는 동일한 문제를 겪고 있었고 테스트 중에 두 가지 문제에 직면했습니다.

1º : "파일을 찾을 수 없음"

2º : 404 오류 페이지

제 경우에는 다음과 같은 사실을 알게되었습니다.

Nginx 볼륨과 PHP 볼륨 모두에 공용 폴더에 대한 볼륨을 마운트해야했습니다.

가 장착 있다면 Nginx에 와에 장착되지 않은 PHP , 그것을 줄 것이다 : " 파일을 찾을 수 없습니다 "

예 ( "파일을 찾을 수 없음 오류"표시) :


services:
  php-fpm:
    build:
      context: ./docker/php-fpm
  nginx:
    build:
      context: ./docker/nginx
    volumes:
        #Nginx Global Configurations
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

        #Nginx Configurations for you Sites:

        # - Nginx Server block
      - ./sites/example.com/site.conf:/etc/nginx/sites-available/example.com.conf
        # - Copy Public Folder:
      - ./sites/example.com/root/public/:/var/www/example.com/public
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-fpm
    restart: always

그것의가에 장착하면 PHP 및 장착되지 Nginx에 , 그것은 줄 것이다 404 페이지를 찾을 수 없습니다 오류 .

Example (Will throw 404 Page Not Found Error):

version: '3'

services:
  php-fpm:
    build:
      context: ./docker/php-fpm
    volumes:
      - ./sites/example.com/root/public/:/var/www/example.com/public
  nginx:
    build:
      context: ./docker/nginx
    volumes:
        #Nginx Global Configurations
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

        #Nginx Configurations for you Sites:

        # - Nginx Server block
      - ./sites/example.com/site.conf:/etc/nginx/sites-available/example.com.conf
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-fpm
    restart: always

And this would work just fine (mounting on both sides) (Assuming everything else is well configured and you're facing the same problem as me):

version: '3'

services:
  php-fpm:
    build:
      context: ./docker/php-fpm
    volumes:
      # Mount PHP for Public Folder
      - ./sites/example.com/root/public/:/var/www/example.com/public
  nginx:
    build:
      context: ./docker/nginx
    volumes:
        #Nginx Global Configurations
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

        #Nginx Configurations for you Sites:

        # - Nginx Server block
      - ./sites/example.com/site.conf:/etc/nginx/sites-available/example.com.conf
        # - Copy Public Folder:
      - ./sites/example.com/root/public/:/var/www/example.com/public
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-fpm
    restart: always

Also here's a Full working example project using Nginx/Php, for serving multiple sites: https://github.com/Pablo-Camara/simple-multi-site-docker-compose-nginx-alpine-php-fpm-alpine-https-ssl-certificates

I hope this helps someone, And if anyone knows more about this please let me know, Thanks!


I just spent like 40 minutes trying to debug a non-working /status with:

$ SCRIPT_NAME=/status SCRIPT_FILENAME=/status QUERY_STRING= REQUEST_METHOD=GET cgi-fcgi -bind -connect /var/run/php5-fpm.sock

It just produced "File not found" error, while the actual scripts (that are found on the filesystem) worked just fine.

Turned out, I had a couple of orphaned processes of php5-fpm. After I killed everything and restarted php5-fpm cleanly, it just went back to normal.

Hope this helps.


In my case the PHP-script itself returned 404 code. Had nothing to do with nginx.


In my case, it was because the permissions on the root web directory were not set correctly. To do this, you need to be in the parent folder when you run this in terminal:

sudo chmod -R 755 htmlfoldername

This will chmod all files in your html folder, which is not recommended for production for security reasons, but should let you see the files in that folder, to be sure that isn't the issue while troubleshooting.


I had this error as well. In my case it was because there was another virtual host that was pointing to the same root directory.


After upgrading to PHP72, we had an issue where the php-fpm.d/www.conf lost the settings for user/group which was causing this error. Be sure to double check those if your setup involves php-fpm.


For me, problem was Typo in location path.

Maybe first thing to check out for this kind of problem

Is path to project.


When getting "File not found", my problem was that there was no symlink in the folder where was pointing this line in ngix config:

root /var/www/claims/web;

ReferenceURL : https://stackoverflow.com/questions/17808787/file-not-found-when-running-php-with-nginx

반응형