Fixing Common Errors

When deploying to Railway, you may encounter some errors that prevent your application from working as expected. These are descriptions and solutions to errors that users commonly encounter.

Application failed to respond

After deploying your application or making changes, you might encounter this screen when accessing your application's domain:

Screenshot of application failed to respond error

This error occurs when Railway cannot communicate with your application, causing your request to fail with a 502 (Bad Gateway) status code.

For Railway to communicate with your application, your web server should be available at host 0.0.0.0 and listen on the port specified by the PORT environment variable, which Railway automatically injects into your application.

Additionally, if you're using target ports for your public domain, have your target port set to the correct value.

Without these configurations, Railway cannot establish a connection to your running application.

Solution

To fix this, start your application's server using:

  • Host = 0.0.0.0
  • Port = Value of the PORT environment variable provided by Railway.

Next, if you're using target ports, ensure that the target port for your public domain matches the port your application is now listening on.

This setting can be found within your service settings.

Screenshot showing target ports on a domain Screenshot showing that the domain was previously configured with port 3000, and the correct 8080 port.

Below are some solution examples for common languages and frameworks.

Node / Express

// Use PORT provided in environment or default to 3000
const port = process.env.PORT || 3000;

// Listen on `port` and 0.0.0.0
app.listen(port, "0.0.0.0", function () {
  // ...
});

Node / Nest

// Use `PORT` provided in environment or default to 3000
const port = process.env.PORT || 3000;

// Listen on `port` and 0.0.0.0
async function bootstrap() {
  // ...
  await app.listen(port, "0.0.0.0");
}

Node / Next

Next needs an additional flag to listen on PORT:

next start --port ${PORT-3000}

Python / Gunicorn

gunicorn listens on 0.0.0.0 and the PORT environment variable by default:

gunicorn main:app

There is no additional configuration necessary.

Python / Uvicorn

uvicorn needs additional configuration flags to listen on 0.0.0.0 and PORT:

uvicorn main:app --host 0.0.0.0 --port $PORT

Go / net/http

This example is for net/http in the Go standard library, but you can also apply this to other frameworks:

func main() {
  // ...
  // Use `PORT` provided in environment or default to 3000
  port := cmp.Or(os.Getenv("PORT"), 3000)

  log.Fatal(http.ListenAndServe((":" + port), handler))
  // ...
}

POST requests turn into GET requests

You might encounter this issue when testing your backend with tools like curl, where your POST requests are all converted into GET requests when they reach your app.

Depending on the application, this may result in your application returning either a 405 Method Not Allowed or a 404 Not Found status code.

This occurs because your request was made using HTTP. Railway will attempt to redirect your insecure request with a 301 Moved Permanently status code.

When an HTTP client encounters a 301 Moved Permanently redirect, the client will follow the redirect. However, according to the HTTP/1.1 specifications, the client will typically change the request method from POST to GET when it follows the redirect to the new URL.

Solution

Fortunately, the solution is simple: Ensure you are using HTTPS when calling your Railway-hosted services.


Edit this file on GitHub