node.js - 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并且无法连接到后端。
解决方案
推荐阅读
- powershell - 路径中的非法字符在哪里?
- python - 如何在 Python Turtle 中覆盖和重写文本?
- javascript - 在 Javascript 中使用 this.variable 作为默认参数有什么问题吗?
- docker - Pod 每次重启都更快
- r - 如何将 JSON 数据转换为 R
- android - Android-证书固定与公钥固定
- c# - OpenTracing 不使用 Serilog 发送日志
- php - 本地 ubuntu 设备如何监听部署在远程服务器上的 Laravel 应用程序的事件
- batch-file - 如何创建一个批处理文件,将另一个批处理文件放在启动文件夹中
- rust - 我可以循环使用 itertools::PutBack::put_back 吗?