当后端在Docker容器中时,Sveltekit SSR fetch()
我在项目中使用Docker撰写。它包括以下容器:
- Nginx
- PostgreSQL
- 后端(Node.js)
- Frontend(Sveltekit)
我使用Sveltekit的加载函数
将请求发送到我的后端。简而言之,它将 HTTP请求
发送到 Backend
客户端
或服务器端
上的容器。这意味着,该请求不仅可以由浏览器发送,而且可以通过容器本身发送。
我无法将其用于客户端和服务器端的获取。他们中只有一个正在工作。
我尝试了这些URL:
-
http://api.localhost/articles
(仅client> client
请求works) -
http://api.host.docker.internal /articles
(仅服务器端
请求工作) -
http://后端:8080/articles
(仅服务器端
请求工作)
我得到此错误:
来自 sveltekit
:
fetcherror:请求到http://api.localhost/articles失败,原因:连接econnrefuse 127.0.0.1:80
来自 nginx
:
超时错误
Docker-compose.yml
文件:
version: '3.8'
services:
webserver:
restart: unless-stopped
image: nginx:latest
ports:
- 80:80
- 443:443
depends_on:
- frontend
- backend
networks:
- webserver
volumes:
- ./webserver/nginx/conf/:/etc/nginx/conf.d/
- ./webserver/certbot/www:/var/www/certbot/:ro
- ./webserver/certbot/conf/:/etc/nginx/ssl/:ro
backend:
restart: unless-stopped
build:
context: ./backend
target: development
ports:
- 8080:8080
depends_on:
- db
networks:
- database
- webserver
volumes:
- ./backend:/app
frontend:
restart: unless-stopped
build:
context: ./frontend
target: development
ports:
- 3000:3000
depends_on:
- backend
networks:
- webserver
networks:
database:
driver: bridge
webserver:
driver: bridge
如何通过使用 http://来发送
作为URL?我还希望其他容器可以通过 Server side
请求到 Docker Container
api.localhost/articles http:// backend:8080
来访问我的容器。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用sveltekit的externalfetch挂钩在前端和后端中具有不同的和覆盖的API url。
在docker-compose中,如果它们在同一docker网络中,容器应该能够通过名称相互访问。
您的前端Docker SSR应该能够使用URL来调用后端docker:
Web浏览器应该能够通过使用URL来调用后端:
自然,这有很多原因可能会失败。解决此问题的最佳方法是,使用卷曲并将地址输入Web浏览器地址,一一一一测试URL,服务器。不可能回答其失败的确切原因,因为问题不包含足够的信息或通常可重复的问题。
有关更多信息,这是我们用于Dockerisise sveltekit Frontend 的示例配置 。 和配置变量。 这是我们的外部效果示例。
Use SvelteKit's externalFetch hook to have a different and overridden API URL in frontend and backend.
In docker-compose, the containers should be able to access each other by name if they are in the same Docker network.
Your frontend docker SSR should be able to call your backend docker by using the URL:
Web browser should be able to call your backend by using the URL:
Naturally, there are many reasons why this could fail. The best way to tackle this is to test URLs one by one, server by server using curl and entering addresses to the web browser address. It's not possible to answer the exact reason why it fails, because the question does not contain enough information, or generally repeatable recipe for the issue.
For further information, here is our sample configuration for a dockerised SvelteKit frontend. The internal backend shortcut is defined using hooks and configuration variables. Here is our externalFetch example.
从Docker组合中,您可以使用DNS(您在Compose文件中给出的服务名称)从一个容器中卷曲,
您也可以通过在主机驱动程序网络上运行所有这些容器来实现此目标。
关于 http://api.localhost/articles
代码>
并指定您希望计算机尝试与此URL进行通信的IP:
http://api.localhost/articles
使用。From a docker compose you will be able to CURL from one container using the dns (service name you gave in the compose file)
You can achieve this also by running all of these containers on host driver network.
Regarding the http://api.localhost/articles
You can change the
/etc/hosts
And specify the IP you want your computer to try to communicate with when this url :
http://api.localhost/articles
is used.