Created
February 27, 2024 18:28
-
-
Save sonnysasaka/e9bdf5fd40c9ac14390c63871bb33e59 to your computer and use it in GitHub Desktop.
Allow serving static files on Google App Engine second generation PHP flex runtime - alternate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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; | |
# If you need to handle any php file, not just index.php | |
# This is mostly copied from https://github.com/GoogleCloudPlatform/buildpacks/blob/ff2ea2737a928087dcec192bf3ce103dc356ad5e/pkg/nginx/nginx.go#L95 | |
# with the difference being that we remove our hack /myapp prefix | |
location ~ ^/myapp(.*)\.php$ { | |
error_log stderr; | |
fastcgi_pass fast_cgi_app; | |
fastcgi_buffering off; | |
fastcgi_request_buffering off; | |
fastcgi_cache off; | |
fastcgi_store off; | |
fastcgi_intercept_errors off; | |
fastcgi_split_path_info ^/myapp(.+\.php)(.*)$; | |
fastcgi_index $fastcgi_script_name; | |
fastcgi_param QUERY_STRING $query_string; | |
fastcgi_param REQUEST_METHOD $request_method; | |
fastcgi_param CONTENT_TYPE $content_type; | |
fastcgi_param CONTENT_LENGTH $content_length; | |
fastcgi_param SCRIPT_NAME $fastcgi_script_name; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
fastcgi_param PATH_INFO $fastcgi_path_info; | |
fastcgi_param REQUEST_URI $request_uri; | |
fastcgi_param DOCUMENT_URI $fastcgi_script_name; | |
fastcgi_param DOCUMENT_ROOT $document_root; | |
fastcgi_param SERVER_PROTOCOL $server_protocol; | |
fastcgi_param REQUEST_SCHEME $scheme; | |
if ($http_x_forwarded_proto = 'https') { | |
set $https_setting 'on'; | |
} | |
fastcgi_param HTTPS $https_setting if_not_empty; | |
fastcgi_param GATEWAY_INTERFACE CGI/1.1; | |
fastcgi_param REMOTE_ADDR $remote_addr; | |
fastcgi_param REMOTE_PORT $remote_port; | |
fastcgi_param REMOTE_HOST $remote_addr; | |
fastcgi_param REMOTE_USER $remote_user; | |
fastcgi_param SERVER_ADDR $server_addr; | |
fastcgi_param SERVER_PORT $server_port; | |
fastcgi_param SERVER_NAME $server_name; | |
fastcgi_param X_FORWARDED_FOR $proxy_add_x_forwarded_for; | |
fastcgi_param X_FORWARDED_HOST $http_x_forwarded_host; | |
fastcgi_param X_FORWARDED_PROTO $http_x_forwarded_proto; | |
fastcgi_param FORWARDED $http_forwarded; | |
} | |
# 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