Windows 和 Symfony 4 的 Docker 中的 NGINX 502 错误网关错误

NGINX 502 bad gateway error in Docker for Windows and Symfony 4

我正在尝试在装有 NGINX 和 PHP-FPM 的 Windows 机器上的 Docker 中设置 Symfony 3。目前,我收到 502 bad gateway 错误。我将 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

Docker文件 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

  ports:
      - "8000:9000"

或者如果它已经在您的主机上使用,您可以仅在容器之间公开端口。

  expose:
      - "9000"

随着 0TshEL_n1ck 的回答,502 错误已解决。我仍然在浏览器中得到 'File not found' 并且在 NGINX 中得到相关的错误日志 'FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream'。

这里是证明有效的配置。需要更改的关键事项:

  • NGINX 配置文件中的根指令不包含 Symfony 项目中需要的 /public 因为前端控制器 index.php 位于那里
  • 我在 docker-compose.yml 中更改了卷目标,这样 Symfony 项目目录现在映射到 /var/www/symfony 而不是 /usr/shared/nginx/html (不确定为什么以前没有没用)

这些要点是在使用位于 https://symfony.com/doc/current/setup/web_server_configuration.html

的 Symfony 文档中的 NGINX 配置后发现的

这是有效的代码。

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: "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;"]

/docker/nginx/config/free-energy.conf(对于 symfony3 应用)

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;
}

启动后,您需要在 /etc/hosts 文件中添加 127.0.0.1 free-energy.org 以解析主机,您的应用程序将在 /src/free-energy.org 上运行(入口点是 - /src/free-energy.org/web/app.php)