首页 > 解决方案 > 带有 PHP Nginx 的 Docker 无法加载图像或 js - 找不到 404

问题描述

我在我的 windows 10 pro 本地机器上部署了 2 个 docker 容器(nginx 和 php fpm)。

我可以正确执行 php 脚本。

例如:http://localhost:8888/phpinfo.php --> 这个返回正确。

但除了 php 脚本(例如图像、js 或 css)之外,它给了我没有找到。

例如:http://localhost:8888/image.jpg --> 未找到

这里可能出了什么问题?

这是我的码头工人撰写文件:

version: '3.7'
services:
  # The Web Server
  web:
    container_name: emm_web
    build:
      context: ./
      dockerfile: web.dockerfile
    volumes:
      - ../log/:/var/log
    ports:
      - 8888:80

  # The PHP Application
  app:
    container_name: emm_app
    build:
      context: ./
      dockerfile: app.dockerfile
    volumes:
      - ../www/:/var/www
    depends_on:
      - web
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"

这是我的 nginx vhost.conf:

server {
    listen 80;
    index index.php index.html;
    root /var/www;

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

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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

谢谢 :)

标签: phpdockernginx

解决方案


您需要为 nginx 容器共享文件。尝试这个:

# The Web Server
web:
  container_name: emm_web
  build:
    context: ./
    dockerfile: web.dockerfile
  volumes:
    - ../log/:/var/log
    # !! nginx need this volume !!
    - ../www/:/var/www:ro
  ports:
    - 8888:80

该只读卷将使容器中的文件可用。


推荐阅读