Docker nginx proxy with Solid Start
Hi I am kind of new to reverse proxy. I have 3 containers frontend, backend and nginx.
My nginx config
I want to use it like this inside solid app:
my proxy setup:
Errors I get:
It seems like it does not know about existance of those api routes.
This is how i use it :
Can someone help me with it?
It works on the server side with ssr with (
http://backend:[backend_port]/api/time) but does not work on the client side.38 Replies
Have you tried simply
?
relevant docs:
https://vite.dev/config/server-options.html#server-proxy
https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/middlewares/proxy.ts#L13
https://github.com/sagemathinc/http-proxy-3#options
but in your case I think the simple oneliner above should work
I think I did but i will try again
Nope does not work
hmm your frontend is also behind nginx
what is actually running on localhost and what is remote?
also, how do you start the frontend?
actually thinking about it again, your nginx should already handle the /api path without needing the vite config
Yup I thought the same.
Have you ever setup something like this before?
I mean maybe there is a way better solution
yes
what do you have running on local, and what is remote, and why do you need nginx?
I mean, I need to know what you are trying to do
just getting development environement setup
or trying to setup for production
setup for production env
I have
frontend, backend and nginx containers working together in docker-composeso if you access your frontend at
website.com
and you can access your backend api at
website.com/api/time
then any client code making a request to
website.com/api/time or /api/time should also work
it isn't a solidjs or solidstart issue and doesn't need the proxy config in the app.config.ts, actually maybe you should remove it
and let nginx handle it. Or rather you must, cause letting /api/time go to website.com/api/time is what you want cause nginx will proxy it to the backend for you
if website.com/api/time doesn't work, then it is an nginx config issueyeah i still testing it in my local host
hmm
But I use
http://backend:[backendport]/apiyeah, in localhost can also be confusing cause everything is localhost
also I'm not completely sure how to reference other docker compose services inside nginx config that is in a docker compose
but as first step if you have eg. nginx at
localhost:3333
you have front end on port 3000
backend on port 3200
test so that localhost:3333 shows front end in your browser
and localhost:3333/api/time shows backend api result in your browser
if you have both of those working, then the front end js code should be able to call "/api/time"
I mean i exposed the port only to inernal docker containers
I mean the backend
right, so when accessing the nginx port you should get the proxied output
btw, I'm not sure if order of nginx blocks has an effect, maybe you should put the
location /api/ block firstwhen i exposed it to the host i for example:
http://localhost:[backend_port]/api/helloworld
I get hello world as expectedright, next you need to get the nginx config right for it to be acessible through http://localhost:[nginxport]/api/helloworld
I will tell ya more in a few minutes
And maybe I will provide some errors
OK, after some reasearch, you need
OR Notice the trailing slashes carefully. otherwise your requests to localhost:[nginxport]/api/helloworld would get stripped to backend:[backendport]/helloworld
OR Notice the trailing slashes carefully. otherwise your requests to localhost:[nginxport]/api/helloworld would get stripped to backend:[backendport]/helloworld
Ok I am back so let's begin from the starting point.
I am working on a frontend admin panel in local network for a company,
I would like to put it into external hosting provider, but they want the database data on their office server so I choose docker for that.
I made
backend, db_service, frontend, nginx and backup_service containers
So I have everything setup in the compose already and it works very well
this function works but for example this function does not work:
I get: Failed to load resource: net::ERR_NAME_NOT_RESOLVED
maybe this one above works cuz it is marked as server function?
Source:
how do you currently open the frontend in your browser? what does the url look like?
you are trying to get the web client to fetch:
http://backend:[backendport]/api/employees/${params.id}
if the same url doesn't work if you input it into the address bar of your browser, then it won't work with fetch
you need to config nginx to be able to access the backend from your browser
or you could expose the backend directly without going through nginx
whichever way, you need a url that works in your browser to access the backend apiyup
then when it works in your browser you can put the same looking url into the fetch
I could show you my config
please take a good look at this too
my config file
ok, the
/api/ and proxy_pass http://backend:[backendport]/api/; should make the url correct
I'm not sure about the $host part
it might depend on if your backend is listening to all hosts
to make sure that's not an issue have the backend listen to 0.0.0.0yup it listens on 0.0.0.0
ok, now how do you open the front end in your browser
please answer
by opening localhost without any port
nope
what happens
do you get anything in the frontend or backend logs?
yup
I get ERR_CONNECTION_REFUSED
in which log?
lemme check
ok now i can access it
I forgot that i composed it down
my bad
alright!

so now you can change
const response = await fetch(
http://backend:[backendport]/api/employees/${params.id},
to
const response = await fetch(
http://localhost/api/employees/${params.id},
and it should work
and when deploying it to production, change the localhost to whatever it is supposed to be (the same url that you access the frontend)
Oh, actually, you can change it to
hmm I get an error like this:
all in production build btw
It does not seems to find /api route
hmm
strange
Hmm.. I haven't used fetch a lot, looks like it needs absolute urls
yeah but it would be trash to hardcode it tbh