You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Docker与Nginx部署PHP网站遇问题求助

问题:Nginx+PHP站点显示默认欢迎页,无法加载自定义index.php

尝试搭建PHP站点,依赖Traefik反向代理处理端口(已有其他站点正常运行),但部署后页面仅显示Nginx默认欢迎页,无法加载自定义的index.php。


docker-compose.yml 配置

version: '3'

services:

    example-website:
        container_name: example-website
        build: ./Dockerfile
        image: 'nginx:alpine'
        restart: unless-stopped
        links:
          - php
        networks:
          - proxy
          - default
        labels:
          - "traefik.enable=true"
          - "traefik.docker.network=proxy"
          - "traefik.http.routers.example-secure.entrypoints=websecure"
          - "traefik.http.routers.example-secure.rule=Host(`example.com`)"
        volumes:
          - /var/docker/test/html:/var/www/html
          
    php:
        image: php:7.1.11-fpm-alpine
        expose:
        - 9000
        volumes:
        - /var/docker/test/html:/var/www/html
        networks:
          - proxy
          - default

networks:
  proxy:
    external: true  

Nginx default.conf 配置

server {

     listen 80;
     root /var/www/html;
     index index.html 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; }

     access_log off;
     error_log /var/log/nginx/error.log error;

     sendfile off;
 client_max_body_size 100m;

     location ~ .php$ {
      fastcgi_split_path_info ^(.+.php)(/.+)$;
      fastcgi_pass php:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_intercept_errors off;
      fastcgi_buffer_size 16k;
      fastcgi_buffers 4 16k;
    }

     location ~ /.ht {
      deny all;
     }
    }

index.php 内容

<!DOCTYPE html>  
<head>  
 <title>Hello World!</title>
</head>  

<body>  
 <h1>Hello World!</h1>
 <p><?php echo 'We are running PHP, version: ' . phpversion(); ?></p>
</body>

Dockerfile 内容

FROM nginx:latest
COPY ./conf.d/default.conf /etc/nginx/conf.d/default.conf

当前异常现象

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.


排查与修复步骤

  • 核心问题:Docker配置冲突导致自定义Nginx配置未生效
    docker-compose中同时指定了build: ./Dockerfile和image: 'nginx:alpine',会直接拉取官方alpine镜像,忽略本地构建的镜像(包含自定义default.conf),Nginx始终加载默认配置。

  • 修复操作:

    1. 修改docker-compose.yml,删除image: 'nginx:alpine'行,确保使用本地构建的镜像:
      example-website:
          container_name: example-website
          build: .  # 简化为当前目录,默认读取Dockerfile
          restart: unless-stopped
          links:
            - php
          # 其余配置保持不变
      
    2. 同步修改Dockerfile的基础镜像为alpine版本(保持环境一致性):
      FROM nginx:alpine
      COPY ./conf.d/default.conf /etc/nginx/conf.d/default.conf
      
    3. 重新构建并启动容器:
      docker-compose down
      docker-compose build --no-cache
      docker-compose up -d
      
  • 额外验证项:

    • 检查宿主机/var/docker/test/html目录下的index.php文件存在,且权限为容器内nginx用户可读取
    • 查看Nginx容器日志,确认配置加载无错误:
      docker logs example-website
      

内容的提问来源于stack exchange,提问作者CodeBlue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 16:30:49