How to correctly link php-fpm and Nginx Docker containers?

后端 未结 7 999
天涯浪人
天涯浪人 2020-12-02 04:20

I am trying to link 2 separate containers:

  • nginx:latest
  • php:fpm

The problem is that php scripts do not work. Perhaps the php-fpm config

7条回答
  •  孤街浪徒
    2020-12-02 04:42

    I know it is kind an old post, but I've had the same problem and couldn't understand why your code didn't work. After a LOT of tests I've found out why.

    It seems like fpm receives the full path from nginx and tries to find the files in the fpm container, so it must be the exactly the same as server.root in the nginx config, even if it doesn't exist in the nginx container.

    To demonstrate:

    docker-compose.yml

    nginx:
        build: .
        ports:
            - "80:80"
        links:
            - fpm
    fpm:
        image: php:fpm
        ports:
            - ":9000"
    
        # seems like fpm receives the full path from nginx
        # and tries to find the files in this dock, so it must
        # be the same as nginx.root
        volumes:
            - ./:/complex/path/to/files/
    

    /etc/nginx/conf.d/default.conf

    server {
        listen  80;
    
        # this path MUST be exactly as docker-compose.fpm.volumes,
        # even if it doesn't exist in this dock.
        root /complex/path/to/files;
    
        location / {
            try_files $uri /index.php$is_args$args;
        }
    
        location ~ ^/.+\.php(/|$) {
            fastcgi_pass fpm:9000;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
    

    Dockerfile

    FROM nginx:latest
    COPY ./default.conf /etc/nginx/conf.d/
    

提交回复
热议问题