【问题标题】:Setting up docker with php7.1, laravel and nginx server, getting Bad Gateway 502 exception使用 php7.1、laravel 和 nginx 服务器设置 docker,出现 Bad Gateway 502 异常
【发布时间】:2019-02-01 15:32:26
【问题描述】:

我正在尝试设置 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() 在连接到上游时失败(111:连接被拒绝),客户端:172.22.0.1,服务器:localhost,请求:“GET / HTTP/1.1”,上游:“fastcgi://172.22.0.2:9000 ", 主机: "localhost:8080" –

【问题讨论】:

  • 能否请您发布异常的确切日志?
  • 这是我得到的确切错误:连接到上游时 connect() 失败(111:连接被拒绝),客户端:172.22.0.1,服务器:localhost,请求:“GET / HTTP/1.1 ", 上游: "fastcgi://172.22.0.2:9000", 主机: "localhost:8080" .
  • 这个:volumes: - /var/www 应该是别的东西。您必须将本地目录映射到服务器上的某个目录。在继续之前,我会先改变它......

标签: docker nginx docker-compose dockerfile php-7.1


【解决方案1】:

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

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

由于 fpm 服务器的connection 中似乎存在问题,请确保容器按预期运行,并且在启动阶段没有失败,如果没问题,我会首先尝试删除一些 fastcgi 参数,看看它们中的任何一个是否会在连接中产生问题。

例如,您在~\.php$ 范围内有两个SCRIPT_FILENAME 参数,首先删除其中一个(与 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 容器进行测试,以确保它确实有效,将本地目录(包含项目的目录)绑定到 docker 容器并查看是否可以访问它,然后如果可以的话,很可能是容器有问题。

【讨论】:

  • 好吧,至少,这是一部分测试,呵呵。我会做一些测试并更新答案! ;)
猜你喜欢
  • 2014-06-28
  • 1970-01-01
  • 2019-02-26
  • 2016-10-23
  • 2020-08-01
  • 2021-09-11
  • 2018-01-20
  • 2017-05-10
  • 2022-07-14
相关资源
最近更新 更多