首页 > 解决方案 > docker error 错误 fetch() 中的错误:使用 nuxt 时连接 ECONNREFUSED 并连接到 node.js

问题描述

对我的项目进行 dockerizing 时遇到问题。我使用 node.js 作为后端,使用 nuxt 作为前端。

我的问题是无法从前端连接到后端并给出此错误: 在此处输入图像描述

码头工人撰写代码

version: "3.8"
services:
  backend:
    build: ./backend
    container_name: backend
    restart: always
    ports:
      - 4000:4000
    external_links:
      - "mongodb:mongodb"
    volumes:
      - ./backend/uploads:/backend/uploads
    networks:
      - default
      - backend

  mongodb:
    image: mongo:latest
    restart: always
    container_name: mongo
    ports:
      - 27021:27017
    volumes:
      - ./data/db:/data/db
    networks:
      - default
      - backend

  frontend:
    image: node:alpine
    environment:
      - NUXT_HOST=0.0.0.0
      - NUXT_PORT=3000
    container_name: frontend
    volumes:
      - ./frontend:/frontend
    working_dir: /frontend
    restart: always
    ports:
      - 3000:3000
    command: npm run dev
    external_links:
      - "server:server"
      - "mongodb:mongodb"
    networks:
      - default
      - backend

  nginx:
    image: nginx
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./config/nginx.conf:/etc/nginx/nginx.conf
      - ./config:/etc/other
    links:
      - backend
      - frontend
    depends_on:
      - backend
      - frontend
    networks:
      - default
      - backend

networks:
  backend:
    external: true

nginx/conf.d

user nginx;
worker_processes auto;
pid /var/run/nginx.pid;


events {
    worker_connections 1024;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 20m;
    # server_tokens off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##
    gzip on;

    server {
        listen 443;

        server_name location; # 域名

        # 开启https,把下面ssl的全部取消注释

        # ssl on;

        # ssl_certificate /etc/other/location.pem;      # https证书(统一前缀/etc/other/) 后面为文件名
        # ssl_certificate_key /etc/other/location.key;   # 全部上传到根目录的config文件夹即可(自动配置)

        # ssl_session_timeout 5m;
        # ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        # ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        # ssl_prefer_server_ciphers on;


        location / {
            proxy_pass http://localhost:3000;
        }

        location /backend {
            proxy_pass http://localhost:4000;
        }

        location ~* \.(txt)$ {
            root /etc/other/;
        }

        location ~* \.(html)$ {
            root /etc/other/;
        }
    }
    server {
        listen 80;

        server_name location;   # 域名

        # rewrite ^(.*)$ https://$server_name$1 permanent; # 开启https

        location / {
            proxy_pass http://localhost:3000;
        }

        location /backend {
            proxy_pass http://localhost:4000;
        }

        location ~* \.(txt)$ {
            root /etc/other/;
        }

        location ~* \.(html)$ {
            root /etc/other/;
        }
    }
}

后端代码运行在http://127.0.0.1:4000 whtiout 问题并且可以连接到 mongo db 但前端运行http://172.25.0.3:3000并且无法连接到后端。

标签: node.jsdockernginxnuxt.js

解决方案


推荐阅读