简体   繁体   English

502错误的网关Docker + Laravel

[英]502 Bad Gateway Docker + Laravel

I've started to learn Docker. 我已经开始学习Docker。 I have Laravel application and want to start it in docker containers. 我有Laravel应用程序,想在Docker容器中启动它。 But I can't run it correctly. 但是我无法正确运行它。

I have this docker-compose.yml : 我有这个docker-compose.yml

version: '2'
services:

  # The Application
  app:
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ./:/var/www
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"

  # The Web Server
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes_from:
      - app
    ports:
      - 8080:80

  # The Database
  database:
    image: mysql:5.6
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      - "MYSQL_DATABASE=homestead"
      - "MYSQL_USER=homestead"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"
    ports:
        - "33061:3306"

volumes:
  dbdata:

And this vhost.conf : 和这个vhost.conf

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

app.dockerfile: app.dockerfile:

FROM php:7.1.3-fpm

RUN apt-get update && apt-get install -y \
    libmagickwand-dev --no-install-recommends \
    libmcrypt-dev \
    cron \
    mysql-client \
    vim \
    && docker-php-ext-install mcrypt pdo_mysql

web.dockerfile: web.dockerfile:

FROM nginx:1.10

ADD vhost.conf /etc/nginx/conf.d/default.conf

After run containers I have 502 Bad Gateway and in web container I have this: connect() failed (111: Connections refused) while connecting to upstream, client: 192.168.99.1, server: request "GET /HTTP/1.1", upstream: "fastcgi://172.19.0.3:9000", host: "192.168.99.101:8080" . 运行容器后,我有502 Bad Gateway并且在Web容器中,我有以下问题: connect() failed (111: Connections refused) while connecting to upstream, client: 192.168.99.1, server: request "GET /HTTP/1.1", upstream: "fastcgi://172.19.0.3:9000", host: "192.168.99.101:8080"

I very bad at server settings and don't understand what happened. 我对服务器设置非常不好,也不了解发生了什么。 Will be very grateful for every help. 非常感谢您的帮助。

This is probably caused by php listening on localhost and does not accept external connections. 这可能是由于php在localhost上侦听引起的,并且不接受外部连接。 You should update you php.ini to listen on 0.0.0.0. 您应该更新您的php.ini以收听0.0.0.0。

you could test this by running the following command on your app container. 您可以通过在应用容器上运行以下命令来进行测试。 And try to telnet from the web container to the app container. 并尝试从Web容器远程登录到应用容器。

/usr/bin/php -S 0.0.0.0:9000

telnet app 9000

If, for some reason that command fails try to edit your php-fpm config to set the listen address to 0.0.0.0:9000 如果由于某种原因该命令失败,请尝试编辑您的php-fpm配置以将侦听地址设置为0.0.0.0:9000

Example: 例:

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.

listen = 0.0.0.0:9000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM