Troubleshooting Nginx in Google Cloud Flexible Environment: Upgrading from PHP 7.3 ( Laravel)

Issue:
Facing challenges with Nginx configuration for serving static files in Google Cloud Flexible Environment with PHP 8.1 runtime.


Observations:

  • Configured app.yaml file with necessary settings for the Flexible Environment, PHP runtime, and Nginx. Despite adding location blocks for static files in nginx-app.conf, the expected outcome is not achieved.
  • Connected with ssh to the PHP 8.1 instance. Notably, the /etc/nginx/conf.d directory is empty, contrary to the setup with PHP 7.3. A test header added to the configuration is successfully recognized, suggesting successful reading of configuration files. Upon running ps -ax | grep nginx, the master process is found to use the configuration file located at /layers/google.php.webconfig/webconfig/nginx.conf instead of the expected /etc/nginx/nginx.conf.
  • The nginx.conf file within the layers/google.php.webconfig/webconfig/ directory contains an include /layers/google.php.webconfig/webconfig/nginxserver.conf; directive. This nginxserver.conf file, in turn, includes include /workspace/nginx-app.conf;, which references the application-specific configuration file.
  •  nginx -T command passes but returns incorrect configuration files, possibly due to the containerized environment.
  • After examining the contents of /layers/google.php.webconfig/webconfig/nginxserver.conf, I noticed a line rewrite ^/(.*)$ /index.php$uri;, which appears to take precedence over my custom additional location blocks. This, along with several other configuration parameters that didn't align with my requirements, compelled me to overwrite the entire nginx.conf file.

Solution:

By substituting the default nginx.conf file with a customized version, I successfully enabled the serving of static files.

To overwrite the entire nginx.conf file, replace nginx_conf_include: nginx-app.conf with nginx_conf_override: nginx.conf, where nginx.conf is the custom configuration file. Copy and paste the existing nginx.conf file from the layers/google.php.webconfig/webconfig/ directory and modify it according to your needs.

app.yaml (sample)

 

runtime: php # The name of the runtime environment that is used by your app.
env: flex # Select the flexible environment.

runtime_config:
  operating_system: "ubuntu18" # The version of the operating system to use.
  runtime_version: "8.1" # The version of PHP to use.
  document_root: public
  front_controller_file: index.php
  nginx_conf_override: nginx.conf # Instead of nginx_conf_include: nginx-app.conf

...

 

configuring nginx (sample)

 

server {
        ...

        root    /workspace/public;

        # commented out: rewrite ^/(.*)$ /index.php$uri;
        index index.php index.html index.htm;

        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
       
       ...

       # Media: images, icons, video, audio, HTC
        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
          expires 10m;
          access_log off;
          etag on;
          add_header Cache-Control "public";
        }

        # CSS and Javascript
        location ~* \.(?:css|js)$ {
          expires 10m;
          access_log off;
          etag on;
          add_header Cache-Control "public";
        }
       
        ...
}

 

Questions:

  1. Have others encountered the same directory layout change when attempting to upgrade to a newer PHP runtime version?

  2. Upon uploading the customized NGINX version, I've noticed the absence of the layers/google.php.webconfig/ directory. Why is this the case, considering that my custom NGINX file is being read correctly?

  3. Could someone provide an overview of the container structure typically built using buildpacks?

  4. What are the potential issues associated with overwriting the nginx.conf file?

  5. Why was the default configuration set to pass everything through index.php?

Documentation:
App Engine Flexible - PHP runtime documentation 

0 1 141
1 REPLY 1

This seems to be an issue with all versions of php. See my post here:

https://www.googlecloudcommunity.com/gc/Serverless/GAE-Flexible-PHP-8-2-Runtime-Bug/td-p/720812

Today, I just encountered this same issue moving an older app from 7.3 to 7.4.