社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  docker

连接到upstram-Docker时连接被拒绝

fajuchem • 5 年前 • 1292 次点击  

nginx_prod|u vet| 2019/03/07 20:57:11[错误]6#6:*1 connect()失败 (111:连接被拒绝)连接到上游时,客户端: 172.23.0.1,服务器:,请求:“GET/backend HTTP/1.1”,上游: http://172.23.0.2:81/backend ,主机:“localhost:90”

我的目标是使用nginx作为反向代理将前端文件和其他服务代理到前端,这样就可以访问localhost:90/backend been call from localhost:90/。

我试图从容器外部访问后端,但它给了我上面的错误。

以下是最相关的文件:

# docker-compose.yml

version: '3'

services:

  nginx:
    container_name: nginx_prod_vet
    build:
      context: .
      dockerfile: nginx/prod/Dockerfile
    ports:
      - "90:80"
    volumes:
      - ./nginx/prod/prod.conf:/etc/nginx/nginx.conf:ro
    networks:
      - main
    depends_on:
      - backend

  backend:
    container_name: backend_prod_vet
    build:
        context: .
        dockerfile: apache/Dockerfile
    ports:
      - "81:81"
    networks:
      - main

networks:
  main:
    driver: bridge

# apache/Dockerfile
FROM httpd:2.4.32-alpine

RUN apk update; \
    apk upgrade;

# Copy apache vhost file to proxy php requests to php-fpm container
COPY apache/apache.conf /usr/local/apache2/conf/apache.conf
RUN echo "Include /usr/local/apache2/conf/apache.conf" \
>> /usr/local/apache2/conf/httpd.conf

# apache/apache.conf
ServerName localhost

LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so

<VirtualHost *:81>
    # Proxy .php requests to port 9000 of the php-fpm container
    # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
    DocumentRoot /var/www/html/
    <Directory /var/www/html/>
        # DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Send apache logs to stdout and stderr
    CustomLog /proc/self/fd/1 common
    ErrorLog /proc/self/fd/2
</VirtualHost>

# nginx/prod/prod.conf
user  nginx;
worker_processes  1;

events {
  worker_connections  1024;
}

http {
  include /etc/nginx/mime.types;
  client_max_body_size 100m;

  upstream backend {
    server backend:81;
  }


  server {
    listen 80;
    charset utf-8;

    root /dist/;
    index index.html;

    location /backend {
      proxy_redirect off;
      proxy_pass http://backend;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
    }


  }
}

# nginx/prod/Dockerfile
# build stage
FROM node:10.14.2-jessie as build-stage
WORKDIR /app/
COPY frontend/package.json /app/
RUN npm cache verify
RUN npm install
COPY frontend /app/
RUN npm run build

# production stage
FROM nginx:1.13.12-alpine as production-stage
COPY nginx/prod/prod.conf /etc/nginx/nginx.conf
COPY --from=build-stage /app/dist /dist/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

docker compose exec后端netstat-lnpt

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.11:38317        0.0.0.0:*               LISTEN      -
tcp        0      0 :::80                   :::*                    LISTEN      1/httpd

docker compose exec nginx sh-c“nc后端81&echo打开| | echo关闭”

closed.
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54208
 
1292 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Thomasleveil
Reply   •   1 楼
Thomasleveil    6 年前

docker-compose exec backend netstat -lnpt 向我们展示了用于服务的httpd web服务器 backend 正在监听端口 80 而不是 81

很可能,你的档案 apache/Dockerfile 它试图提供自定义httpd配置的方式不正确 apache/apache.conf .

进一步调查:

  • 确保apache conf的主要内容符合您的期望: docker-compose exec backend cat /usr/local/apache2/conf/httpd.conf
  • docker-compose logs backend

这样做,你就会意识到你错过了 Listen 81 阿帕奇/Dockerfile 文件:

# apache/Dockerfile
FROM httpd:2.4.32-alpine

RUN apk update; \
    apk upgrade;

# Copy apache vhost file to proxy php requests to php-fpm container
COPY apache/apache.conf /usr/local/apache2/conf/apache.conf

RUN echo "Listen 81" >> /usr/local/apache2/conf/httpd.conf
RUN echo "Include /usr/local/apache2/conf/apache.conf" >> /usr/local/apache2/conf/httpd.conf

为什么后端容器监听端口81?

使不同的容器打开不同的端口不会增加任何值。每个容器都有自己的IP地址,因此不需要避免docker compose项目中定义的服务之间的端口冲突。