GoogleCloudPlatform / buildpacks

Builders and buildpacks designed to run on Google Cloud's container platforms
Apache License 2.0
985 stars 144 forks source link

PHP/GAE Buildpacks cannot serve static files #234

Open vbarbarosh opened 2 years ago

vbarbarosh commented 2 years ago

It turns out that all requests are passed to the front controller. What about static files? I found no way to instruct nginx to serve them. Seems https://github.com/GoogleCloudPlatform/buildpacks/blob/main/pkg/nginx/nginx.go is responsible for making nginx.conf file.

This is how I built my app for GAE:

$ cat run.Dockerfile
FROM gcr.io/gae-runtimes/buildpacks/php74/run
USER root

        # Install phpredis
        RUN apt-get update
        RUN apt-get install -y autoconf make g++
        RUN echo | pecl install redis
        RUN echo 'extension=redis.so' >> /etc/php.ini
        RUN php -r 'echo "PHPREDIS: ", phpversion("redis"), "\n";'

USER www-data

$ docker build -t my-run-image -f run.Dockerfile .

$ pack build hello-app --builder=gcr.io/gae-runtimes/buildpacks/php74/builder:latest --run-image=my-run-image --env=X_GOOGLE_TARGET_PLATFORM=gae 
vbarbarosh commented 2 years ago

Current solution:

<?php

$file = __DIR__ . $_SERVER['PATH_INFO'];
if (is_file($file)) {
    switch (preg_replace('/.*?([^.]+)$/', '$1', basename($file))) {
    case 'jpg': $mime = 'image/jpeg'; break;
    case 'jpeg': $mime = 'image/jpeg'; break;
    case 'png': $mime = 'image/png'; break;
    case 'svg': $mime = 'image/svg+xml'; break;
    case 'gif': $mime = 'image/gif'; break;
    case 'html': $mime = 'text/html'; break;
    case 'md': $mime = 'text/plain'; break;
    case 'css': $mime = 'text/css'; break;
    case 'xml': $mime = 'text/xml'; break;
    case 'js': $mime = 'text/javascript'; break;
    case 'json': $mime = 'application/json'; break;
    default: $mime = 'application/octet-stream'; break;
    }
    header("Content-Type: $mime");
    header('Content-Length: ' . filesize($file));
    flush();
    readfile($file);
    exit;
}

require_once __DIR__ . '/index-laravel.php';