502 on app with correct HOST and PORT

theo-nejmHOBBY

8 months ago

Hi, friends! I'm deploying a new app on railway, I've followed the guides without problem.

The build was successful, the application is running, but I can't access this, because I'm getting a 502 status code.

https://docs.railway.app/guides/fixing-common-errors

I've followed this guide but didn't solved my problem.

I'm already using the PORT that railway provides and the host is set to 0.0.0.0

Am I missing something?

0 Replies

theo-nejmHOBBY

8 months ago

75f448f4-88c9-4d82-b428-61740d90e40c


theo-nejmHOBBY

8 months ago

My public route is this:
niki-backend-homolog.up.railway.app


theo-nejmHOBBY

8 months ago

It just works if I specify the app port to 3000 on railway env :/


8 months ago

hey, send your app.listen.
I also use nest and have no problems, pretty weird.


theo-nejmHOBBY

8 months ago

await app.listen(process.env.PORT || 3000, '0.0.0.0');


8 months ago

I don't even need the 0.0.0.0, maybe you could give it a try?
but anyway, the port env would work without any problems.

1278189202911137800


theo-nejmHOBBY

8 months ago

k, imma try removing the 0.0.0.0


theo-nejmHOBBY

8 months ago

but it was not working also


theo-nejmHOBBY

8 months ago

still not working


theo-nejmHOBBY

8 months ago

when i removed the 0.0.0.0 and port


theo-nejmHOBBY

8 months ago

just works when i specify the port :/


8 months ago

weird, are u using fastify or express?


theo-nejmHOBBY

8 months ago

fastify


8 months ago

hmmm im using express so it might have a difference


theo-nejmHOBBY

8 months ago

hmm maybe


8 months ago

I guess I've an application where I used fastify but anyway the port env should be working alright so I guess there's no need to do something about it


theo-nejmHOBBY

8 months ago


theo-nejmHOBBY

8 months ago

without solution


theo-nejmHOBBY

8 months ago

(when I specify the port it works)


theo-nejmHOBBY

8 months ago

should I specify this and just move forward?


theo-nejmHOBBY

8 months ago

should I have any problem by doing this?


8 months ago

couldnt find any other issue related to fastify and 502


8 months ago

yep its alright


theo-nejmHOBBY

8 months ago

oh, i have another problem


theo-nejmHOBBY

8 months ago

the database cannot connect to run migrations without the public url


theo-nejmHOBBY

8 months ago

my build command runs the migrations before the build


theo-nejmHOBBY

8 months ago

to keep the db up-to-date


theo-nejmHOBBY

8 months ago

but it just works when I add an env variable with the public database url


8 months ago

the private networking isn't available at build unfortunately.
in my case I do the migration on the start script


theo-nejmHOBBY

8 months ago

hmm


theo-nejmHOBBY

8 months ago

is there any problem to use the public url?


theo-nejmHOBBY

8 months ago

(just for migrations)


8 months ago

by using public url you'll be charged more by bandwidth.
I don't know how much you would but I'm pretty sure it would be cheap


theo-nejmHOBBY

8 months ago

hmm, it is used only at migrations, so I guess there is no problem


8 months ago

If you're sure of that then ok


8 months ago

but tbh I would just move the migration script to the start command


theo-nejmHOBBY

8 months ago

you put migrations before start, right?


theo-nejmHOBBY

8 months ago

with && operator


8 months ago

yep, are u using prisma?


theo-nejmHOBBY

8 months ago

if migrations fail, you dont start?


theo-nejmHOBBY

8 months ago

typeorm


8 months ago

ah typeorm


theo-nejmHOBBY

8 months ago

okay, imma try this approach


theo-nejmHOBBY

8 months ago

put it on start script


8 months ago

yep but my backend contains a healthcheck
if yours dont then it would just fail



theo-nejmHOBBY

8 months ago

lemme understand
in this case, what does the healthcheck change?


8 months ago

this is my script with prisma:

"start": "prisma generate --no-hints && prisma migrate deploy && node dist/src/main",

theo-nejmHOBBY

8 months ago

i didnt set a healthcheck yet


8 months ago

Railway will hold the deployment of your application until its actually reachable by doing an HTTP ping to a /health endpoint


8 months ago

you've to configure it in the service settings


theo-nejmHOBBY

8 months ago

oh, so it will not stop trying to restart until the app is up?


8 months ago

trying to restart? you mean an infinite loop of fail restart?


theo-nejmHOBBY

8 months ago

yup


8 months ago

nope, thats another setting

1278193155992195000


theo-nejmHOBBY

8 months ago

where should I configure health route?


8 months ago

1278193469931917300


8 months ago

go to your service settings and scroll down until you see that


theo-nejmHOBBY

8 months ago

oh good


theo-nejmHOBBY

8 months ago

will this be enough?

@Controller('health')
export class HealthController {
  @Get()
  healthCheck() {
    return { ok: true };
  }
}

8 months ago

yep it should be alright


8 months ago

nestjs also offers the option to check your database and other external services but thats optional, that health controller will work


theo-nejmHOBBY

8 months ago

imma see them later, if my db is down, the routes will be the smaller problem lmao


8 months ago

makes sense <:kekw:788259314607325204>


theo-nejmHOBBY

8 months ago

ive added the route, but its not working, probably because previous deploy was not with port


8 months ago

wdym by not working? 502 or build not going through?


theo-nejmHOBBY

8 months ago

502


theo-nejmHOBBY

8 months ago

the deploy with the port is not working


theo-nejmHOBBY

8 months ago

because the previous one was the health one


theo-nejmHOBBY

8 months ago

and the status is 502, because there is no port yet


theo-nejmHOBBY

8 months ago

🤣


theo-nejmHOBBY

8 months ago

removed healthcheck for a while


8 months ago

no port yet? Railway should just do a load balance so both of the deploys will contain the port


theo-nejmHOBBY

8 months ago

the problem was the 0.0.0.0 probably


theo-nejmHOBBY

8 months ago

it appears to be needed on fastify apps


8 months ago

yep, i'll see if I can get my fastify app to see what I did but it just worked 💀


theo-nejmHOBBY

8 months ago

gods with u


8 months ago

yep it just works 💀

1278197666362294300


theo-nejmHOBBY

8 months ago

not using process to env? 😕


8 months ago

I use to validate my env variables and then import it elsewhere


theo-nejmHOBBY

8 months ago

interesting


theo-nejmHOBBY

8 months ago

u use redis in any of your nest apps?


theo-nejmHOBBY

8 months ago

deployed on railway


8 months ago

yes


theo-nejmHOBBY

8 months ago

does it worth it?


8 months ago

you mean in terms of pricing?


theo-nejmHOBBY

8 months ago

yes


theo-nejmHOBBY

8 months ago

instead of using the instance memory itself


8 months ago

well Railway's memory pricing is not that cheap but if your just using for cache it should be alright


theo-nejmHOBBY

8 months ago

well, health check is now working


theo-nejmHOBBY

8 months ago

obrigado amigo


theo-nejmHOBBY

8 months ago

late night, imma sleep


8 months ago

I used to host a bullmq queue with at least 16k per minute inserts and it was pretty cheap IMO


8 months ago

great!


8 months ago

boa noite!


502 on app with correct HOST and PORT - Railway Help Station