Docker Community Forums

Share and learn in the Docker community.

Nginx, PHP-FPM : Problems with connection


(Tristan Pouliquen) #1

Hello,

I have been trying to take over a project that has been set up with docker and docker-compose but I don’t seem to get it to work, getting a 502 Bad Gateway from Nginx after having run docker-compose up that works fine.

I have run docker-compose elinoi-webserver ping -c3 elinoi-php-fpm to try and test the link between my two images and it seems to be working fine (no packet loss).

Could anyone help me in understanding what is going wrong in my configuration? Of all the tutorials or posts I have seen, it seems to me as if my config should actually work :confused:

I’d be extra greatful! :smile:

I’m running it on Ubuntu 16.04, with Docker v17.12.1-ce and Docker Compose v1.19.0.

Here is my docker-compose.yml:

elinoi-memcached:
  image: memcached:1.4.25
  container_name: elinoi-memcached

elinoi-mailhog:
  image: diyan/mailhog
  container_name: elinoi-mailhog
  ports:
    - "2001:8025"

elinoi-redis:
  image: redis:3.0.7
  container_name: elinoi-redis

elinoi-mysql:
  image: mysql:latest
  container_name: elinoi-mysql
  environment:
    - MYSQL_ROOT_PASSWORD=root-password
    - MYSQL_DATABASE=elinoi
    - MYSQL_USER=elinoi
    - MYSQL_PASSWORD=jJQLVPy0sH9rOmqGQvobZ336IwTy86hjpaGwCv1p
  ports:
    - "33061:3306"

elinoi-webserver:
  build: .
  dockerfile: docker/Dockerfile.nginx.conf
  container_name: elinoi-webserver
  volumes:
      - .:/var/www/elinoi.com
  ports:
   - "2000:80"
  links:
   - elinoi-php-fpm

elinoi-php-fpm:
  build: .
  dockerfile: docker/Dockerfile.php-fpm.conf
  container_name: elinoi-php-fpm
  volumes:
    - .:/var/www/elinoi.com
    - /var/docker_volumes/elinoi.com/shared:/var/www/elinoi.com/shared
  ports:
   - "22001:22"
  links:
    - elinoi-mailhog
    - elinoi-memcached
    - elinoi-mysql
    - elinoi-redis 

And my two custom Dockerfiles for my webserver (with nginx:latest) and my php-fpm:

FROM smebberson/alpine-nginx:latest

COPY /docker/nginx.conf /etc/nginx/conf.d/default.conf

WORKDIR "/var/www/elinoi.com"

For php-fpm image:

FROM phpdockerio/php71-fpm:latest
# Install selected extensions
RUN apt-get update \
    && apt-get -y --no-install-recommends install php7.1-memcached php7.1-mysql php7.1-redis php7.1-gd php7.1-imagick php7.1-intl php7.1-xdebug php7.1-mbstring \
    && apt-get -y --no-install-recommends install nodejs npm nodejs-legacy vim ruby-full git build-essential libffi-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN npm install -g bower
RUN npm install -g less
RUN gem install sass

# If you're using symfony and the vagranted environment, I strongly recommend you change your AppKernel to use the following temporary folders
# for cache, logs and sessions, otherwise application performance may suffer due to these being shared over NFS back to the host
RUN mkdir -p "/tmp/elinoi/cache" \
    && mkdir -p "/tmp/elinoi/logs" \
    && mkdir -p "/tmp/elinoi/sessions" \
    && chown www-data:www-data -R "/tmp/elinoi"

RUN apt-get update \
    && apt-get -y --no-install-recommends install openssh-server \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22
ADD docker/.ssh /root/.ssh
RUN chmod 700 /root/.ssh/authorized_keys

CMD ["/usr/sbin/sshd", "-D"]

WORKDIR "/var/www/elinoi.com"

My nginx conf file is as follows:

server {
    listen 80 default;

    root /var/www/elinoi.com/current/web;

    rewrite ^/app\.php/?(.*)$ /$1 permanent;

    try_files $uri @rewriteapp;

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    # Deny all . files
    location ~ /\. {
        deny all;
    }

    location ~ ^/(app|app_dev)\.php(/|$) {
        fastcgi_pass elinoi-php-fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index app.php;
    }

    # Statics
        location /(bundles|media) {
        access_log off;
        expires 30d;
        try_files $uri @rewriteapp;
    }

}

(Karthik Bhat) #2

I think the problem is with your php-fpm conf. You should expose 9000 port because /etc/php7/fpm/pool.d/www.conf file of your php service will have listen=127.0.0.1:9000

Second thing I noticed is that the index key is missing in nginx configuration

server {
    listen 80 default;

    root /path/to/root;
    index app.php;
}

Finally, login to the running container once docker-compose up -d is done using docker exec -it containerId sh and check the nginx error and access logs

OR

just do docker-compose up and you will see the logs on your screen