0

TL;DR why this wordpress:latest Dockerfile is ok with nginx-proxy but this one not wordpress:fpm Dockerfile ? And how can I use a wordpress:fpm image with nginx-proxy

Hi,

I successfully use nginx-proxy with some wordpress container. for example this docker-compose.yml works perfectly :

db:
  image: mariadb
  environment:
    - MYSQL_ROOT_PASSWORD=password
  volumes:
    - /home/stack/my_domain/bdd:/var/lib/mysql

wordpress:
  image: wordpress
  links:
    - db:mysql
  environment:
    - VIRTUAL_HOST=my_domain.fr,www.my_domain.fr
    - LETSENCRYPT_HOST=www.my_domain.fr
    - LETSENCRYPT_EMAIL=contact@my_domain.fr
  env_file:
    - ./env

  volumes:
    - /home/stack/my_domain/wordpress:/var/www/html

BUT if I use the wordpress:fpm image (instead of an apache based image) I have 502 Bad Gateway error, and this message in the log:

nginx.1 | 2017/08/14 21:29:51 [error] 347#347: *2447 connect() failed (111: Connection refused) while connecting to upstream, client: 86.222.20.31, server: www.my_domain.fr, request: "GET /contact/ HTTP/2.0", upstream: "http://172.17.0.14:80/contact/", host: "www.my_domain.fr", referrer: "https://www.my_domain.fr/"

and this message :

root@9408854fae4b:/etc/nginx/conf.d# nginx -s reload 2017/08/14 21:37:35 [emerg] 671#671: invalid number of arguments in "upstream" directive in /etc/nginx/conf.d/default.conf:53 nginx: [emerg] invalid number of arguments in "upstream" directive in /etc/nginx/conf.d/default.conf:53

the default.conf at line 53 contains

upstream mydomain.fr {
                                ## Can be connect with "bridge" network
                        # my_domain_wordpress_1
                        server 172.17.0.14:9000;
}

other domain have server 172.17.0.xx:80; so I add port:80 and/or expose:80 in the docker-compose.yml file. I manage to obtain

upstream mydomain.fr {
                                ## Can be connect with "bridge" network
                        # my_domain_wordpress_1
                        server 172.17.0.14:80;
}

but with the same 502 error.

Any idea why ?

Regards

Vincent Guyader
  • 2,927
  • 1
  • 26
  • 43

1 Answers1

0

The reason is that both of the images are different in their working.

The wordpress:latest uses apache on port 80 and responds to requests with the proper PHP script executed. It handles the HTTP protocol.

On the other hand wordpress:fpm uses PHP-FPM, which is a Fast CGI server, it is not expecting just proxy_pass but other nginx parameters like below

     location ~ \.php$ {
     try_files $uri =404;
           fastcgi_pass http://fpm:9000;
           fastcgi_index index.php;
           include /etc/nginx/fastcgi_params;
     }

nginx-proxy image just checks which containers are launched with a VIRTUAL_HOST environment variables, check its exposed ports and then just creates a proxy pass template. What you need is a different template for this. This can be done using a per-host configuration.

Per-VIRTUAL_HOST

To add settings on a per-VIRTUAL_HOST basis, add your configuration file under /etc/nginx/vhost.d. Unlike in the proxy-wide case, which allows multiple config files with any name ending in .conf, the per-VIRTUAL_HOST file must be named exactly after the VIRTUAL_HOST.

In order to allow virtual hosts to be dynamically configured as backends are added and removed, it makes the most sense to mount an external directory as /etc/nginx/vhost.d as opposed to using derived images or mounting individual configuration files.

For example, if you have a virtual host named app.example.com, you could provide a custom configuration for that host as follows:

$ docker run -d -p 80:80 -p 443:443 -v /path/to/vhost.d:/etc/nginx/vhost.d:ro -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy $ { echo 'server_tokens off;'; echo 'client_max_body_size 100m;'; } > /path/to/vhost.d/app.example.com

Follow the below URL for more details

https://github.com/jwilder/nginx-proxy#per-virtual_host

Community
  • 1
  • 1
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265