docker - 使用 php7.1、laravel 和 nginx 服务器设置 docker,出现 Bad Gateway 502 异常

标签 docker nginx docker-compose dockerfile php-7.1

我正在尝试设置 php7.1、nginx 和 laravel 5.5。我在连接服务器时收到错误 502。

docker-compose.yml

version: '3'
services:
    # The Application 
    web:
        image: virajkaulkar/laravel-web
        ports:
            - "8080:80"
        volumes:
            -  /var/www
        links:
            - php

    # The Application
    php:
      image: virajkaulkar/laravel-app
      volumes:
          - /var/www
      env_file: '.env'
      environment:
          - HOST=127.0.0.1
          - PORT=8080

vhost.conf
upstream phpserver {
    server php:9000;
}

server {
   listen 80;
   listen [::]:80 default ipv6only=on; 
   server_name  localhost;
   root /var/www/public;
   index  index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;        
    }

   #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

   location ~ \.php$ {
       fastcgi_pass phpserver;
       fastcgi_index  index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
       fastcgi_buffers 256 128k;
       fastcgi_connect_timeout 300s;
       fastcgi_send_timeout 300s;
       fastcgi_read_timeout 300s;
       include fastcgi_params;
    }
}

在 docker-compose.yml 上,我的 laravel 服务器在 localhost:8080 启动,webserver 也启动了。但在网络浏览器中,它给出了错误的网关异常:

connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://172.22.0.2:9000", host: "localhost:8080" –

最佳答案

首先,您运行的是标准 docker 还是 docker swarm?版本3.x格式适用于 docker swarm。
我不会怀疑这是启动和连接到 FPM 容器的问题。

我似乎找不到 virajkaulkar/laravel-app docker hub 上的图像,所以我怀疑这是一个本地或私有(private)存储库图像(?),在这种情况下,您应该首先确认它实际上是使用 PHP-FPM 而不是其他版本的 php。

因为这似乎是 connection 中的一个问题使用 fpm 服务器,确保容器按预期运行,并且在启动阶段没有失败,如果没问题,我会开始尝试删除一些 fastcgi 参数,看看是否有任何他们在连接中产生问题。

例如,您有两个 SCRIPT_FILENAME ~\.php$ 中的参数范围,首先删除其中一个(与 fpm 容器索引文件不匹配的那个),直到一切正常,删除优化参数并在完成时重新添加它们。

   fastcgi_pass phpserver;
   fastcgi_index  index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   # fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
   # fastcgi_buffers 256 128k;
   # fastcgi_connect_timeout 300s;
   # fastcgi_send_timeout 300s;
   # fastcgi_read_timeout 300s;
   include fastcgi_params;

如果这没有帮助,我建议使用另一个 FPM 容器进行测试,以确保它确实有效,将本地目录(包含项目的目录)绑定(bind)到 docker 容器,看看是否可以访问它,如果可以,容器可能有一些问题。

关于docker - 使用 php7.1、laravel 和 nginx 服务器设置 docker,出现 Bad Gateway 502 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52033478/

相关文章:

reactjs - 不是来自 webpack 的内容由/app/public docker 提供

docker - 通过 microk8s 获取 "x509: certificate signed by unknown authority"

docker - 如何在Docker中安装Adafruit-GPIO库

linux - Nginx 在 PHP 解析错误后发送状态 200

php - Nginx 和 PHP-FPM 502 网关错误

docker - 如何在docker堆栈中查看服务的输出?

linux - 如何在 Docker 容器上配置或更改 eth1 到 eth0

shell - 如何使用Shell脚本在dockerfile中的环境变量中设置动态值

nginx - 有没有人看到 https 突然停止工作?

docker - 将Docker设置移至文件夹