gpt4 book ai didi

apache - 与上游连接时拒绝连接-Docker

转载 作者:行者123 更新时间:2023-12-02 18:20:10 26 4
gpt4 key购买 nike

我得到的错误:

nginx_prod_vet | 2019/03/07 20:57:11 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, server: , request: "GET /backend HTTP/1.1", upstream: "http://172.23.0.2:81/backend", host: "localhost:90"



我的目标是使用nginx作为反向代理来传递前端文件并将其他服务代理到前端,因此可以从localhost:90 /调用localhost:90 / backend。

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

以下是最相关的文件:
# 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.

最佳答案

docker-compose exec backend netstat -lnpt向我们显示服务backend的httpd Web服务器正在侦听80端口而不是81

因此,很可能您的Dockerfile apache/Dockerfile在尝试提供自定义httpd配置apache/apache.conf方面是错误的。

要进一步调查:

  • 确保主要的apache conf内容符合您的期望:docker-compose exec backend cat /usr/local/apache2/conf/httpd.conf
  • 检查您的后端服务日志:docker-compose logs backend

  • 这样做,您将意识到您在主apache配置文件中缺少 Listen 81指令。您可以在 apache/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项目中定义的服务之间的端口冲突。

    关于apache - 与上游连接时拒绝连接-Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55052784/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com