Search code examples
google-app-engineapp-engine-flexiblegoogle-app-engine-php

PHP App Engine Flex serving SPA and API


My goal is to serve a SPA and PHP api on the same site. I would like to be able to browse the website at mywebsite.com, and request api calls at mywebsite.com/api/.

My directory structure is:

public
|
+-- index.html 
+-- api
     |
     index.php

My app.yaml:

runtime: php
env: flex

runtime_config:
  document_root: public

Navigation to mywebsite.com gives a 404 because public/index.php doesn't exist.

So I tried this app.yaml:

runtime: php
env: flex

runtime_config:
  document_root: public
  front_controller_file: index.html

And I can access mywebsite.com normally since index.html is the default file, but api/index.php is still 404.

Is something like this possible on App Engine php flex? I've read the docs- https://cloud.google.com/appengine/docs/flexible/php/configuring-your-app-with-app-yaml

Thanks!


Solution

  • Remove the front_controller_file in the runtime_config. Create a file named nginx-app.conf with the following content:

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

    Then re-deploy. Both URL will work.