Docker - Nginx 和 PHP 容器可以工作,但不提供 html 文件

问题描述 投票:0回答:2

我有以下设置:Windows 10 Pro 运行 docker for windows(两者都是最新版本)。

我有 2 个容器/图像“nginx_contianer”和“php_container”,它们是由两个 docker 文件通过 docker-compose.yml 文件构建的。

我有以下文件被放入 /var/www/html/ 的 php_container 中 index.php (phpinfo())、test.html (html hello world) 和 test.php (echo 的 hello world)

我使用的 nginx.conf 提供 php 文件(index.php / 和 test.php 工作),但不会提供 html 文件... test.html 结果为 404。该文件与 php_container 中的 php 文件一起存在。 大家有什么想法吗?

更新 看起来 html 文件需要从位于 /var/www/html 的 nginx_contianer 提供服务

如何从同一目录获取要提供的 html 和 php 文件?

这是我的 docker 文件。

文件:Dockerfile.nginx

FROM nginx:alpine

# RUN rm /etc/nginx/conf.d/default.conf
RUN mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak

# USER root

# Ensure nginx user has permissions to the cache and pid directory
RUN mkdir -p /var/cache/nginx/client_temp && \
    chown -R nginx:nginx /var/cache/nginx && \
    mkdir -p /var/run/nginx && \
    chown -R nginx:nginx /var/run/nginx

# Update, upgrade and install some things
RUN apk update && \
    apk upgrade && \
    apk add --no-cache \
        nano \
        ncdu

# Switch back to nginx user
# USER nginx

# Copy a new configuration file from your local to the container
COPY ./nginx/nginx.conf /etc/nginx/conf.d
RUN chown -R nginx:nginx /etc/nginx/conf.d

# Expose port 80 and 443 to the host
EXPOSE 80 443

WORKDIR /etc/nginx/conf.d

文件:Dockerfile.php

FROM php:8.3-fpm-alpine

# Update, upgrade and install some things
RUN apk update && \
    apk upgrade && \
    apk add --no-cache \
        nano \
        ncdu \
        libzip-dev \
        zip


# Install some stuff
RUN docker-php-ext-install \
    zip

# Clean up
RUN rm -rf /var/cache/apk/*

COPY --chown=www-data:www-data ./src/ /var/www/html/
RUN chmod -R 755 /var/www/html

WORKDIR /var/www/html/

文件:docker-compose.yml

version: '3.8'

services:
  php_container:
    container_name: php_container
    build:
      context: .
      dockerfile: Dockerfile.php
    restart: on-failure:3
    networks:
      - backend

  nginx_container:
    container_name: nginx_container
    build:
      context: .
      dockerfile: Dockerfile.nginx
    ports:
      - "80:80"
      - "443:443"
    restart: on-failure:3
    depends_on:
      - php_container
    networks:
      - backend
      - default

networks:
  backend:

文件:nginx.conf

server {
    listen 80;
    server_name localhost;

    root /var/www/html;
    index index.php index.html index.htm;

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

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

    location / {
        try_files $uri $uri/ =404;
        # try_files $uri $uri/ /index.php?$query_string; # works but everything goes to index.php
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php_container:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
php docker nginx docker-compose fpm
2个回答
0
投票

非 PHP 源文件(HTML、CSS 等)需要可由 nginx 容器访问,因为 nginx 无法从 PHP 容器中提供它们。如果 nginx 随后遇到 PHP 文件,它将通过 FPM 套接字将该文件的路径传递给 PHP 容器,然后 PHP 容器将在本地访问它。所以,简单的解决方案,只需将文件添加到 nginx 容器即可:

COPY --chown=www-data:www-data ./src/ /var/www/html/
RUN chmod -R 755 /var/www/html

理想情况下,您希望它们仅位于一个位置,您可以使用另一个静态容器或安装座来完成此操作。


0
投票

答案是使用卷...

将我的 docker-compose.yml 修改为此,现在工作正常。

version: '3.8'

services:

  php_container:
    container_name: php_container
    build:
      context: .
      dockerfile: Dockerfile.php
    restart: on-failure:3
    volumes:
      - html_data:/var/www/html
    networks:
      - backend

  nginx_container:
    container_name: nginx_container
    build:
      context: .
      dockerfile: Dockerfile.nginx
    ports:
      - "80:80"
      - "443:443"
    restart: on-failure:3
    depends_on:
      - php_container
    volumes:
      - html_data:/var/www/html
    networks:
      - backend
      - default

volumes:
  html_data:

networks:
  backend:
© www.soinside.com 2019 - 2024. All rights reserved.