英文:
Connection refused nginx proxy for Docker containers
问题
I'm trying to set up an nginx proxy pass between two docker containers. My frontend container seems to be working fine, but I'm getting a 502 error when trying to access the backend. I have two docker-compose files:
docker-compose.yml
version: "3.1"
services:
backend:
image: backend_image
container_name: backend_container
frontend:
container_name: frontend_container
image: frontend_image
proxy:
container_name: proxy_container
image: nginx
restart: unless-stopped
ports:
- "8080:80"
depends_on:
- website
- frontend
docker-compose-development.yml
version: "3.1"
services:
backend:
container_name: backend_container_development
frontend:
build:
context: ./site/public_html/frontend
target: runner
volumes:
- ./site/public_html/frontend:/app
command: npm run dev
ports:
- "3000:80"
environment:
NODE_ENV: development
proxy:
volumes:
- ./nginx/local.conf:/etc/nginx/nginx.conf
- ./logs:/var/logs
and my nginx local.conf file:
events {
worker_connections 1024;
}
http {
server {
listen 80 default_server;
location / {
proxy_pass http://frontend:3000;
error_log /var/log/frontend_errors.log;
}
location /api {
proxy_pass http://backend:8000;
error_log /var/log/backend_errors.log;
}
}
}
This is the error that I'm receiving
[error] 29#29: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.48.1, server: , request: "GET /api HTTP/1.1", upstream: "http://192.168.48.3:8000/api", host: "localhost:8080"
I've verified that the IP for the backend and frontend container is 192.168.48.3 and that the containers run separately on localhost:8000 and localhost:3000.
英文:
I'm trying to set up an nginx proxy pass between two docker containers. My frontend container seems to be working fine, but I'm getting a 502 error when trying to access the backend. I have two docker-compose files:
docker-compose.yml
version: "3.1"
services:
backend:
image: backend_image
container_name: backend_container
frontend:
container_name: frontend_container
image: frontend_image
proxy:
container_name: proxy_container
image: nginx
restart: unless-stopped
ports:
- "8080:80"
depends_on:
- website
- frontend
docker-compose-development.yml
version: "3.1"
services:
backend:
container_name: backend_container_development
frontend:
build:
context: ./site/public_html/frontend
target: runner
volumes:
- ./site/public_html/frontend:/app
command: npm run dev
ports:
- "3000:80"
environment:
NODE_ENV: development
proxy:
volumes:
- ./nginx/local.conf:/etc/nginx/nginx.conf
- ./logs:/var/logs
and my nginx local.conf file:
events {
worker_connections 1024;
}
http {
server {
listen 80 default_server;
location / {
proxy_pass http://frontend:3000;
error_log /var/log/frontend_errors.log;
}
location /api {
proxy_pass http://backend:8000;
error_log /var/log/backend_errors.log;
}
}
}
This is the error that I'm receiving
[error] 29#29: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.48.1, server: , request: "GET /api HTTP/1.1", upstream: "http://192.168.48.3:8000/api", host: "localhost:8080"
I've verified that the IP for the backend and frontend container is 192.168.48.3 and that the container run separately on localhost:8000 and localhost:3000
答案1
得分: 1
使用相同的问题今天,应该使用容器的 "inner" 端口,在你的情况下 :80
而不是 :3000
和 :8080
:
proxy_pass http://frontend:80;
...
proxy_pass http://backend:80;
英文:
Got the same issue today, should use container`s "inner" port, in your case :80
instead :3000
and :8080
:
proxy_pass http://frontend:80;
...
proxy_pass http://backend:80;
答案2
得分: 0
以下是翻译好的内容:
-
错误消息 "connect() failed (111: Connection refused)" 表示 Nginx 无法连接到 http://backend:8000 上的后端容器。以下是一些用于排除问题的步骤:
-
检查后端容器:通过运行 docker ps 来验证后端容器是否正在运行。如果没有运行,请使用 docker-compose up backend 启动它。
-
验证后端容器的端口:检查后端容器是否在端口 8000 上监听。您可以通过运行 docker-compose exec backend netstat -tlnp 来执行此操作,并验证端口 8000 是否在打开端口列表中。
-
检查网络连通性:确保前端和后端容器可以在网络上相互通信。您可以通过运行 docker-compose exec frontend ping backend 和 docker-compose exec backend ping frontend 来执行此操作。
-
验证 Nginx 配置:确保在 nginx.conf 文件的 location /api 块的 proxy_pass 指令中正确拼写了后端服务的名称。还要检查从代理容器运行 docker-compose exec proxy ping backend 是否能够访问后端容器。
-
检查后端容器日志:检查后端容器的日志是否包含可能说明其未响应的任何错误消息。您可以通过运行 docker-compose logs backend 来执行此操作。
-
验证后端容器的 IP 地址:验证后端容器是否在 Nginx 尝试连接的 IP 地址上监听。您可以通过运行 docker-compose exec backend netstat -tlnp | grep 8000 来执行此操作。
如果以上步骤都没有帮助,您可能需要提供更多信息,例如 Dockerfiles 的内容或前端容器日志中的任何错误消息。
英文:
The error message "connect() failed (111: Connection refused)" indicates that Nginx is unable to connect to the backend container at http://backend:8000. Here are some steps you can take to troubleshoot the issue:
-
Check the backend container: Verify that the backend container is running by running docker ps. If it is not running, start it with docker-compose up backend.
-
Verify the backend container's port: Check that the backend container is listening on port 8000. You can do this by running docker-compose exec backend netstat -tlnp and verifying that port 8000 is in the list of open ports.
-
Check network connectivity: Ensure that the frontend and backend containers can communicate with each other on the network. You can do this by running docker-compose exec frontend ping backend and docker-compose exec backend ping frontend.
-
Verify the Nginx configuration: Ensure that the backend service is spelled correctly in the proxy_pass directive in the location /api block of the nginx.conf file. Also, check that the backend container is reachable from the proxy container by running docker-compose exec proxy ping backend.
-
Check the backend container logs: Check the logs of the backend container for any error messages that may indicate why it is not responding. You can do this by running docker-compose logs backend.
-
Verify the backend container's IP address: Verify that the backend container is listening on the IP address that Nginx is trying to connect to. You can do this by running docker-compose exec backend netstat -tlnp | grep 8000.
If none of these steps help, you may need to provide more information such as the contents of the Dockerfiles or any error messages in the frontend container logs.
答案3
得分: 0
请设置代理传递到服务后端和前端的端口,之后使用 proxy_pass localhost:backend-port 和 proxy_pass localhost:backend-port。
docker-compose
services:
frontend:
ports:
- 3000:3000
nginx.conf
proxy_pass http://localhost:3000;
英文:
your must set ports for service backend and frontend after proxy_pass localhost:backend-port and proxy_pass localhost:backend-port
docker-compose
services:
frontend:
ports:
- 3000:3000
nginx.conf
proxy_pass http://localhost:3000;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论