Php 适用于Windows和Symfony 4的Docker中的NGINX 502网关错误

Php 适用于Windows和Symfony 4的Docker中的NGINX 502网关错误,php,symfony,docker,nginx,fpm,Php,Symfony,Docker,Nginx,Fpm,我正在尝试在Windows机器上用NGINX和PHP-FPM在Docker中设置Symfony 3。目前,我得到一个502坏网关错误。我将FPM端口从9000更改为8000,因为在我的主机上,端口9000已被hyper-v服务vmms.exe使用。我不知道这是否有关系 docker-compose.yml version: "3" services: nginx: build: ./nginx volumes: - ./symfony:/usr/s

我正在尝试在Windows机器上用NGINX和PHP-FPM在Docker中设置Symfony 3。目前,我得到一个502坏网关错误。我将FPM端口从9000更改为8000,因为在我的主机上,端口9000已被hyper-v服务vmms.exe使用。我不知道这是否有关系

docker-compose.yml

version: "3"

services:

  nginx:
      build: ./nginx
      volumes:
        - ./symfony:/usr/shared/nginx/html
      ports:
        - "80:80"
        - "443:443"
      environment:
        - NGINX_HOST=free-energy.org
      depends_on:
        - fpm

  fpm:
      image: php:fpm
      ports:
          - "8000:8000"
      # It seems like FPM receives the full path from NGINX
      # and tries to find the files in this dock, so it must
      # be the same as nginx.root
      volumes:
          - ./symfony:/usr/shared/nginx/html
version: "2"
services:
  nginx:
    build: "./docker/nginx/"
    container_name: "nginx-free-energy"
    volumes_from:
      - php-fpm
    restart: always
    ports:
      - "80:80"
  php-fpm:
    image: php:7.1-fpm
    container_name: "php-fpm-free-energy"
    volumes:
      - ./src/free-energy.org:/var/www/free-energy.org
    restart: always
    expose:
      - "9000"
Dockerfile NGINX:

FROM nginx:1.13.7-alpine

# Change Nginx config here...
RUN rm /etc/nginx/conf.d/default.conf
ADD ./default.conf /etc/nginx/conf.d/

EXPOSE 80
EXPOSE 443
default.conf覆盖NGINX:

server {
    listen  80;
    server_name free-energy.org;

    # this path MUST be exactly as docker-compose.fpm.volumes,
    # even if it doesn't exist in this dock.
    root /usr/share/nginx/html;

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

    location ~ ^/.+\.php(/|$) {
        fastcgi_pass fpm:8000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

您能检查一下php fpm容器吗?我记得,php fpm默认端口是9000,而不是8000。将容器内部的端口映射从8000更改为9000

或者,如果它已在主机上使用,则只能在容器之间公开端口


回答为0tshell\u n1ck时,502错误得到解决。我仍然在浏览器中找到“未找到文件”,在NGINX“从上游读取响应头时在stderr中发送的FastCGI:Primary script未知”中找到相关错误日志

以下是最终证明有效的配置。需要改变的关键事项:

NGINX配置文件中的root指令不包含Symfony项目中所需的/public,因为front controller index.php位于其中 我在docker-compose.yml中更改了卷目的地,使Symfony项目目录现在映射到/var/www/Symfony,而不是/usr/shared/nginx/html。我不确定为什么以前的目录不起作用 这些要点是在使用位于

这是有效的代码

docker-compose.yml:

version: "3"

services:

  nginx:
      build: ./nginx
      volumes:
        - ./symfony:/var/www/symfony

      ports:
        - "80:80"
        - "443:443"
      depends_on:
        - fpm

  fpm:
      image: php:fpm
      ports:
          - "9000"
      volumes:
          - ./symfony:/var/www/symfony
NGINX配置default.conf:

server {
    listen  80;
    server_name free-energy.org, www.free-energy.org;
    root /var/www/symfony/public;

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/index.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

我们将在一周内猜测,尝试使用以下方法: /docker-compose.yml

version: "3"

services:

  nginx:
      build: ./nginx
      volumes:
        - ./symfony:/usr/shared/nginx/html
      ports:
        - "80:80"
        - "443:443"
      environment:
        - NGINX_HOST=free-energy.org
      depends_on:
        - fpm

  fpm:
      image: php:fpm
      ports:
          - "8000:8000"
      # It seems like FPM receives the full path from NGINX
      # and tries to find the files in this dock, so it must
      # be the same as nginx.root
      volumes:
          - ./symfony:/usr/shared/nginx/html
version: "2"
services:
  nginx:
    build: "./docker/nginx/"
    container_name: "nginx-free-energy"
    volumes_from:
      - php-fpm
    restart: always
    ports:
      - "80:80"
  php-fpm:
    image: php:7.1-fpm
    container_name: "php-fpm-free-energy"
    volumes:
      - ./src/free-energy.org:/var/www/free-energy.org
    restart: always
    expose:
      - "9000"
/docker/nginx/Dockerfile

FROM nginx

COPY config/free-energy.conf /etc/nginx/conf.d/free-energy.conf

RUN rm /etc/nginx/conf.d/default.conf

RUN usermod -u 1000 www-data

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
/symfony3应用程序的docker/nginx/config/free-energy.conf

server {
    listen      80;
    server_name www.free-energy.org free-energy.org;
    root /var/www/free-energy.org/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }

    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_pass php-fpm-free-energy:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/free-energy.org_error.log;
    access_log /var/log/nginx/free-energy.org_access.log;
}

启动后,您需要将127.0.0.1 free-energy.org添加到您的/etc/hosts文件以解析主机,您的应用程序将位于/src/free-energy.org入口点为-/src/free-energy.org/web/app.php

谢谢,我将其改回9000。现在我得到了'FastCGI sent in stderr:“Primary script unknown'。如果您公开了端口,您是否更改了FastCGI_pass fpm:8000;fastcgi_通过fpm:9000;在您的NGINX默认配置中?也许你忘了重建图像?我知道这一点,并已改为fastcgi_pass fpm:9000,并使用docker compose up-build