H
H
HellWalk2019-01-23 11:18:53
Nginx
HellWalk, 2019-01-23 11:18:53

How to point nginx to php-fpm on another docker?

Made two simple dockers:

version: '3.3'

services:

    nginx:
        image: nginx:latest
        ports:
            - "6080:80"
            - "543:443"
        volumes:
            - ./nginx/core:/etc/nginx/conf.d
            - ./nginx/www:/var/www/
            - ./nginx/logs:/var/log/nginx/
            - ./nginx/html:/usr/share/nginx/html/
        links:
            - php

    php:
        build: ./php
        container_name: php-fpm
        volumes:
            - ./nginx/www:/var/www

nginx settings:
server {
    listen       80;
    server_name  localhost;

    access_log /var/log/nginx/default.log;
    error_log /var/log/nginx/default-error.log;

    location / {
        root   /var/www/site.loc;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }
}

If you put an html file in the directory with the local domain, it is displayed normally, if .php is "502 Bad Gateway nginx/1.15.8"
And in the nginx logs:
2019/01/23 07:57:21 [error] 9#9: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.24.0.1, server: localhost, request: "GET / HTTP /1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:6080"

The error, as far as I understand, is that nginx does not see php-fmp, which is located in another docker. How to properly set this up? docker ps
:
CONTAINER ID        IMAGE                      COMMAND                  CREATED              STATUS              PORTS                                                    NAMES
77071ee4b780        nanoninja/php-fpm:latest   "docker-php-entrypoi…"   About a minute ago   Up About a minute   9000/tcp                                                 docker_test3_php_1
dbe3d92b037f        nginx:latest               "nginx -g 'daemon of…"   39 minutes ago       Up 21 minutes       0.0.0.0:6080->80/tcp, 0.0.0.0:543->443/tcp               docker_test2_nginx_1

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
ivankomolin, 2019-01-23
@HellWalk

Instead of
Specify
, your error indicates that nginx cannot find php-fpm at 127.0.0.1.
And this happens because the address 127.0.0.1 is an address inside the nginx container, and php-fpm lies outside it inside another container.
Due to the fact that docker-compose by default creates a network between the containers specified in the file, you can reach the service by its name from docker-compose.yml. In your case it is php

A
Ainur Valiev, 2019-01-23
@vaajnur

see here how to properly configure the server
phpjs.ru/2017/09/07/nginx-php-fpm-%D0%BD%D0%B0-vps...

H
hOtRush, 2019-01-23
@hOtRush

https://phptoday.ru/post/gotovim-lokalnuyu-sredu-d...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question