nginx-proxy: fastcgi does not work

I am unable to get this to work via fastcgi with a simple php-fpm container. Here’s my docker-compose.yml file:

version: "3"

services:
  proxy:
    image: jwilder/nginx-proxy
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    environment:
      - DEFAULT_HOST=test.local

  fpm:
    image: php:fpm
    environment:
      - VIRTUAL_HOST=test.local
      - VIRTUAL_PROTO=fastcgi

I then drop in a simple index.php file by running:

 docker container exec -it web_fpm_1 /bin/bash -c 'echo "<?php phpinfo(); ?>" > /var/www/html/index.php'

(It puts web_ in front because this project is in a directory named web/.)

I also modify my hosts file to point test.local to 127.0.0.1, so I can test it. However, every attempt to browse to test.local results in a blank white page.

The logs for the web_fpm_1 container show that nothing gets sent except a 200 response:

[06-Jul-2020 15:40:39] NOTICE: fpm is running, pid 1
[06-Jul-2020 15:40:39] NOTICE: ready to handle connections
172.19.0.3 -  06/Jul/2020:15:41:02 +0000 "- " 200
172.19.0.3 -  06/Jul/2020:15:41:03 +0000 "- " 200

What am I doing wrong?

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 19 (5 by maintainers)

Most upvoted comments

Based on the example by @flowl I have implemented https://github.com/tkw1536/nginx-proxy-fpm-workaround. This solution does not require manually intervening in the nginx-proxy container. It additionally serves non-php files statically.

@tkw1536 in the config is no try_files $uri $uri/ /index.php and in fastcgi_params or the default.conf there is no fastcgi_param SCRIPT_FILENAME $request_filename;

I’m not sure if this would be a solution, but if there was somehow a way to implement additional VIRTUAL_ variables in the services being proxied for a location.conf and static.conf so nginx would see these variables on a new container and add them to /vhost.d/. I’m basing this on @fowl / @tkw1536 fix. The issue is though this workaround is “working” we need a way to instead of defining the static and location conf in nginx-proxy container, instead define it in the service-being-proxied’s container, so when a new container is “seen” by nginx-proxy it automagically adds location / static confs. Not sure if this is possible.

How it’s currently done via @tkw1536 workaround via volume mounts in nginx-proxy container:

version: '3'
services:
   nginx:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    volumes:
        - /var/run/docker.sock:/tmp/docker.sock:ro
        - ./fpm_workaround_location.conf:/etc/nginx/vhost.d/example.com_location:ro
        - ./fpm_workaround_static.conf:/etc/nginx/vhost.d/example.com:ro

   php:
    image:  php:fpm
    environment:
      VIRTUAL_HOST: example.com
      VIRTUAL_PROTO: fastcgi
      VIRTUAL_PORT: 9000
      VIRTUAL_ROOT: /var/www/html
    volumes:
      - ./html:/var/www/html

Proposed workaround in the actual proxied service via environment variables:

php:
    image:  php:fpm
    environment:
      VIRTUAL_HOST: example.com
      VIRTUAL_PROTO: fastcgi
      VIRTUAL_PORT: 9000
      VIRTUAL_ROOT: /var/www/html

      # Proposed variables to add to nginx-proxy:
      VIRTUAL_LOCATION_CONF: ./example.com_location.conf
      VIRTUAL_STATIC_CONF: ./example.com_static.conf

    volumes:
      - ./html:/var/www/html

fpm_workaround_location.conf / example.com_location.conf :

fastcgi_param SCRIPT_FILENAME $request_filename;

fpm_workaround_static.conf / example.com.conf :

# match anything that doesn't end in .php
location ~ .*(?<!\.php)$ {
    root /var/www/$server_name;
    try_files $uri =404;
}

Again, not sure if this is even possible, but it seems like it might be a fix if one of the devs could get it working.@buchdag

I just searched through the commits and there is some that actually revert the change https://github.com/nginx-proxy/nginx-proxy/commits?author=qiqizjl but there is not much context about why so it is difficult to find the reason behind those commits

I opened a MR to fix this https://github.com/nginx-proxy/nginx-proxy/pull/2011

Basically the old jwilder image used to include SCRIPT_FILENAME in the fastcgi_params file so fastcgi worked. Luckily we can just include the more complete fastcgi.conf that sets the SCRIPT_FILENAME variable. More details about the differences. https://github.com/nginxinc/docker-nginx/issues/298#issuecomment-981521505

I guess at some point upstream nginx decided not to include that variable in fastcgi_params for the user to set it manually and have fastcgi.conf for a more complete and out-of-the-box config.

The examples still suggest that SCRIPT_FILENAME is included https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/