1
votes

I am trying to setup php7.1, nginx and laravel 5.5. I am getting an error 502 connecting the server.

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

On docker-compose.yml up, my laravel server get start on localhost:8080, webserver is also getting started. But in web browser it's giving bad gateway exception:

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" –

1
Can you post the exact log of the exception, please?regina_fallangi
This is the exact error I am getting: 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" .Viraj Kaulkar
This: volumes: - /var/www should be something else. You must map your local directory to something on the server. I would change that first before going forward...UnderDog

1 Answers

0
votes

First of, are you running standard docker or docker swarm? The version 3.x formats are for docker swarm.
I would not suspect that is the issue with starting up and connecting to the FPM container though.

I can't seem to find the virajkaulkar/laravel-app image on docker hub, so I suspect that this is a local or private repository image(?), in that case, you should first of confirm that it is actually using PHP-FPM and not some other version of php.

As it seems to be an issue in the connection with the fpm server, make sure that the container is running as it is supposed to and that it did not fail in the start up phase, if it's fine, I would start with trying to remove some of the fastcgi parameters and see if any of them creates issues in the connection.

For example, you have two SCRIPT_FILENAME parameters in the ~\.php$ scope, start with removing one of them (the one that does not match the fpm containers index file), until everything works as you wish it to, remove the optimization parameters and re-add them when it does.

   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;

If this does not help, I would recommend testing with another FPM container just to make sure that it actually works, bind the local directory (the one with the project) to the docker container and see if you can reach it, and if you can, it's likely that the container have some issues.