How to serve a static HTML page on Platform.sh

Goal

To serve a static HTML page on Platform.sh.

Assumptions

You will need:

Problems

In addition to your HTML file, Platform.sh needs three YAML files to configure an application in a project. These files define the routing, configure the web server, and set minimal defaults for everything else.

Steps

1. Set git remote to Platform.sh project

$ git init
$ platform project:set-remote <project id>

2. Create your HTML file:

./web/index.html

<!DOCTYPE html>
<html>
  <body>
    <p>Hello World</p>
  </body>
</html>

3. Define routes

./.platform/routes.yaml

https://{default}/:
    type: upstream
    upstream: htmlhowto:http

4. Add empty services

./.platform/services.yaml

# empty

5. Add .platform.app.yaml

./.platform.app.yaml

# The name of this app. Must be unique within a project.
name: htmlhowto

# Any type will work. There is no "plain HTML" type.
type: "python:3.7"

# There is no need for a writable file mount, so set it to the smallest possible size.
disk: 256

# Configure the web server to serve our static site.
web:
    commands:
        # Run a no-op process that uses no CPU resources, since this is a static site.
        start: sleep infinity

    locations:
        # This tells Nginx to serve from the base directory
        "/":
            root: "web"
            index:
                - "index.html"

This is a stripped down application configuration for serving a static file on Platform.sh. You can find more information about additional commands that can be included in .platform.app.yaml in the Platform.sh documentation.

6. Add, commit, and push these files to your empty Platform.sh project

git add .
git commit -m "Adding configuration to serve a static html file"
git push platform master

7. Test by visiting the URL of your environment.

2019-02-25_10-54-29

Conclusion

By adding a route to routes.yaml, and by adding the proper web server configuration to .platform.app.yaml, a project is able to serve static HTML files.

Is it possible to remove the trailing slash?

Unfortunately, it not possible. We use the following Nginx directive try_files $uri $uri/ =404; which is not customizable by users. As explained in the try_file documentation, if Nginx cannot match the URL path as a file it will try to match it as a directory. The path will always match the existing directory whether it ends with a slash or not.

1 Like

There is a way to catch the 404, but it is not officially supported:

https://community.platform.sh/t/custom-404-page-for-a-static-website/637