Serving multiple services with different internal ports not working

I’m unable to get running an app which attaches multiple services on internal ports to different external subdomains. I’m using nginx to reverse proxy. Everything is working as expected when run locally.

e.g. myapp.com should map to internal port 8000, api.myapp.com should map to internal port 8001.

What I see instead is that only one of the 2 web services is receiving requests, whichever comes first in my fly.toml (i.e. both api.myapp.com and myapp.com resolve to the same service), when deployed to fly. Otherwise the services are running as expected.

I have a fly.toml like (shortened):

# this doesn't work
app = "myapp"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[[services]]
  http_checks = []
  internal_port = 8000
  protocol = "tcp"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

[[services]]
  http_checks = []
  internal_port = 8001
  protocol = "tcp"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

An nginx configuration:

server {
    server_name myapp.com;
    listen 80;
    listen [::]:80;

    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

server {
    server_name api.myapp.com;
    listen 80;
    listen [::]:80;
    
    location / {
        proxy_pass http://localhost:8001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Alongside a Dockerfile which runs the processes and nginx.

I suspect something with fly internal routing configuration might be causing some unspoken behavior here. I’ve tried many things – going through fly docs, configuring DNS, nginx, and debugging my local web services, which all seems to be configured and running OK.

EDIT: Here is the fixed fly.toml for future viewers

# this works
app = "myapp"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[[services]]
  http_checks = []
  internal_port = 80
  protocol = "tcp"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

It seems to me like your fly.toml should have a single service with internal port 80 (on which nginx is listening). The redirection to ports 8000/8001 is done entirely by the nginx configuration.

2 Likes

Brilliant. Thank you for catching this and helping me understand!
(updated the OP to include solution)