【问题标题】:Why is nginx returning a 502 when doing a `uwsgi_pass` to a linked docker container?为什么 nginx 在对链接的 docker 容器执行“uwsgi_pass”时返回 502?
【发布时间】:2016-04-16 00:32:53
【问题描述】:

我正在使用 Docker Compose 编排由 Django webapp 和 nginx 反向代理组成的多容器应用程序。

我正盯着一个简单的测试用例,但我已经遇到了障碍。应用程序应该通过网络套接字 (frontend:8000) 以 uwsgi_pass 的方式将所有对 / 的请求传递给 Django 应用程序。

但是,在使用docker-compose up 启动应用程序并且没有看到任何错误消息后,对/ 的任何请求都会在控制台中产生以下错误消息:gateway_1 | 2016/01/11 15:45:12 [error] 8#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.99.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://172.17.0.2:8000", host: "192.168.99.100:8000"

我的问题如下:问题可能是什么?用content_by_lua_file path/to/file.lua; 替换location 块中的uwsgi_pass frontend; 行为符合预期,所以我怀疑uWSGI 在容器链接上存在问题,但我不知道下一步该去哪里。

以下是相关文件:

Docker Compose:30,000 英尺视图

docker-compose.yml文件如下:

postgres:
    image: mystuff/app.testdb:latest
    expose:
        - "5432"
frontend:
    image: mystuff/app.frontend:latest
    expose:
        - "8000"
    environment:
        APP_DBCONN: "user=xxx dbname=xxx port=5432 host=postgres sslmode=require password=xxx"
        APP_ENV: "test"
gateway:
    image: mystuff/app.gateway:latest
    links:
        - frontend
    expose:
        - "8000"
    ports:
        - "8000:8000"

NGINX:反向代理

下面是我的nginx.conf文件:

worker_processes 1;
user me;

events {
    use epoll;
    worker_connections 1024;
}

http {
    access_log /dev/stdout;

    upstream frontend {
        server frontend:8000;  # assumption:  `frontend` is a known hostname thanks to docker-compose
    }

    server {
        listen 8000;
        server_name localhost;

        location / {
            uwsgi_pass frontend;
            include uwsgi_params;
        }
    }
}

最后,这是我的uwsgi_params 文件:

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

uWSGI & Django:应用服务器

uwsgi.ini:

[uwsgi]
chdir = /home/app
wsgi-file = ./NFC/wsgi.py
socket = 127.0.0.1:8000
master = true
processes = 1
threads = 2
uid = me

编辑

1。在uwsgi.ini 中使用http = 127.0.0.1:8000 记录输出

$ cat /tmp/uwsgi.log 
*** Starting uWSGI 2.0.12 (64bit) on [Wed Jan 13 12:09:44 2016] ***
compiled with version: 4.9.2 on 03 January 2016 21:09:04
os: Linux-4.1.13-boot2docker #1 SMP Fri Nov 20 19:05:50 UTC 2015
nodename: default
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /home/srg
detected binary path: /home/srg/.pyenv/versions/2.7.11/bin/uwsgi
chdir() to /home/srg
your processes number limit is 1048576
your memory page size is 4096 bytes
detected max file descriptor number: 1048576
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on 127.0.0.1:8000 fd 7
uwsgi socket 0 bound to TCP address 127.0.0.1:38922 (port auto-assigned) fd 6
Python version: 2.7.11 (default, Jan  3 2016, 21:07:12)  [GCC 4.9.2]
Python main interpreter initialized at 0x1d37300
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 166144 bytes (162 KB) for 2 cores
*** Operational MODE: threaded ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1d37300 pid: 173 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 173)
spawned uWSGI worker 1 (pid: 210, cores: 2)
spawned uWSGI http 1 (pid: 211)
SIGINT/SIGQUIT received...killing workers...
gateway "uWSGI http 1" has been buried (pid: 211)
worker 1 buried after 1 seconds
goodbye to uWSGI.

【问题讨论】:

  • 尝试在 uWSGI 文件中设置http = 127.0.0.1:8000 并直接从浏览器连接到该套接字。
  • @GwynBleidD 这应该替换socket 行吗?
  • 是的,因为它使用相同的端口。但是您可以使用不同的端口并保留两者。
  • @GwynBleidD 对不起,我应该更清楚...我可以将socket 设置为不同的端口设置http,还是这两个条目相互不兼容?
  • 正如我所说,如果它们在不同的端口上,您可以同时使用它们。

标签: django nginx docker uwsgi docker-compose


【解决方案1】:

使用 uWSGI 配置,尤其是:

socket = 127.0.0.1:8000

uWSGI 将只允许本地连接(这意味着来自同一个 docker,而不是来自主机或其他 docker)。要允许来自 docker 外部的连接,您必须将其更改为:

socket = :8000

【讨论】:

    【解决方案2】:

    尝试将以下属性添加到您的 .ini 配置中。

    chmod-socket = 666
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-09
      • 2020-12-15
      • 2019-08-07
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      相关资源
      最近更新 更多