502 Bad Gateway using a reverse proxy

Hello everyone,

I’m deploying a reverse proxy to split the backend and frontend traffic by path. I want to send the backend application all the traffic with the path starting with /api and send any other path to the frontend application.

E.g

[mysubdomain].domain.com → frontend application
[mysubdomain].domain.com/api → backend application (Hosted in Fly)

I have a very simple Nginx file:

server {
  listen 8080;
  listen [::]:8080;

  server_name *.domain.io;

  location / {
      proxy_pass https://myfrontend-domain.com;
      proxy_set_header X-Forwarded-Host $http_host;
  }

    location /api {
      proxy_pass https://myflyapp.fly.dev;
      proxy_set_header X-Forwarded-Host $http_host;
  }
}

Everything works fine with the /path, it sends all the traffic to the frontend application, but when I type the backend path /api a 502 Bad Gateway Nginx error message appears.

In my reverse proxy logs I’m getting the error [error] 548#548: *40 peer closed connection in SSL handshake while SSL handshaking to upstream

It seems like my backend application has an Nginx configuration that prevents a secure connection is made.

Does anyone has had this error?

Thank you!

Try these lines in the /api location:

proxy_ssl_name $host;
proxy_ssl_server_name on;

Hey @jerome, adding proxy_ssl_server_name on; did the trick.

Thanks a lot!

1 Like