Docker + Nginx + PHP-FPM error: [emerg] 1#1: host not found in upstream

25,310

nginx fails to resolve the hostname php-fpm and therefore refuses to start.

There is a simple workaround which - in this case - results in a 502 - Bad Gateway until nginx is able to resolve the upstream's hostname: Put the upstream address into a variable!

Further you should manually point nginx to docker's internal DNS with the resolver option. The docker internal DNS server is always at 127.0.0.11 as to be found in the documentation.

    resolver 127.0.0.11;
    set $upstream php-fpm:9000;
    # nginx will now start if host is not reachable
    fastcgi_pass    $upstream; 
Share:
25,310

Related videos on Youtube

samayo
Author by

samayo

Fullstack Developer living in CH Github Projects: A PHP secure image uploader Country data to JSON Fast PHP blogging app for developers

Updated on September 18, 2022

Comments

  • samayo
    samayo over 1 year

    I have a docker setup for LEMP stack that I cloned from this repo.

    Everything works nicely on my development machine running window 10, but when I push the image to docker hub and pull it on my VPS no matter what I do I always get this error:

    [emerg] 1#1: host not found in upstream "php-fpm:9000" in /etc/nginx/conf.d/upstream.conf:1
    

    This error is coming from two files.

    First: From this Nginx Docker file

    Here is the code:

    RUN echo "upstream php-upstream { server ${PHP_UPSTREAM_CONTAINER}:${PHP_UPSTREAM_PORT}; }" > /etc/nginx/conf.d/upstream.conf \
    && rm /etc/nginx/conf.d/default.conf
    

    Second From this Nginx default.conf file

    Here is the code:

        location ~ \.php$ {
          ...
          fastcgi_pass php-upstream;
          ...
        }
    

    I say these two files are the cause b/c there is no reference of php-upstream elsewhere.

    I have tried every possible combination of adding/removing hosts, adding depends_on, changing nginx, php version, disabling selinux but it just doesn't work. I always get the same error on production, but on local server everything works.

    • Jon Buys
      Jon Buys over 5 years
      So, that error looks to me like it's trying to resolve what "php-fpm:9000" is, and failing. This line sets the variable, is there something different on your local machine than what's in production? Are you deploying using the same docker-compose.yml file? From what I can tell it looks like you need to have the php-fpm container running before you can run the nginx container.
    • samayo
      samayo over 5 years
      The machine is just a simple centos7 vps, nothing installed except yum update ..how do I maka php-fpm run after the nginx container?
    • Nicolai Fröhlich
      Nicolai Fröhlich over 5 years
      If you're using docker-compose add depends_on: [php-fpm] to the configuration of the nginx service. This will start nginx after php-fpm. Take a look at the documentation
    • Jyosua
      Jyosua almost 5 years
      Did you ever figure this out? I have run into this same issue. Docker compose and my nginx config works fine on Windows, but not when I try to use the exact same things on my Mac. I have noticed that, for me, the issue only started when I switched from doing image: nginx:latest in the yml file to using build: context: .nginx and having a dockerfile in the my nginx folder.
    • samayo
      samayo almost 5 years
      oh man, yes i figured it out but I forgot :) I remember spending days but somehow either i found out by chance or someone told me and I can't remember. But I have a docker-compose for lemp stack which i made after i resolved the issue and now I just pull and run compose and it works. it has php-fpm, nginx, mariadb ... let me know if interested.
    • Jyosua
      Jyosua almost 5 years
      Yeah, if you don't mind dropping me a link to it, I would greatly appreciate it!!!
    • samayo
      samayo almost 5 years
      Also I remember the error was related to a network. A docker network must be created to avoid the upstream error, let me know if my repo doesn't work or if it works
    • Jyosua
      Jyosua almost 5 years
      Yeah, in my case, I already had a network defined and it worked on windows, but not on mac, which is why it was weird. On my mac, it also worked when I used image instead of build, too. I'll check out your repo tonight and see if it works for me on my mac.
  • samayo
    samayo over 5 years
    thanks, but this does not work. I spent 3 hours, the only difference is now my app doesn't even run on my local machine. I get 502 - Bad Gateway on local (which used to work) but I get the same issue on production as well, so this answer actually was a regression
  • Nicolai Fröhlich
    Nicolai Fröhlich over 5 years
    what happens if you ommit the resolver line? Does it still not work? I am 100% sure that the "trick" to put the upstream address into a variable works very well with any container setup. While your problem of the nginx container not being able to communicate with the php-fpm container might have a different cause it's still a good practice to include this trick in order to prevent a non-starting nginx container i.e. upon system restart.
  • samayo
    samayo over 5 years
    I actually removed images, and now I am only using docker-compose to install the libraries, and I upload the compose file in my prod machine to install the same libs, so I can have the same environment in both dev/prod machines, because the whole image create, push, pull does not work. I hired someone on upwork.com and he said the issue was with the network, I run docker create new network my-network e.t.c... and it partially work but with more problem. So, the issue I am sure is with docker network and not php, nginx upstream
  • rémy
    rémy over 4 years
    Adding also proxy_pass $upstream; solved the problem for me.