#Hosting a static site (mdbook) on Railway

10 messages · Page 1 of 1 (latest)

gloomy novaBOT
#

New reply sent from Central Station thread:

No idea why this is happening but let's try this. Create a file named
Caddyfile
at the root of your project. Put the following in it.
{
admin off
persist_config off
auto_https off

log {
format json
}

servers {
trusted_proxies static private_ranges
}
}

:{$PORT:80} {
log {
format json
}

respond /health 200

Security headers

header {
# Enable cross-site filter (XSS) and tell browsers to block detected attacks
X-XSS-Protection "1; mode=block"
# Prevent some browsers from MIME-sniffing a response away from the declared Content-Type
X-Content-Type-Options "nosniff"
# Keep referrer data off of HTTP connections
Referrer-Policy "strict-origin-when-cross-origin"
# Enable strict Content Security Policy
Content-Security-Policy "default-src 'self'; img-src 'self' data: https: *; style-src 'self' 'unsafe-inline' https: *; script-src 'self' 'unsafe-inline' https: *; font-src 'self' data: https: *; connect-src 'self' https: *; media-src 'self' https: *; object-src 'none'; frame-src 'self' https: *;"
# Remove Server header
-Server
}

root * {{.STATIC_FILE_ROOT}}

Handle static files

file_server {
hide .git
hide .env*
}

Compression with more formats

encode {
gzip
zstd
}

Try files with HTML extension and handle SPA routing

try_files {path} {path}.html {path}/index.html /index.html

Handle 404 errors

handle_errors {
rewrite * /{err.status_code}.html
file_server
}
}

You're seeing this because this thread has been automatically linked to the Central Station thread.

gloomy novaBOT
#

New reply sent from Central Station thread:

This seems to have fixed the server from crashing, even with the
mdbook build
as the build command, but it has the problem where going to the domain leads to a 404.

You're seeing this because this thread has been automatically linked to the Central Station thread.

#

New reply sent from Central Station thread:

Hmmm. Do you have a few routes that you can test on to make sure? This seems like it might be a application handling issue but I'm unsure.
Do the routes all work locally?

You're seeing this because this thread has been automatically linked to the Central Station thread.

#

New reply sent from Central Station thread:

When I build it with
mdbook build
locally, it generates the files in the
book
directory as expected, and I can open the
index.html
file up from here and access all of the files locally as you would expect.
I'm not entirely sure how I managed to break it on Railway or if I'm doing something wrong haha

You're seeing this because this thread has been automatically linked to the Central Station thread.

gloomy novaBOT
#

New reply sent from Central Station thread:

Hm, I have
STATIC_FILE_ROOT
set to
book
, not
/book
. Maybe this is the issue?

You're seeing this because this thread has been automatically linked to the Central Station thread.

#

New reply sent from Central Station thread:

That would be why! If you look in the caddy file it appends a . to it. If you said it to book it'll be using the directory
.book
, where it should be using
./book

You're seeing this because this thread has been automatically linked to the Central Station thread.

gloomy novaBOT
#

New reply sent from Central Station thread:

This still doesn't work - crashes without the
Caddyfile
, and builds with it - but loads a 404 on the generated domain.
I'm wondering if it's still something to do with the build not put into the correct directory or something. Here's some of the build logs attached.
I do appreciate both of your help so far in trying to resolve this though, so thank you.

You're seeing this because this thread has been automatically linked to the Central Station thread.

#

New reply sent from Central Station thread:

The only other solution I can think of here would be to take the
book
directory out of the .gitignore, and commit the hosted website and use this as the directory to look in. Ideally though it would be great if I could host the source files on GitHub, and have Railway handle the builds etc.

You're seeing this because this thread has been automatically linked to the Central Station thread.