R
Railwayā€¢8mo ago
gr33nTHumB

Reverse proxy returning 502, but the underlying app is running

Trying to set up a simple proxy to handle CORS, but the proxy can't reach the application. It was working moments ago (without the CORS config) but I can't pinpoint what changed
Solution:
we aren't going to use a proxy, there's no reason to use a proxy just to set cors headers
Jump to solution
78 Replies
Percy
Percyā€¢8mo ago
Project ID: 9a8ccdaa-0d00-40e4-823c-5199e84248cf
gr33nTHumB
gr33nTHumBā€¢8mo ago
9a8ccdaa-0d00-40e4-823c-5199e84248cf
Brody
Brodyā€¢8mo ago
what kind of app are you trying to add cors headers to
gr33nTHumB
gr33nTHumBā€¢8mo ago
a Node.js app having issues with the native way issues = doesn't seem to work :p
Brody
Brodyā€¢8mo ago
well let's try to solve the actual underlying problem and not add more problems on top
gr33nTHumB
gr33nTHumBā€¢8mo ago
k
Brody
Brodyā€¢8mo ago
so we are throwing out the entire proxy thing, there shouldn't be anything wrong with setting headers from your app itself so more specifically what kind of node app is this
gr33nTHumB
gr33nTHumBā€¢8mo ago
so, here's the proxied link: https://bot-server-proxy.karensama.com/link and here the app with it's own domain: https://bot-server.karensama.com/link it should say Cannot GET cuz it's a POST route, butyou can see one is visible and the other blanks
Solution
Brody
Brodyā€¢8mo ago
we aren't going to use a proxy, there's no reason to use a proxy just to set cors headers
gr33nTHumB
gr33nTHumBā€¢8mo ago
ye, I'm just on a deadline and trying everything to get this out man ok, removing the proxy
Brody
Brodyā€¢8mo ago
you have access to the node apps code right?
gr33nTHumB
gr33nTHumBā€¢8mo ago
yep
Brody
Brodyā€¢8mo ago
^
gr33nTHumB
gr33nTHumBā€¢8mo ago

const options = {
origin: ['www.karensama.com', 'karensama.com'],
credentials: true
}

async function main() {
await mongoose.connect(process.env.MONGODB_CONNECTION)

server.use(express.json())

server.use(session({
secret: 'APDDRltTbfaZWgZYZrTNDRltTWgZYrft',
resave: false,
saveUninitialized: false,
name: 'exosama_tkgate',
cookie: {
expires: 1000000,
},
}))

server.use(cors(options))

const options = {
origin: ['www.karensama.com', 'karensama.com'],
credentials: true
}

async function main() {
await mongoose.connect(process.env.MONGODB_CONNECTION)

server.use(express.json())

server.use(session({
secret: 'APDDRltTbfaZWgZYZrTNDRltTWgZYrft',
resave: false,
saveUninitialized: false,
name: 'exosama_tkgate',
cookie: {
expires: 1000000,
},
}))

server.use(cors(options))
Brody
Brodyā€¢8mo ago
okay and show me some errors you get on the frontend that relate to cors?
gr33nTHumB
gr33nTHumBā€¢8mo ago
rn it's failing because of the 502 on the proxy I'm redeploying without the proxy, sec brah it's working now you must have a magic wand or something šŸ˜
Brody
Brodyā€¢8mo ago
see, no need to over complicate things
gr33nTHumB
gr33nTHumBā€¢8mo ago
when I sent you this, I realised the origins didn't have the protocol
gr33nTHumB
gr33nTHumBā€¢8mo ago
now the app doesn't work, but that's on me xD thanks again sir!
Brody
Brodyā€¢8mo ago
no problem! šŸ™‚
gr33nTHumB
gr33nTHumBā€¢8mo ago
best support ever ā¤ļø
Brody
Brodyā€¢8mo ago
thank you ā¤ļø
gr33nTHumB
gr33nTHumBā€¢8mo ago
if you're still here, I have another server with the same codebase running node telegram.js which is a telegram bot thingy the app we were talking about connets to that through websockets like so:
const wsTelegram = io(process.env.WEBSOCKET_URL_TELEGRAM)
wsTelegram.on("connect", () => console.log(`Connected to Telegram bot ${process.env.WEBSOCKET_URL_TELEGRAM}`))
const wsTelegram = io(process.env.WEBSOCKET_URL_TELEGRAM)
wsTelegram.on("connect", () => console.log(`Connected to Telegram bot ${process.env.WEBSOCKET_URL_TELEGRAM}`))
this is the value of the env var: ws://karensama-tg-socket.railway.internal/
Brody
Brodyā€¢8mo ago
you need to use a port in that url
gr33nTHumB
gr33nTHumBā€¢8mo ago
the thing is the domain always redirects to the $PORT, no? I tried with he port too, no bueno
Brody
Brodyā€¢8mo ago
nope, don't treat the internal domains like the public domains, you can think of like your local network but instead of IP addresses it's a nice domain name, you will always need to specify a port
gr33nTHumB
gr33nTHumBā€¢8mo ago
perhaps I'm setting the wrong port? I have $PORT set to 8805 but not sure that's what gets exposed
Brody
Brodyā€¢8mo ago
your app would also need to listen on :: to be accessable over the ipv6 only private network
gr33nTHumB
gr33nTHumBā€¢8mo ago
I've set up a subdomain now
Brody
Brodyā€¢8mo ago
it's a private network, everything is exposed, there's no proxy and no firewall
gr33nTHumB
gr33nTHumBā€¢8mo ago
ws://socket.karensama.com:8805/ but I'm pretty sure 8805 is not the port mapped to the service's 8805
Brody
Brodyā€¢8mo ago
^
gr33nTHumB
gr33nTHumBā€¢8mo ago
still applies using the public domain?
Brody
Brodyā€¢8mo ago
you are using the private domain for this?
gr33nTHumB
gr33nTHumBā€¢8mo ago
I was, but I'm trying to use all the options saw on the internet somewhere that internal domains are unstable for websockets and thought it might be related
Brody
Brodyā€¢8mo ago
nope not at all, where did you read that?
gr33nTHumB
gr33nTHumBā€¢8mo ago
ok, I have it set this way now: ws://socket.karensama.com:8805/ 8805 is the $PORT for the tg bot app, so it's running on that port
Brody
Brodyā€¢8mo ago
that doesn't say anything about internal domains?
gr33nTHumB
gr33nTHumBā€¢8mo ago
misread perhaps, not internal, but the wildcard domains
Brody
Brodyā€¢8mo ago
what service is starting the websocket server
gr33nTHumB
gr33nTHumBā€¢8mo ago
tg-websocket
Brody
Brodyā€¢8mo ago
what host is it listening on
gr33nTHumB
gr33nTHumBā€¢8mo ago
No description
gr33nTHumB
gr33nTHumBā€¢8mo ago
::
Brody
Brodyā€¢8mo ago
can you show me how you configured that
gr33nTHumB
gr33nTHumBā€¢8mo ago
server.listen(process.env.WEBSOCKET_PORT_TELEGRAM, '::', () => console.log(Websocket open on port ${process.env.WEBSOCKET_PORT_TELEGRAM})); I can change that port to something else if needed this is an old app that was in a single repo and I'm breaking it down into isolated things
Brody
Brodyā€¢8mo ago
it is in the same project right?
gr33nTHumB
gr33nTHumBā€¢8mo ago
yep
gr33nTHumB
gr33nTHumBā€¢8mo ago
No description
Brody
Brodyā€¢8mo ago
and what error are you getting trying to connect to it
gr33nTHumB
gr33nTHumBā€¢8mo ago
I don't see I can see those logs anywhere
const wsTelegram = io(process.env.WEBSOCKET_URL_TELEGRAM)
wsTelegram.on("connect", () => console.log(`Connected to Telegram bot ${process.env.WEBSOCKET_URL_TELEGRAM}`))
const wsTelegram = io(process.env.WEBSOCKET_URL_TELEGRAM)
wsTelegram.on("connect", () => console.log(`Connected to Telegram bot ${process.env.WEBSOCKET_URL_TELEGRAM}`))
Brody
Brodyā€¢8mo ago
the same way you showed me these logs
gr33nTHumB
gr33nTHumBā€¢8mo ago
that log never shows up in the platform's logs
Brody
Brodyā€¢8mo ago
are you logging to stdout/stderr?
gr33nTHumB
gr33nTHumBā€¢8mo ago
good question
Brody
Brodyā€¢8mo ago
how is bot-server being built? nixpacks? dockerfile?
gr33nTHumB
gr33nTHumBā€¢8mo ago
the default railway offer, it doesn't actually need to build other than npm install then it runs as node server.js and node telegram.js respectively
gr33nTHumB
gr33nTHumBā€¢8mo ago
I can see this on the logs, after making a request to the server, which would use the websocket
No description
gr33nTHumB
gr33nTHumBā€¢8mo ago
but that's a caught exception from here
No description
gr33nTHumB
gr33nTHumBā€¢8mo ago
I'll remove the handler to see the error
Brody
Brodyā€¢8mo ago
no lol, just log the error too
gr33nTHumB
gr33nTHumBā€¢8mo ago
I have no idea where the error is to log it :p so I'll jsut let it explode
Brody
Brodyā€¢8mo ago
well that's one way to do it show me the wsTelegram code
gr33nTHumB
gr33nTHumBā€¢8mo ago
what if I told you that removing those lines, it works? you gotta be kidding me javascript the error handler was erroring šŸ¤¦ā€ā™‚ļø
Brody
Brodyā€¢8mo ago
sounds flakey, do a few more redeploys to see if it's stable
gr33nTHumB
gr33nTHumBā€¢8mo ago
ye, I'll definitely do that
Brody
Brodyā€¢8mo ago
at what point in your apps lifecycle do you make this connection to the websocket server
gr33nTHumB
gr33nTHumBā€¢8mo ago
at initialization the very first lines after the imports:
const wsTelegram = io(process.env.WEBSOCKET_URL_TELEGRAM)
wsTelegram.on("connect", () => console.log(`Connected to Telegram bot ${process.env.WEBSOCKET_URL_TELEGRAM}`))
const wsTelegram = io(process.env.WEBSOCKET_URL_TELEGRAM)
wsTelegram.on("connect", () => console.log(`Connected to Telegram bot ${process.env.WEBSOCKET_URL_TELEGRAM}`))
Brody
Brodyā€¢8mo ago
there is around 2 seconds when your app starts where the dns resolver for the private network hasn't initialised yet, add a sleep for 3 seconds before attempting the websocket connection
gr33nTHumB
gr33nTHumBā€¢8mo ago
hmmmmm that's interesting
Brody
Brodyā€¢8mo ago
but definitely use a health check so that railway knows when your app starts
gr33nTHumB
gr33nTHumBā€¢8mo ago
yeah, Railway offers all the tools for me to avoid that happening but I should have deployed this yesterday already and spent a;lll day fiddling in AWS
Brody
Brodyā€¢8mo ago
well you made the right choice coming to railway
gr33nTHumB
gr33nTHumBā€¢8mo ago
tell me about it! devops devs dont work on Fridays šŸ¤£ if I was to ask a devops to set all this up in aws, would get a week timeline probably
gr33nTHumB
gr33nTHumBā€¢8mo ago
this is the correct place to check for app logs, right?
No description
Brody
Brodyā€¢8mo ago
yes it is, if you are logging to stdout / stderr without buffering it will show up there