Hosting a static site (mdbook) on Railway
jacksmithxyz
HOBBYOP

5 months ago

Hello all, I am tying to host a static site generated with mdbook on Railway.

I have set up the RAILPACK_PACKAGES environment variable with mdbook, and set up the RAILPACK_STATIC_FILE_ROOT environment variable with the value book, which is the output directory.

It seems to build and deploy successfully, but when going to the generated domain it returns a 404.

If I add mdbook build as the custom build command it seems to run it in build logs, but then crashes in deployment.

Any ideas?

$10 Bounty

12 Replies

jacksmithxyz
HOBBYOP

5 months ago

The crashing seems to be from the following:
Error: reading config from file: open Caddyfile: no such file or directory


5 months ago

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
	}
}

samgordon

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 } }

jacksmithxyz
HOBBYOP

5 months ago

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.


jacksmithxyz

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.

5 months ago

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?


noahd

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?

jacksmithxyz
HOBBYOP

5 months ago

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


jacksmithxyz

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

5 months ago

Do you still have STATIC_FILE_ROOT set to /book?


samgordon

Do you still have STATIC_FILE_ROOT set to /book?

jacksmithxyz
HOBBYOP

5 months ago

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


jacksmithxyz

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

5 months ago

That might be the issue! Can you try setting it to /book?


5 months ago

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


jacksmithxyz
HOBBYOP

5 months ago

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.

Attachments


jacksmithxyz
HOBBYOP

5 months ago

The only other solution I can think of here would be to take the book directory out of the .gitignore, and commit the files, 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.

I'm starting to think it's something to do with the configuration with the book.toml file, as opposed to something on Railway.


jacksmithxyz

The only other solution I can think of here would be to take the book directory out of the .gitignore, and commit the files, 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.I'm starting to think it's something to do with the configuration with the book.toml file, as opposed to something on Railway.

5 months ago

Can you send the GitHub repository?


Loading...