Docker Community Forums

Share and learn in the Docker community.

Nginx, PHP-FPM : Problems with connection

(Tristan Pouliquen) #1


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:

  image: memcached:1.4.25
  container_name: elinoi-memcached

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

  image: redis:3.0.7
  container_name: elinoi-redis

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

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

  build: .
  dockerfile: docker/Dockerfile.php-fpm.conf
  container_name: elinoi-php-fpm
    - .:/var/www/
    - /var/docker_volumes/
   - "22001:22"
    - 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/"

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* optional' -i /etc/pam.d/sshd

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

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

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

WORKDIR "/var/www/"

My nginx conf file is as follows:

server {
    listen 80 default;

    root /var/www/;

    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=

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


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