我有一个 Laravel 应用程序,计划将其部署在 Docker Swarm 上。问题是,无论我做什么,调用时都会出现空白页localhost/index.php
。
我使用两个独立的服务来:
- Laravel 应用程序 (php-fpm)
- 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
答案1
好的,经过几个小时的调试和搜索后,发现我的 nginx 配置中的以下两行是为什么它不工作以及为什么没有显示错误:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
nginx 容器未将请求的文件名传递给php-fpm
。请记住,要使其在不实际输入index.php
URL 的情况下工作,您需要index.php
在 nginx 根文件夹中有文件。它只需存在即可。如果它是空的也没关系。