1

Nginx is running on my server (not a docker image) to proxy the subdomain requests to my docker containers.

Having an additional nginx container for each image works well as follows:

docker-compose.yml

version: '2'

services:
    nginx:
        image: nginx:latest
        ports:
            - "8080:80"
        volumes:
            - .:/app
            - ./site.conf:/etc/nginx/conf.d/default.conf
        networks:
            - code-network
    php:
        image: php:fpm
        volumes:
            - .:/app
        networks:
            - code-network

networks:
    code-network:
        driver: bridge

site.conf

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /app;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

/etc/nginx/sites-available/dev.domain.co.uk

server {
    listen 80;
    listen [::]:80;
    server_name dev.domain.co.uk;
    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

But seems wasteful to have an additional instance of nginx for each php site.

How can I route the server instance of nginx directly to each php:fpm container?

note: the docker 'code-network' gets renamed appname-code-network upon docker-compose up -d

2 Answers 2

4

To proxy to multiple php-fpm docker instances, you need to give each php instance a unique alias which then should be referenced in your server configuration.

For example:

in docker-compose.yml file (see php1 in 'aliases' section):

php:
  image: php:fpm
  volumes:
      - .:/app
  networks:
    code-network:
      aliases:
        - php1

and then in site.conf use fastcgi_pass php1:9000; See php1?

Giving each php container instance a unique alias allows you to run different php setup (versions, etc...) on the same host with a single nginx container.

Hope it makes sense.

Sign up to request clarification or add additional context in comments.

1 Comment

2 1/2 years later, this reply saved me from hours of hair pulling. Thank you @Alexander Benjamin
2

Figured this out.

In the nginx server conf, the root is the location to the code on the server host i.e. /home/user/CODE/site

the fastcgi_param is the location of the code on the Docker container, (/app) as defined in docker-compose.yml

fastcgi_param SCRIPT_FILENAME /app$fastcgi_script_name;

You also need to pass to the docker container host ip:

fastcgi_pass 172.21.0.2:9000;

As discovered by hostname -I, when using bash on the container.

I'm yet to discover how to use the container hostname instead

1 Comment

Hi. Thanks for sharing the solution. Your comment regarding fastcgi_param helped me a lot. I think I am also able to replay you :) In docker-compose use container_name: your-name. You can then use this instead of IP for fastcgi_pass. Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.