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 

Solved Solved
0 8 469
1 ACCEPTED SOLUTION


@tasos wrote:

Solution:

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


View solution in original post

8 REPLIES 8

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.

It's an issue of 7.4 > the nginx conf has been changed.

I'm having this same problem.   what's missing in the ...  portions of that nginx file?

did you just copy the original and essentially comment out the one line?

Look at the php8.x automatically generated nginx conf files, and the php7.x nginx conf files and compare them. Then modify the php8.x nginx configuration according to your project needs.

You can read more on:
Nginx - Serving static content
Nginx-Docs 

Yes I am having the same issue -  I reported this issue late last year and the Google support staff told me to come to the forums here so others can solve the problem for me. The only solution is to overwrite the nginx.conf - you can't rely on google support. Basically same solution as yourself.

Hi, I am upgrading also from 7.3 to 8.1 (Laravel) and encountered the same problem, a solution that does not require substituting the whole configuration is to use this nginx-app.conf file and it should work.

Now I have another problem and that is that I cannot use the php artisan optimize command as it does not take the env_variables from the app.yaml for some reason (I use the command in the post-install-cmd section of composer.json when deploying), in 7.3 it worked fine.

I believe just modifying nginx-app.conf shouldn't work because the line

rewrite ^/(.*)$ /index.php$uri;

at the start of nginx.conf takes precedence over it.

It seems that your problem is on the app.yaml file. You probably have mistyped something when you made changes to it. Make sure it is formatted properly before deploying.


@tasos wrote:

Solution:

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