Score:0

PHP files downloading when using php-fpm and nginx with Docker

in flag

I already saw other posts here about a similar issue, but I think mine needs a different solution because the other posts didn't help.

I am running two separate containers with Docker: For php-fpm and for nginx. When I try to display text using index.html file it's all ok, but when I change the file name to index.php it downloads a "download" file instead.

I'm not sure if it's only because of my default.conf file or because of how I setup my containers.

This is my docker-compose.yml:

networks:
    laravel:


services:
    nginx:
        image: nginx:stable-alpine
        container_name: nginx
        ports:
            - "8088:80"
        volumes:
            - ./src:/var/www/html 
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
        
        depends_on:
            - php
            - mysql
            
        networks:
            - laravel
        
    
    mysql:
        image: mysql
        container_name: mysql

        tty: true
        ports:
            - "4306:3306"
        volumes:
            - ./src:/var/lib/mysql
        environment:
            MYSQL_DATABASE: laravel
            MYSQL_ROOT_USER: root
            MYSQL_ROOT_PASSWORD: secret
            SERVICE_TAGS: dev
            SERVICE_NAME: mysql            
        
        networks:
            - laravel
          
    
    php:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: php
        volumes:
            - ./src:/var/www/html
        ports:
            - "9000:9000"
        networks:
            - laravel
       

This is my default.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 /var/www/html/public;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    
}
Score:1
cn flag

You seem to be missing the main part: the fastcgi_pass directive to actually forward the request to FPM. Add this under the location ~ \.php$ section:

fastcgi_pass php:9000;

See also the example in Nginx documentation.

Stackerito avatar
in flag
Thank you! it worked
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.