首页 > 解决方案 > 访问php文件返回404代码但在其他文件上成功

问题描述

我正在运行 docker 的四个镜像,它们是 php 7.3、nginx 1.17、redis 5.0.5 和 mysql 8.0,所有这些都是从 DockerHub 上的官方镜像中提取的。然后我创建一个docker-compose.yml文件,看起来如下:

version: "3.1"
services:

  nginx:
    image: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - $PWD/../:/usr/share/nginx/html
      - $PWD/nginx/conf.d:/etc/nginx/conf.d
    links:
      - php
    depends_on:
      - php
    networks:
      - net-app
    container_name: nginx

  mysql:
    image: mysql
    command: "--innodb_use_native_aio=0"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"
    volumes:
      - $PWD/mysql/datadir:/var/lib/mysql
      - $PWD/mysql/conf.d:/etc/mysql/conf.d
    networks:
      - net-app
    container_name: mysql


  php:
    image: php
    restart: always
    tty: true
    ports:
      - "9000:9000"
      - "9501:9501"
    volumes:
      - $PWD/../:/var/www/html/
    links:
      - mysql
      - redis
    depends_on:
      - mysql
      - redis
    networks:
      - net-app
    container_name: php

  redis:
    image: redis:5.0.5
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - $PWD/redis/datadir:/data
    networks:
      - net-app
    container_name: redis

networks:
  net-app:
    driver: bridge

info.php然后我用and创建一个项目test.txt

有我的 nginx 配置:

server {
    listen       80;
    server_name  test.site.com;

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

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/test;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME
 /var/www/html/test/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

配置并启动 docker 后,我访问 urltest.site.com/info.php并得到一个 nginx 404 错误页面,但我可以访问test.site.com/test.txt. 它返回文件中的文本。为什么?我附加到 php 容器中并运行php命令,它可以工作。我不知道原因,我尝试了一些方法但失败了,我在谷歌上找不到答案。

更新
有一些细节,我在大约 3 周前提取了 php:5.6,我复制docker-compose.yml了文件并修改了 php 部分的 repo/tag。但是我无法成功启动php,它总是处于重新启动状态,然后我添加了配置tty: true,然后php容器可以继续运行

更多更新

如果我删除以下内容:

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html/test;
}

错误页面变成502 bad gateway

标签: phpdockernginx

解决方案


我认为您想在 PHP 容器中运行 PHP-FPM,而不仅仅是 PHP-CLI。在docker-compose.yml

services:
    php:
        image: php:fpm

推荐阅读