大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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 di…

问题原文

原帖链接 · pileup · CC BY-SA 4.0

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;
    }
    
}

已采纳解答

原帖链接 · justinas · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-08-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。