我有一个带有 strapi、nextjs 和 nginx 的 docker 容器。我已经设置好了,如果我导航到 front.development 我会点击下一个前端,如果我去 back.development 我会点击strapi后端。
我可以向back.development/articles/some-article-title
邮递员提出请求,它可以工作。但是,如果我在 nextjs 中向同一个 url 发出 fetch 或 axios 请求,我会得到Error: connect ECONNREFUSED 127.0.0.1:80
.
我被困在如何解决这个问题上。我在这里的一个类似问题中阅读了解决方案 Can't call my Laravel API from the Node.js container 但我可以从 Postman 调用它,但尝试该解决方案会导致 404。
码头工人-compose.yml
version: "3"
services:
# NGINX reverse proxy
nginx:
image: nginx:1.17.10
container_name: nginx_reverse_proxy
restart: unless-stopped
depends_on:
- frontend
- strapi
volumes:
- ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
# NextJS Front end
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
container_name: frontend
restart: unless-stopped
volumes:
- ./frontend:/srv/frontend
- /srv/frontend/node_modules
- /srv/frontend/.next
ports:
- 3000:3000
# Strapi CMS
strapi:
image: strapi/strapi
container_name: strapi
restart: unless-stopped
env_file: .env
environment:
DATABASE_CLIENT: ${DATABASE_CLIENT}
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_HOST: ${DATABASE_HOST}
DATABASE_PORT: ${DATABASE_PORT}
DATABASE_USERNAME: ${DATABASE_USERNAME}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
volumes:
- ./app:/srv/app
ports:
- 1337:1337
# MongoDB database
db:
image: mongo
container_name: db
restart: unless-stopped
env_file: .env
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
volumes:
- strapidata:/data/db
ports:
- 27017:27017
networks:
wellington-network:
driver: bridge
volumes:
strapidata:
Nginx 配置
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name front.development;
location / {
proxy_pass http://frontend:3000;
proxy_pass_request_headers on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
}
server {
listen 80;
server_name back.development;
location / {
proxy_pass http://strapi:1337;
proxy_pass_request_headers on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
}
}