Open6

【docker】php-fpmを使う際のnginxのconfファイルについて

HondaHonda

イメージのバージョン
image: nginx:1.19.9
nginxのデフォルト設定ファイルは
/etc/nginx/nginx.conf
にある。

/etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
HondaHonda

最後の行に
include /etc/nginx/conf.d/*.conf;
とある。

インクルードしているディレクトリの、デフォルトの中身はこのファイル一つだけ↓
/etc/nginx/conf.d/default.conf

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

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

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

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

nginxをプロキシサーバーとして、php-fpmをアプリケーションサーバーとして使う場合、この/etc/nginx/conf.d/default.confを編集する。

HondaHonda

nginxで受け取ったリクエストをphp-fpmに渡す。
そのためには、、、

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

上記のコメントアウトを解除し、FastCGIへのリクエストの受け渡しを有効化する(php-fpmはphp-FastCGI Process Managerの略)。

解除後

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    
    location ~ \.php$ {
       root           html;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
       include        fastcgi_params;
   }

編集後

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    
    location ~ \.php$ {
       root           /var/www/html/public;
       fastcgi_pass   php-fpm:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
    }

一行ずつ見ていく。
root /var/www/html/public;
これはphp-fpmコンテナのドキュメントルートを指定している。この場合のドキュメントルートはindex.phpがある場所へのパスのこと。
ルートが/var/www/html なのは、php-fpmのデフォルトのワーキングディレクトリが/var/www/htmlだから。ここではその先に/publicディレクトリを作り、そこにindex.phpを置いていることを想定している。
fastcgi_pass php-fpm:9000;
php-fpmの場所を指定している。php-fpmという名前でコンテナを作成していることを想定。dockerが自動的に名前解決を行ってくれるため、このようにconfファイルの中でもコンテナの名前を使うことができる。php-fpmはデフォルトで9000番ポートを使用する。
https://github.com/docker-library/php/blob/64811791f0682262478d73514819908fcfe73d7f/8.0/buster/fpm/Dockerfile
fastcgi_index index.php;
起点となるファイルを指定する。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
サーバー情報の設定。/scriptsを$document_root(=/var/www/html/public)に変更。

HondaHonda

/etc/nginx/conf.d/default.conf

編集後

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

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

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

index.phpを追加。

HondaHonda

上記の例では、nginxの /usr/share/nginx/html には.phpファイルを置かなければならない。(そうしないとnginxはphp-fpmにリクエストを渡してくれない)