[英]Docker nginx php-fpm not serving php
I'm trying to setup nginx and php-fpm images to work together for a Laravel application.我正在尝试设置 nginx 和 php-fpm 图像,以便为 Laravel 应用程序协同工作。 This is my set up:这是我的设置:
docker-compose.yml docker-compose.yml
version: "3.8"
services:
server:
container_name: nginx
build:
context: .
dockerfile: docker/nginx/Dockerfile
volumes:
- ./src/public:/var/www/html/public
ports:
- "8888:80"
depends_on:
- php
networks:
- laravel_net
php:
container_name: php
build:
context: .
dockerfile: docker/php/Dockerfile
ports:
- "9000:9000"
volumes:
- ./src:/var/www/html
networks:
- laravel_net
networks:
laravel_net:
driver: 'bridge'
Nginx Nginx
FROM nginx:stable-alpine
# puts app.conf into the container as /etc/nginx/nginx.conf
# has by default this: include /etc/nginx/conf.d/*.conf;
COPY docker/nginx/conf.d/*.conf /etc/nginx/conf.d/
WORKDIR /var/www/html
COPY ./src/public /var/www/html/public
docker/nginx/conf.d/app.conf docker/nginx/conf.d/app.conf
server {
listen 80;
server_name laravel.local;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass php:9000;
}
}
PHP PHP
FROM php:8.1-fpm-alpine
WORKDIR /var/www/html
COPY ./src /var/www/html
EXPOSE 9000
Inside src folder is where the laravel app should live so for now I'm just outputting phpinfo() on the public dir just to check that everything is working as expected src/public/index.php在 src 文件夹内是 laravel 应用程序应该存在的地方所以现在我只是在公共目录上输出 phpinfo() 只是为了检查一切是否按预期工作 src/public/index.php
<?php
phpinfo();
If I run docker-compose up --build
and docker ps
I can see the containers running如果我运行docker-compose up --build
和docker ps
我可以看到容器正在运行
% docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123 laravel_server "/docker-entrypoint.…" 8 seconds ago Up 6 seconds 0.0.0.0:8888->80/tcp nginx
abc456 laravel_php "docker-php-entrypoi…" 8 seconds ago Up 7 seconds 0.0.0.0:9000->9000/tcp php
also on both docker-compose exec server /bin/sh
and docker-compose exec php /bin/sh
I can see the index.php file inside the container同样在docker-compose exec server /bin/sh
和docker-compose exec php /bin/sh
我可以看到容器内的 index.php 文件
docker-compose exec server sh
/var/www/html # cat public/index.php
<?php
phpinfo();
/var/www/html # ls -la public
total 12
drwxr-xr-x 2 root root 4096 Feb 12 13:41 .
drwxr-xr-x 1 root root 4096 Feb 12 13:41 ..
-rw-r--r-- 1 root root 18 Feb 12 12:51 index.php
docker-compose exec php sh
/var/www/html # ls -lah public/
total 4K
drwxr-xr-x 3 root root 96 Feb 12 13:42 .
drwxr-xr-x 3 root root 96 Feb 10 21:28 ..
-rw-r--r-- 1 root root 18 Feb 12 13:42 index.php
/var/www/html # cat public/index.php
<?php
phpinfo();
However when I try to access http://localhost:8888
from the browser I get the default Welcome to nginx!但是,当我尝试从浏览器访问http://localhost:8888
时,我得到默认的Welcome to nginx! page.页。 Any ideas what I'm doing wrong?任何想法我做错了什么?
I think the problem is nginx config.我认为问题是 nginx 配置。 You are copying app.conf
to /etc/nginx/conf.d/
but there already exists default config /etc/nginx/conf.d/default.conf
.您正在将app.conf
复制到/etc/nginx/conf.d/
但已经存在默认配置/etc/nginx/conf.d/default.conf
。 Because you are seeing the default nginx page I would assume the default one is used.因为您看到的是默认的 nginx 页面,所以我假设使用的是默认页面。
Try to rename app.conf
to default.conf
or change copy command to尝试将app.conf
重命名为default.conf
或将复制命令更改为
COPY docker/nginx/conf.d/app.conf /etc/nginx/conf.d/default.conf
Based on the comments of the other answer:根据其他答案的评论:
There is no prioritization of configuration files at all.根本没有配置文件的优先级。 They are all become on giant NGINX configuration time at runtime.它们在运行时都变成了巨型 NGINX 配置时间。
In your case the default NGINX configuration will listen on port 80 as default server name.在您的情况下,默认 NGINX 配置将侦听端口 80 作为默认服务器名称。 That is where the default "Welcome to NGINX" page is configured.这是配置默认“欢迎使用 NGINX”页面的地方。
In your app configuration you set the server_name
to laravel.local
.在您的应用程序配置中,您将server_name
设置为laravel.local
。 Sending this as a Host-header curl -v -H "Host: laravel.local" 127.0.0.1:8888
will let NGINX pick the server configuration from your app.conf
for this specific Hostname.将其作为主机标头发送curl -v -H "Host: laravel.local" 127.0.0.1:8888
将让 NGINX 从您的app.conf
中为这个特定的主机名选择服务器配置。
You can add this to your clients /etc/hosts/
file to make it work from your web browser.您可以将其添加到您的客户端/etc/hosts/
文件中,以使其在您的 web 浏览器中运行。
127.0.0.1 laravel.local
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.