V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lbfeng
V2EX  ›  问与答

docker uwsgi container 和本地 nginx 无法连通,返回 502

  •  
  •   lbfeng · 2019-08-24 09:57:46 +08:00 · 1401 次点击
    这是一个创建于 1699 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本来是想 nginx 和 uwsgi 各自放一个 container. 通过 volume 映射 unix socket.但目前 uwsgi container 和本地的 nginx 都跑不通。。。同样的 uwsgi.ini 如果在本地直接运行则没有任何问题。

    nginx 一直是 61 connection refused

    [error] 12033#0: *1 connect() to unix:/tmp/uwsgi.sock failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi.sock:", host: "localhost"

    socket 在本地的映射(container volume 映射和本地直接运行 uwsgi 完全一样)

    srw-rw-r-- 1 username wheel 0B Aug 23 21:43 uwsgi.sock

    uwsgi.ini

    [uwsgi]
    module = app:app
    strict = true
    
    master = true
    processes = 4
    
    socket = /tmp/uwsgi.sock
    chmod-socket = 664
    vacuum = true
    
    enable-threads = false
    single-interpreter = true
    need-app = true
    die-on-term = true
    max-requests = 1000
    max-worker-lifetime = 3600
    reload-on-rss = 2048
    worker-reload-mercy = 60
    

    app . py

    from flask import Flask
    
    def create_app():
        app = Flask(__name__)
    
        @app.route('/')
        def hello():
            return '<h1>Hello There!</h1>'
    
        return app
    
    app = create_app()
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0')
    

    Dockerfile

    FROM python:3.7-slim
    
    RUN apt-get update && apt-get -y install python3-dev build-essential \
    libpcre3 libpcre3-dev
    
    COPY . /src
    WORKDIR /src
    
    RUN pip install flask uwsgi
    
    ENTRYPOINT ["./run.sh"]
    

    run . sh

    #!/usr/bin/env bash
    
    uwsgi --ini uwsgi.ini
    

    docker-compose

    version: "3.5"
    services:
      uwsgi:
        build: app/
        restart: on-failure
        volumes:
          - /tmp:/tmp/
    

    webserver.conf

    server {
        listen 80;
        server_name localhost;
    
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/tmp/uwsgi.sock;
        }
    }
    
    
    4 条回复    2019-08-24 22:56:41 +08:00
    lhx2008
        1
    lhx2008  
       2019-08-24 10:00:04 +08:00 via Android
    通过 volume 共享 socket ?这个想法一般人可想不到。
    lhx2008
        2
    lhx2008  
       2019-08-24 10:02:08 +08:00 via Android
    可惜 socket 应该只是记录了一些内存里面的信息,并不是用来传递数据的载体
    lbfeng
        3
    lbfeng  
    OP
       2019-08-24 10:08:08 +08:00
    @lhx2008 原文在这:blog . myhro . info/2017/01/benchmarking-ip-and-unix-domain-sockets-for-real。

    unix socket 比 tcp socket 更高效。

    补充一下:
    我刚才到 uwsgi 的 container 里看了下。

    uwsgi.sock 在 container 里是这样的。
    srw-rw-r-- 1 root root 0 Aug 24 02:00 uwsgi.sock

    会不会和 user, group 有关??
    julyclyde
        4
    julyclyde  
       2019-08-24 22:56:41 +08:00
    这就是你们“啥都喜欢用 docker 解决”引发的问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   950 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:59 · PVG 05:59 · LAX 14:59 · JFK 17:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.