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

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

泊坞窗,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覆盖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错误。 浏览器中仍然有'文件未find',而NGINX'FastCGI中有一个相关的错误日志:stderr:“读取来自上游的响应头时读取主脚本”。

这是原来的configuration工作。 关键的事情需要改变:

  • NGINXconfiguration文件中的根指令不包含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文档中的NGINXconfiguration之后,

这是有效的代码。

泊坞窗,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 

NGINXconfigurationdefault.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" 

/搬运工/ 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; } 

开始之后,需要将127.0.0.1 free-energy.org添加到/ etc / hosts文件中以parsing主机,并且您的应用程序将位于/src/free-energy.org(入口点是 – / src / free-energy.org/web/app.php)