Trouble setting up a reverse proxy with nginx
tiltowait
HOBBYOP

a year ago

I'm trying to set up a reverse proxy to two different web services I have running in Railway (same project). It's going to be very simple (linking to two different services) but is starting extremely simple (linking to a single service).

Here is my nginx.conf.

server {
    listen 80;
    server_name reverse-proxy-beta.up.railway.app;

    location / {
        proxy_pass http://docs/;
        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;
    }
}

I'm using the internal networking name for the service in this example, but I've also tried the externally available https URL that works if I visit it directly.

For completeness, my Dockerfile:

FROM nginx:alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Project ID: 12fe90d4-42ca-4704-980b-e16da59696c0

Solved

25 Replies

a year ago

What if you used caddy instead?


a year ago

there's a ready to deploy template for it
https://railway.com/template/7uDSyj


tiltowait
HOBBYOP

a year ago

I'll take a look! Not married to nginx


tiltowait
HOBBYOP

a year ago

So I gave it 8080 for both the frontend and backend domains and the private networking names for each (docs and web).

I don't think it's working. If navigate to the new domain (caddy-proxy-beta.up.railway.app), I get a blank page. If I try navigating to /docs or /web, I get a blank page on those as well


a year ago

yeah you'd want to use caddy here, nginx will cache the DNS for it's lifetime and that's a problem because the DNS will change every time you redeploy the upstream


tiltowait
HOBBYOP

a year ago

Got it


tiltowait
HOBBYOP

a year ago

Still struggling with Caddy. Since I had no luck with the template, I tried setting one up with Docker. Here's my Caddyfile.

{
  admin off
  auto_https off
}

reverse-proxy-beta.up.railway.app {
  encode gzip

  # Handle /docs path
  handle /docs* {
    uri strip_prefix /docs
    reverse_proxy docs:8080
  }

  # Handle root path
  handle /* {
    reverse_proxy web:8080
  }
}

When I try to visit https://reverse-proxy-beta.up.railway.app/wizard, which is a path in the web service, I get a Railway "application failed to respond" page. Checking the web service's logs, I see no traffic. It does say it's listening on 8080.

(I have never even heard of Caddy today, so I'm probably doing something obviously incorrect!)


tiltowait
HOBBYOP

a year ago

If I omit auto_https off, then Caddy complains about not being able to get a certificate. I figured that, since Railway takes care of that, I should turn it off


a year ago

is the web service listening on IPv6?


tiltowait
HOBBYOP

a year ago

Should be. I have server: { host: '::' }


tiltowait
HOBBYOP

a year ago

It's an astro site I deployed using the Railway guide.

Here's from the deploy logs:
23:58:49 [@astrojs/node] Server listening on local: http://localhost:8080 network: http://10.250.19.9:8080
IIRC localhost can be IPv6?


tiltowait
HOBBYOP

a year ago


tiltowait
HOBBYOP

a year ago

CLI deployment, not dockerfile


tiltowait
HOBBYOP

a year ago

… do I need to turn off public networking?


a year ago

nope, but localhost is generally not ipv6


tiltowait
HOBBYOP

a year ago

Hmm, so I ran npm run build, then npm run start. From what I can see, it's using the same entrypoint as shown in the doc I linked.

I'm able to see the site if I navigate to http://[::1]:4321


tiltowait
HOBBYOP

a year ago

I'm also able to curl -6 http://localhost:4321


a year ago

how familiar are you with the dev tools of your browser?


tiltowait
HOBBYOP

a year ago

Reasonably familiar, but I give up, lol. I'm just going to use a subdomain


a year ago

subdomains are a good choice


tiltowait
HOBBYOP

a year ago

Yeah. Sadly I now have to fix a slew of links and DNS 🥲


a year ago

easy work!


tiltowait
HOBBYOP

a year ago

Easy, just annoying. lol. Thanks for trying to help me nail it down


a year ago

no problem!


a year ago

!s


Status changed to Solved brody 11 months ago


Loading...