Problem connecting frontend and backend - 502 or 504

gabrielbc6
HOBBY

2 months ago

I have a backend service that is not receiving api calls from my frontend service, the stack is flask + react.js, the public url of the frontend is running on port 80 and the nginx is getting the backend with this proxy: proxy_pass http://${BACKEND_URL_WITH_PORT}, with port 5000 for the backend.

The problem is that everytime I try to do an api call from the frontend, the response is either 502 or 504, the database environment is correctly setup in the backend, but no logs from what error is causing the 502 is appearing.

$10 Bounty

7 Replies

2 months ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody 2 months ago


smolpaw
HOBBYTop 10% Contributor

2 months ago

The details are very vague. I need more details to suggest a solution. Like config files, logs of each service. potentially the link to your frontend as well as your nginx config file.


gabrielbc6
HOBBY

2 months ago

These are the backend logs, only this appears, attached there are my nginx, the frontend logs:
[27/May/2025:03:56:52 +0000] "POST /example_api_call HTTP/1.1" 504 569
[error] 4#4: *1 upstream timed out (110: Operation timed out) while connecting to upstream
Nginx.conf.template:

server {
  listen 80;
  server_name localhost;

  root /usr/share/nginx/html;
  index index.html index.htm;

  location / {
    try_files $uri $uri/ /index.html;
  }

  location /api {
    proxy_pass http://${BACKEND_URL_WITH_PORT};
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

Attachments


gabrielbc6

These are the backend logs, only this appears, attached there are my nginx, the frontend logs:[27/May/2025:03:56:52 +0000] "POST /example_api_call HTTP/1.1" 504 569[error] 4#4: *1 upstream timed out (110: Operation timed out) while connecting to upstreamNginx.conf.template:server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html index.htm; location / { try_files $uri $uri/ /index.html; } location /api { proxy_pass http://${BACKEND_URL_WITH_PORT}; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }

smolpaw
HOBBYTop 10% Contributor

2 months ago

In your nginx config file is this your actual url "${BACKEND_URL_WITH_PORT}" or is that just a placeholder you gave me?

If that is actually what you are using then can you change it to the actual private domain mentioned in Settings -> Private Networking ?
This should be your backend.railway.internal:8080
replace "backend" with your service name and "8080" with your backend's actual port


smolpaw
HOBBYTop 10% Contributor

2 months ago

If that doesn't work then replace the api block with this

location /api {
    # Use Docker's internal DNS resolver.
    # The 'valid' parameter specifies how long Nginx should cache the resolved IP.
    resolver 127.0.0.11 valid=30s;

    # Set the backend address as a variable.
    # Using a variable triggers Nginx to use the resolver.
    set $backend_upstream backend:3000; # 'backend' is service name, 3000 is internal port

    proxy_pass http://$backend_upstream;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }

gabrielbc6
HOBBY

2 months ago

This is a variable that is currently being replaced in the build of the frontend, i confirmed it in the build logs, but i am using port 5000, since in my backend dockerfile i do

CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]

smolpaw
HOBBYTop 10% Contributor

2 months ago

The problem isn't your frontend. It's nginx not being able to connect to your backend because the url you provided to nginx is incorrect.
The connection to backend is getting timed out so your frontend also doesn't get anything.

This is your network flow right ? Frontend -> Nginx -> Backend ?
If nginx isn't working then frontend won't either.

Replace the config api block with what i have provided. replace "backend" with your backend's service name and "port" to your backend's actual port which should be 5000 according to your dockerfile


gabrielbc6
HOBBY

2 months ago

I used the block below(with my actual backend service name) and now all my requests to the endpoint return 502 status after 30 seconds:

  location /api {
    # Use Docker's internal DNS resolver.
    # The 'valid' parameter specifies how long Nginx should cache the resolved IP.
    resolver 127.0.0.11 valid=30s;

    # Set the backend address as a variable.
    # Using a variable triggers Nginx to use the resolver.
    set $backend_upstream (my_backend_name_here).railway.internal:5000; # 'backend' is service name, 3000 is internal port

    proxy_pass http://$backend_upstream;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}