nginx: PHP-FPM 기본 설정 (FastCGI)

정적 파일과 PHP 실행을 분리하고, 보안 실수(임의 파일 실행)를 줄이는 기본 패턴

분야: DevOps/인프라 nginxphpphp-fpmfastcgi

PHP를 nginx에서 직접 실행하지 않고, PHP-FPM(FastCGI)으로 위임하는 가장 흔한 구성입니다.

관련 용어: PHP-FPM, FastCGI

기본 패턴

server {
  server_name example.com;
  root /var/www/example.com/public;
  index index.php index.html;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ \\.php$ {
    # (중요) 실제 파일이 없으면 실행하지 않기
    try_files $uri =404;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_index index.php;

    # 배포 환경에 맞게 소켓/포트 선택
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
  }
}

체크리스트

  • 업로드 디렉터리에서는 .php 실행을 더 강하게 차단(업로드 파일 실행 취약점 방지)
  • opcache/FPM 설정은 트래픽과 메모리에 맞춰 튜닝

관련 용어: OPcache

같은 분야의 템플릿