douyong1850
2017-12-25 15:03
浏览 136
已采纳

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

I'm trying to set up Symfony 3 in Docker on a Windows machine with NGINX and PHP-FPM. At the moment, I get a 502 bad gateway error. I changed the FPM port from 9000 to 8000 because on my host, port 9000 is already in use by a hyper-v service vmms.exe. I don't know if it's related.

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

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

图片转代码服务由CSDN问答提供 功能建议

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

docker-compose.yml

  version:“3”
 
services:\  n 
 nginx:
 build:./ nginx 
卷:
  -  ./symfony:/usr/shared/nginx/html 
 ports:
  - “80:80”
  - “443:443  “
环境:
  -  NGINX_HOST = free-energy.org 
 depends_on:
  -  fpm 
 
 fpm:
 image:php:fpm 
 ports:
  - ”8000:8000“
  #看起来FPM从NGINX接收完整路径
#并尝试查找此dock中的文件,因此它必须与nginx.root 
卷相同:
  -  ./symfony:/usr  / shared / nginx / html 
   
 
 

Dockerfile NGINX:

  FROM nginx:1.13.7-alpine 
  
#在这里更改Nginx配置... 
RUN rm /etc/nginx/conf.d/default.conf
ADD ./default.conf /etc/nginx/conf.d/
nnEXPOSE 80 
EXPOSE 443 \  n   
 
 

default.conf覆盖NGINX:

  server {
 listen 80; 
 server_name free-energy。  org; 
 
#this path M  UST就像docker-compose.fpm.volumes,
#,即使它在这个底座中不存在。
 root / usr / share / nginx / html; 
 
 location / {
 try_files $ uri  /index.php$is_args$args;
} 
 
位置〜^ /。+ \ .php(/ | $){
 fastcgi_pass fpm:8000; 
包括fastcgi_params; 
 fastcgi_param SCRIPT_FILENAME $ document_root $  fastcgi_script_name; 
} 
} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongxue7306 2017-12-25 17:20
    已采纳

    Could you please check php-fpm container? As I remember, the php-fpm default port is 9000, not 8000. Change port mapping from inside container from 8000 to 9000

      ports:
          - "8000:9000"
    

    Or if it already to use on your host you can expose the port only between containers.

      expose:
          - "9000"
    
    打赏 评论
  • download1002 2017-12-26 09:03

    With the answer of 0TshEL_n1ck the 502 error was solved. I still got 'File not found' in the browser and a related error log in NGINX 'FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream'.

    Here is the configuration that turned out to work. The key things that needed to change:

    • the root directive in the NGINX configuration file did not contain /public which is needed in a Symfony project because the front controller index.php is located there
    • I changed the volume destination in docker-compose.yml such that the Symfony project directory is now mapped to /var/www/symfony instead of /usr/shared/nginx/html (not sure why previous didn't work)

    These points came to light after using the NGINX configuration from the Symfony docs located at https://symfony.com/doc/current/setup/web_server_configuration.html

    Here's the code that worked.

    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 configuration 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;
    }
    
    打赏 评论
  • dorbmd1177 2017-12-26 09:16

    We will guess so a week, try to use this: /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 (for symfony3 app)

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

    After start, you need to add 127.0.0.1 free-energy.org to your /etc/hosts file for resolving the host, and your app will live on the /src/free-energy.org (entry point is - /src/free-energy.org/web/app.php)

    打赏 评论

相关推荐 更多相似问题