Nginx with my web application

a year ago

Hello there! Been trying out a few different services to see where I want to host my web app.

Frontend: React
Backend: Golang

I've got the backend up and running without any issues. I'm having some trouble getting my frond end up and running. I've tried with/without a docker file. Currently with the dockerfile the application is building fine, but crashes.

Error:

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/03/12 22:28:26 [emerg] 7#7: host not found in upstream "my_server.railway.internal" in /etc/nginx/nginx.conf:3
nginx: [emerg] host not found in upstream "my_server.railway.internal" in /etc/nginx/nginx.conf:3
...

Dockerfile:

FROM node:14 as build
WORKDIR /client

COPY package.json .
RUN npm install
COPY . .
RUN npm run build

FROM nginx
COPY --from=build /client/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf

nginx.conf

http {
  server {
    server_name my_server.railway.internal;

    location / {
      root /usr/share/nginx/html; 
    }

    location /api/ {
      proxy_pass http://my_server.railway.internal;
    }
  }
}
events {}

From what I can tell from the docs and other support tickets something like this should be possible. Just not sure what I'm missing.

Thank you for any help!

0 Replies

a year ago

cc7cfdf3-8735-4236-8e74-0d7398ca6dc3


a year ago

create react app?


a year ago

Not sure I understand the question sorry. Yes, the front end is React


a year ago

was it made with create react app?


a year ago

or react scripts, however you wanna call it


a year ago

Ahhh, I beleve so. It's been a while since I initialized it.

Probably something like

npx create-react-app my-app --template typescript


a year ago

does the api endpoint have to live on the same domain as the frontend?


a year ago

No, but it's preferred.

I can do something like [api.my](api.my)_[domain.com](domain.com) if I have to, but I'd prefer my_[domain.com](domain.com)/api


a year ago

okay fair enough


a year ago

okay so let's get started


a year ago

show me a screenshot of your railway project so I know what we are working with


a year ago

Just this?

1217480700991639600


a year ago

yep that's it


a year ago

can I see a screenshot of the frontend project in github?


a year ago

just the files and stuff


a year ago

1217481812272615400


a year ago

looking good


a year ago

I'm gathering information so I can work out the best course of action


a year ago

does the client have a public domain?


a year ago

You mean like here? Not yet, but I do have a domain I will use

1217482410967695400


a year ago

generate one, just for testing



a year ago

delete the Dockerfile and nginx.conf from your repo.

copy the nixpacks.toml and Caddyfile from this repo into yours.

if you have any custom build or start commands set in the clients service settings, please remove them.


a year ago

after that, let me know the outcome


a year ago

if that works the next step will be to join the frontend and backend under one domain, but one step at a time


a year ago

Build failed

ERROR: failed to solve: process "/bin/bash -ol pipefail -c npm run build" did not complete successfully: exit code: 1
Error: Docker build failed

I'm assuming it's because there's a few warnings that I have not cleaned up yet.

#19 77.49 Treating warnings as errors because [process.env.CI](process.env.CI) = true.

I'm new to Caddyfile, is there somewhere I can update this so it ignores warnings for now?


a year ago

Caddyfile has nothing to do with building your site.

set a service variable CI to false


a year ago

Ahhh okay, will let that build.


a year ago

side note, please use this for sending me your build logs

the log snippet you sent didn't have any useful information.


a year ago

Okay, looks like the build worked and the app is live via the url I sent above.


a year ago

awesome, send another screenshot of the project please


a year ago

do you have a PORT service variable set on the server service?


a year ago

do you have a PORT service variable set on the server service?

In railway? no

1217487774676947000


a year ago

can you show me the server code that starts listening?


a year ago

err = http.ListenAndServe(appConfig.Application.SERVER_DOMAIN, r)
if err != nil {
    log.Fatal(err)
}

a year ago

what is the server domain set to


a year ago

Just the domain I want to use for the application


a year ago

please show me


a year ago

Show you how?

I have an env variable set up in railway that it picks up.


a year ago

screenshot please


a year ago

1217489251067887600


a year ago

show me the value


a year ago

on second thought, please use this code instead of what you currently have, that way we know it's correct

func main() {
  // ...
  // Use `PORT` provided in environment or default to 3001
  var port = envPortOr("3001")

  log.Fatal(http.ListenAndServe(port, handler))
  // ...
}

// Returns PORT from environment if found, defaults to
// value in `port` parameter otherwise. The returned port
// is prefixed with a `:`, e.g. `":3001"`.
func envPortOr(port string) string {
  // If `PORT` variable in environment exists, return it
  if envPort := os.Getenv("PORT"); envPort != "" {
    return ":" + envPort
  }
  // Otherwise, return the value of `port` variable from function argument
  return ":" + port
}

a year ago

I'm not going to need a domain on that?


a year ago

nope


a year ago

a domain isn't applicable here, and if you say it worked on railway I will be shocked


a year ago

but then again, for some reason, you never did tell me what you had it set to


a year ago

I had it set to my domain (with no port).


a year ago

again, that's not telling me what it's set to


a year ago

I don't understand the secrecy, but either way, please use the code I provided instead


a year ago

Just not a domain I wanted to share. I'm not sure why knowing the exact domain matters. Could be my_[domain.com](domain.com) and it wouldn't really make a difference.


a year ago

Updated the code. Waiting for it to build


a year ago

I understand that but details matter when dealing with these things, omitting details has dramatically increased the time it takes to resolve this and other help threads


a year ago

Okay, server built and is active.


a year ago

does it work?


a year ago

Do you mean does the client call it? No


a year ago

no, just the backend, does it work on its own?



a year ago

on the client service, set a PORT service variable to 3000
on the server service, set a PORT service variable to 3001


a year ago

Okay that's done

1217497129128427500


a year ago

go ahead and deploy the reverse proxy template into your project.

for the FRONTEND_HOST variable, use [http://${{Client.RAILWAY_PRIVATE_DOMAIN}}:${{Client.PORT](http://${{Client.RAILWAY_PRIVATE_DOMAIN}}:${{Client.PORT)}}

for the BACKEND_HOST variable, use [http://${{Server.RAILWAY_PRIVATE_DOMAIN}}:${{Server.PORT](http://${{Server.RAILWAY_PRIVATE_DOMAIN}}:${{Server.PORT)}}


a year ago

Deploying

What does the ENABLE_ALPINE_PRIVATE_NETWORKING env variable do? I'm not seeing it referenced in the reverse proxy code


a year ago

there was a hyperlink under it that linked to docs explaining what it does


a year ago

but for the end user, you can ignore it


a year ago

so now you can remove the public domains from the server and client services because they will no longer be accessed publicly. you now use the domain on the proxy service


a year ago

So I would just add my custom domain to the proxy service then? Instead of the client?


a year ago

correct


a year ago

Is this generally the expected deploy time for the proxy?

1217508805777166300


a year ago

not at all


a year ago

did you missconfigure something?


a year ago

Let me double check


a year ago

I noticed I was missing the http:// on the env variables. I added that, but it seems the health check is still failing on the server

1217515543897313300
1217515544467734500


a year ago

full screenshot of your project please


a year ago

1217518112597868800


a year ago

why is the health check failing



a year ago

you might need to make changes to the Caddyfile, please have a read of the comments in the Caddyfile


a year ago

This is the correct health check plan? It was what it was set to when I created the template.

1217525840833675300


a year ago

your backend does have an /api/ path right?


a year ago

No, just figured this out. I updated it to /api/ping. That fixed it.


a year ago

sorry but that's not the correct path you want to use


a year ago

okay now that's good


a year ago

so we in business?


a year ago

We are good!

Thank you so much for your help! Greatly appriciated!


a year ago

happy to help!