当后端在Docker容器中时,Sveltekit SSR fetch()

发布于 2025-02-12 06:46:57 字数 1969 浏览 2 评论 0 原文

我在项目中使用Docker撰写。它包括以下容器:

  1. Nginx
  2. PostgreSQL
  3. 后端(Node.js)
  4. 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://来发送 Server side 请求到 Docker Container api.localhost/articles 作为URL?我还希望其他容器可以通过 http:// backend:8080 来访问我的容器。

I use docker compose for my project. It includes these containers:

  1. Nginx
  2. PostgreSQL
  3. Backend (Node.js)
  4. Frontend (SvelteKit)

I use SvelteKit's Load function to send request to my backend. In short, it sends http request to the backend container either on client-side or server-side. Which means, the request can be send not only by browser but also by container itself.

I can't get it to work on both client-side and server-side fetch. Only one of them is working.

I tried these URLs:

  • http://api.localhost/articles (only client-side request works)
  • http://api.host.docker.internal/articles (only server-side request works)
  • http://backend:8080/articles (only server-side request works)

I get this error:

From SvelteKit:
FetchError: request to http://api.localhost/articles failed, reason: connect ECONNREFUSED 127.0.0.1:80

From Nginx:
Timeout error

Docker-compose.yml file:

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

How can I send server-side request to docker container by using http://api.localhost/articles as URL? I also want my container to be accesible by other containers as http://backend:8080 if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渔村楼浪 2025-02-19 06:46:59

使用sveltekit的externalfetch挂钩在前端和后端中具有不同的和覆盖的API url。

在docker-compose中,如果它们在同一docker网络中,容器应该能够通过名称相互访问。

您的前端Docker SSR应该能够使用URL来调用后端docker:

http://backend:8080

Web浏览器应该能够通过使用URL来调用后端:

(whatever reads in your Nginx configuration files)

自然,这有很多原因可能会失败。解决此问题的最佳方法是,使用卷曲并将地址输入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:

http://backend:8080

Web browser should be able to call your backend by using the URL:

(whatever reads in your Nginx configuration files)

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.

棒棒糖 2025-02-19 06:46:59

从Docker组合中,您可以使用DNS(您在Compose文件中给出的服务名称)从一个容器中卷曲,

CURL -XGET backend:8080

您也可以通过在主机驱动程序网络上运行所有这些容器来实现此目标。

关于 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)

CURL -XGET backend:8080

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文