Rista

Deploying with nginx

The output of rista build is a directory of files. Any file host can serve it, as long as it serves /blog from blog/index.html. For nginx, Rista writes the config. For Cloudflare and GitHub Pages, see the next page.

The config

rista nginx prints an nginx server block for the site in the current directory:

bundle exec rista nginx > nginx.conf
bundle exec rista nginx --port 8080 --server-name example.com --root /srv/site
OptionDefault
--port80
--server-name_
--root/usr/share/nginx/html

The defaults fit the official nginx image, where the file goes to /etc/nginx/conf.d/default.conf. The config:

  • serves /blog from blog/index.html, and redirects /blog/ to /blog
  • answers a missing path with the nearest 404.html and status 404
  • caches the hashed files under /assets/ forever
  • compresses text responses with gzip

The config depends on the site, because each directory with a not-found page gets its own location. Print it again after adding or removing one.

A Docker image

A Dockerfile for a site whose Gemfile loads rista from RubyGems:

FROM ruby:4.0-slim AS build
RUN apt-get update \
  && apt-get install -y --no-install-recommends build-essential pkg-config libmagickwand-dev libssl-dev
WORKDIR /site
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . .
RUN bundle exec rista build -o /out \
  && bundle exec rista nginx > /nginx.conf

FROM nginx:alpine
COPY --from=build /nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /out /usr/share/nginx/html

The build stage is Debian, because the Klenod plugins ship prebuilt gems for glibc Linux. Gemfile.lock needs those platforms:

bundle lock --add-platform x86_64-linux-gnu aarch64-linux-gnu

The build stage needs network access if the site imports Google Fonts.

Under a sub-path

A site is served from the root of its host by default. To serve it from a sub-path such as /docs, set the asset base in klenod.config.rb:

base "/docs/assets/"

The site’s root is the directory above the assets. Pages and Markdown keep their links as they are: %a{ href: "/blog" } and [the blog](/blog) are written as /docs/blog. The same goes for src, action, formaction and poster on any element, so %img{ src: "/logo.png" } finds a file in public/.

CommandUnder /docs
buildwrites the same files; serve the output directory at /docs
serveserves the site at http://localhost:4000/docs
nginxprints locations under /docs, and expects the files in <root>/docs

In the Dockerfile above, that makes the last line COPY --from=build /out /usr/share/nginx/html/docs.

Two more things to set:

  • :url in src/site.rb is the full address, such as https://example.com/docs, so the sitemap and the feed link to the right pages. build fails when the path of :url is not the sub-path.
  • Write the base yourself in the places Rista does not read: a srcset written by hand, inline scripts and styles, and files in public/. The srcset of an imported image already has it.

$request.path is the route path, without the base.

This site

example/Dockerfile in the Rista repository builds this site the same way. To try it at http://localhost:8080, run this from the repository root:

docker compose -f example/compose.yaml up --build