Nginx

在 Docker swarm 中使用 nginx 部署 php/laravel 應用程序會產生空白頁面並且沒有錯誤

  • August 23, 2018

我有一個 Laravel 應用程序,計劃部署在 Docker Swarm 上。問題是,無論我做什麼,呼叫localhost/index.php.

我正在使用 2 個單獨的服務:

  1. Laravel 應用程序 (php-fpm)
  2. Nginx

一些可能對你幫助我有幫助的筆記。

  • 除了靜態文件的捲之外,容器不共享任何內容
  • 我可以驗證 php-fpm 是否正在接收請求
  • 任何日誌都沒有錯誤,我得到的只是 nginx 日誌和 php-fpm 中的 200 個響應程式碼
  • 即使是簡單的也會<?php phpinfo();產生相同的結果,所以這不是 laravel 問題

這些是我建構圖像的 Dockerfile。

// Dockerfile for `Nginx`
FROM nginx

ADD ./api.conf /etc/nginx/conf.d/

RUN rm /etc/nginx/conf.d/default.conf

CMD ["nginx", "-g", "daemon off;"]

這是 nginx 配置文件:

server {
   listen 80;
   server_name _;
   root /app/public;

   add_header X-Frame-Options "SAMEORIGIN";
   add_header X-XSS-Protection "1; mode=block";
   add_header X-Content-Type-Options "nosniff";

   index index.html index.htm index.php;

   charset utf-8;

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

   location = /favicon.ico { access_log off; log_not_found off; }
   location = /robots.txt  { access_log off; log_not_found off; }

   error_page 404 /index.php;

   location ~ \.php$ {
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass api:9000;
       fastcgi_index index.php;
       include fastcgi_params;
   }

   location ~ /\.(?!well-known).* {
       deny all;
   }
}

這將是我的 laravel 圖像的 Dockerfile:

FROM ubuntu:bionic

RUN apt update
RUN apt install -y software-properties-common
RUN add-apt-repository -y ppa:ondrej/php

RUN apt install -y php7.2-fpm
RUN apt install -y php7.2-pgsql
RUN apt install -y php7.2-mongodb
RUN apt install -y php7.2-redis
RUN apt install -y php7.2-mbstring
RUN apt install -y php7.2-xml
RUN apt install -y php7.2-zip
RUN apt install -y php7.2-curl
RUN apt install -y ffmpeg

RUN mkdir /run/php

COPY . /app

RUN chmod 0777 /app/storage -R

RUN sed  -i 's/pm.max_children = 5/pm.max_children = 10/g' /etc/php/7.2/fpm/pool.d/www.conf && \
   sed  -i 's/pm.start_servers = 2/pm.start_servers = 4/g' /etc/php/7.2/fpm/pool.d/www.conf && \
   sed  -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 3/g' /etc/php/7.2/fpm/pool.d/www.conf && \
   sed  -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 5/g' /etc/php/7.2/fpm/pool.d/www.conf && \
   sed  -i 's/;pm.max_requests = 500/pm.max_requests = 500/g' /etc/php/7.2/fpm/pool.d/www.conf && \
   sed  -i 's|;access.log = log/$pool.access.log|access.log = /proc/self/fd/2|g' /etc/php/7.2/fpm/pool.d/www.conf && \
   sed  -i 's|;php_admin_value[error_log] = /var/log/fpm-php.www.log|php_admin_value[error_log] = /proc/self/fd/2|g' /etc/php/7.2/fpm/pool.d/www.conf && \
   sed  -i 's|error_log = /var/log/php7.2-fpm.log|error_log = /proc/self/fd/2|g' /etc/php/7.2/fpm/php-fpm.conf && \
   sed  -i 's|listen = /run/php/php7.2-fpm.sock|listen = "9000"|g' /etc/php/7.2/fpm/pool.d/www.conf

WORKDIR /app

EXPOSE 9000

CMD ["php-fpm7.2", "-F"]

最後一個想法是我的 docker-compose.yml 文件:

version: "3"
services:
 nginx:
   image: hamed/nginx
   ports: 
     - 80:80
   volumes:
     - my-files:/app/public/files
 api:
   image: hamed/api
   volumes:
     - my-files:/app/public/files

更新:lsof -Pn | grep LISTEN | grep ':80 ' @GerardH.Pille 要求 的結果:

nginx      13     root    6u     IPv4             206078      0t0    TCP *:80 (LISTEN)

更新 2: 這些是我nginx服務的日誌:

10.255.0.2 - - [21/Aug/2018:12:27:32 +0000] "GET /index.php HTTP/1.1" 200 5 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0" "-"

從我的php-fpm服務:

10.0.0.2 -  21/Aug/2018:12:27:32 +0000 "- " 200

好的,經過數小時的調試和搜尋,在我的 nginx 配置中發現以下兩行是為什麼不起作用以及為什麼沒有顯示錯誤:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;

nginx 容器沒有將請求的文件名傳遞給php-fpm. 請記住,要使其在不實際放入index.phpurl 的情況下工作,您需要index.php在 nginx 根文件夾中包含文件。它只需要存在。如果它是空的,那沒關係。

引用自:https://serverfault.com/questions/927163