Skip to content

Instantly share code, notes, and snippets.

@sonnysasaka
Last active September 10, 2024 09:47
Show Gist options
  • Save sonnysasaka/f3fab0c269891a3654d935dff14b0d63 to your computer and use it in GitHub Desktop.
Save sonnysasaka/f3fab0c269891a3654d935dff14b0d63 to your computer and use it in GitHub Desktop.
Allow serving static files on Google App Engine second generation PHP flex runtime
# This file is used by App Engine flex environment PHP runtime by name convention nginx-app.conf:
# https://cloud.google.com/appengine/docs/flexible/php/runtime#customize_nginx
# App Engine rewrites everything to index.php$uri, this prevents us to catch
# anything else with `location` block, so the hack is to catch it with another
# rewrite that lets us handle in our own `location ~ ^/myapp` below
# (https://github.com/GoogleCloudPlatform/buildpacks/blob/ff2ea2737a928087dcec192bf3ce103dc356ad5e/pkg/nginx/nginx.go#L93)
rewrite ^/index.php(.*)$ /myapp$1;
# Handle this our way first because we want to try serving a file if it matches,
# otherwise go back to app engine's default index.php `location` block.
location ~ ^/myapp(.*)$ {
# We can't send to /index.php$1 directly here because that would be caught
# by our `rewrite ^/index.php` causing infinite rewrites. So instead send
# this to the @fallback `location` below where we can do a final rewrite
# with the `last` keyword.
try_files $1 @fallback;
}
location @fallback {
rewrite ^/myapp(.*)$ /index.php$1 last;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment