cookie-cutter django nginx connect() 在连接到上游时失败(111:连接被拒绝),

Posted

技术标签:

【中文标题】cookie-cutter django nginx connect() 在连接到上游时失败(111:连接被拒绝),【英文标题】:cookie-cutter django nginx connect() failed (111: Connection refused) while connecting to upstream, 【发布时间】:2017-07-08 06:09:03 【问题描述】:

您好,我是这个项目的新手,在 CentOS7 ec2 实例上托管它时遇到问题。 我在访问我的域时收到此错误:

2017/02/17 05:53:35 [error] 27#27: *20 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server:myApp.io, request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.18.0.7:5000/favicon.ico", host: "myApp.io", referrer: "https://myApp.io"

当我查看日志时

docker logs d381b6d093fa
sleep 5
build starting nginx config
replacing ___my.example.com___/myApp.io
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events 
  worker_connections  1024;


http 
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  sendfile        on;
  #tcp_nopush     on;

  keepalive_timeout  65;

  #gzip  on;

  upstream app 
    server django:5000;
  

  server 
    listen 80;
    charset     utf-8;


    server_name myApp.io ;

    location /.well-known/acme-challenge 
      proxy_pass http://certbot:80;
      proxy_set_header Host            $host;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header X-Forwarded-Proto https;
    


    location / 
      # checks for static file, if not found proxy to app
      try_files $uri @proxy_to_app;
    

    # cookiecutter-django app
    location @proxy_to_app 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass   http://app;

    
  


.
Firing up nginx in the background.
Waiting for folder /etc/letsencrypt/live/myApp.io to exist
replacing ___my.example.com___/myApp.io
replacing ___NAMESERVER___/127.0.0.11
I made sure to add my ip address to the env file for allowed hosts.

当我查看正在运行的容器时,我得到:

docker ps -a
CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS                     PORTS                                      NAMES
3887c3465802        myApp_nginx                     "/bin/sh -c /start.sh"   3 minutes ago       Up 3 minutes               0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   myApp_nginx_1
91cbc2a2359d        myApp_django                    "/entrypoint.sh /g..."   3 minutes ago       Up 3 minutes                                                          myApp_django_1

我的 docker-compose.yml 看起来像:

version: '2'

volumes:
  postgres_data: 
  postgres_backup: 

services:
  postgres:
    build: ./compose/postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - postgres_backup:/backups
    env_file: .env

  django:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile
    user: django
    depends_on:
      - postgres
      - redis
    command: /gunicorn.sh
    env_file: .env

  nginx:
    build: ./compose/nginx
    depends_on:
      - django

      - certbot

    ports:
      - "0.0.0.0:80:80"

    environment:
      - MY_DOMAIN_NAME=myApp.io
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"
    volumes:
      - /etc/letsencrypt:/etc/letsencrypt
      - /var/lib/letsencrypt:/var/lib/letsencrypt

  certbot:
    image: quay.io/letsencrypt/letsencrypt
    command: bash -c "sleep 6 && certbot certonly -n --standalone -d myApp.io --text --agree-tos --email morozovsdenis@gmail.com --server https://acme-v01.api.letsencrypt.org/directory --rsa-key-size 4096 --verbose --keep-until-expiring --standalone-supported-challenges http-01"
    entrypoint: ""
    volumes:
      - /etc/letsencrypt:/etc/letsencrypt
      - /var/lib/letsencrypt:/var/lib/letsencrypt
    ports:
      - "80"
      - "443"
    environment:
      - TERM=xterm


  redis:
    image: redis:latest

  celeryworker:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile
    user: django
    env_file: .env
    depends_on:
     - postgres
     - redis
    command: celery -A myApp.taskapp worker -l INFO

  celerybeat:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile
    user: django
    env_file: .env
    depends_on:
      - postgres
      - redis
    command: celery -A myApp.taskapp beat -l INFO

我的 .env 文件具有正确的允许主机,即我的 ec2-instance ip 地址

知道我做错了什么吗?

【问题讨论】:

【参考方案1】:

几个月前我遇到了同样的问题。请看一下这个答案: Problem with SELinux。它像魅力一样帮助了我:)

【讨论】:

谢谢,但我认为这与我遇到的问题不同。我尝试了解决方案,但没有奏效。我收到错误代码 111,该解决方案适用于错误代码 13。

以上是关于cookie-cutter django nginx connect() 在连接到上游时失败(111:连接被拒绝),的主要内容,如果未能解决你的问题,请参考以下文章

Nginx之美多商城前台部署

Ngins 配置常用八大实例

ubuntu Ngin Install

shell编写 ngin启动脚本

ngin隐藏版本号

ngin负载均衡集群