How to Deploy a Static Website with NGINX on Railway

While it seems straightforward, hosting a static website on a custom domain for free hasn't traditionally been quite as noob-friendly. GitHub Pages allows custom domains for public repositories only, while AWS S3 requires you to jump a few CDN hoops to serve HTTPS-protected websites. Thankfully, newer platforms like Vercel, Netlify, Render and Railway are changing the status-quo for the better.

What is Railway?

Railway is a modern app hosting platform that makes it easy to deploy production-ready apps quickly. Railway offers persistent database services for PostgreSQL, MySQL, MongoDB, and Redis, as well as application services with a Github repository as the deployment source. For the latter, Railway can automatically determine the application runtime and deploy the service. Railway offers several one-click starters for common applications and use cases; we'll use the NGINX starter to deploy HTML files today.

Source: Railway.app

Deploy Your Static Website

Launch the NGINX starter on Railway, and sign in with Github to deploy the service.

NGINX one-click starter on Railway

Configure the Github accounts and orgs that Railway should have access to. Keep the defaults and click Install & Authorize.

Install & authorize Railway app on Github

You'll be prompted for the repository details and PORT environment variable. You can also choose to make the repository private. Accept the default repository name, or amend as desired, and click Deploy.

Deploy NGINX service on Railway

In a matter of seconds, your project will be ready, and the sample website will be deployed seamlessly on Railway. Click on the nginx-***.up.railway.app URL to launch the static website in a browser, and voila!

NGINX static website deployed on Railway 

The sample website shipped with the starter includes the following files:

  • site
    • favicon.ico
    • index.html
    • script.js
    • styles.css
  • Dockerfile
  • README.md

By default, the site directory gets deployed as a static website. Feel free to add or update the assets under this directory to reflect your own website - Railway will automatically detect the changes and trigger a new deployment.

Configure a Custom Domain (Optional)

Railway services can be exposed to the internet on a custom domain instead of the default  ***.up.railway.app subdomain. If you don't already own a domain, head over to Cloudflare Registrar or Namecheap and register your domain first.

Add a custom domain for the NGINX service

Navigate to the Settings tab for your service, specify a custom domain under Domains and click Add Domain. If the domain is available, you'll be prompted with the details to add to your DNS host.

Create a DNS record for the custom domain

Add a CNAME record as specified for this service e.g. CNAME nginx.example.com nginx-demo.up.railway.app, and wait for the DNS changes to be propagated. If you are using Cloudflare as your DNS host, disable the Proxy option while the certificate is being issued for your site.

You can use dig to check whether the DNS records have propagated e.g. dig nginx.example.com. You can also use the Google Admin Toolbox. Once DNS is set up, your static website will be publicly available. Railway's Starter tier is sufficient to test this, but the 500 hours monthly usage limit will require you to upgrade to the Developer tier for the website to be always available.