【问题标题】:Fails to bind port 80 on Heroku for a flask app with uwsgi无法为带有 uwsgi 的烧瓶应用程序绑定 Heroku 上的端口 80
【发布时间】:2020-09-20 14:44:47
【问题描述】:

我写了一个简单的烧瓶应用程序,我想将它部署到 Heroku。做完 push 和 release 后,我查看日志看看:

2020-09-20T14:32:06.077331+00:00 app[web.1]: *** Starting uWSGI 2.0.18 (64bit) on [Sun Sep 20 14:32:06 2020] ***
2020-09-20T14:32:06.077331+00:00 app[web.1]: compiled with version: 8.3.0 on 09 May 2020 21:28:24
2020-09-20T14:32:06.077331+00:00 app[web.1]: os: Linux-4.4.0-1076-aws #80-Ubuntu SMP Thu Aug 6 06:48:10 UTC 2020
2020-09-20T14:32:06.077335+00:00 app[web.1]: nodename: 8a7c5567-c468-4b88-aae5-7c8917589599
2020-09-20T14:32:06.077335+00:00 app[web.1]: machine: x86_64
2020-09-20T14:32:06.077336+00:00 app[web.1]: clock source: unix
2020-09-20T14:32:06.077336+00:00 app[web.1]: pcre jit disabled
2020-09-20T14:32:06.077337+00:00 app[web.1]: detected number of CPU cores: 8
2020-09-20T14:32:06.077337+00:00 app[web.1]: current working directory: /app
2020-09-20T14:32:06.077338+00:00 app[web.1]: detected binary path: /usr/local/bin/uwsgi
2020-09-20T14:32:06.077338+00:00 app[web.1]: your processes number limit is 256
2020-09-20T14:32:06.077338+00:00 app[web.1]: your memory page size is 4096 bytes
2020-09-20T14:32:06.077339+00:00 app[web.1]: detected max file descriptor number: 10000
2020-09-20T14:32:06.077339+00:00 app[web.1]: lock engine: pthread robust mutexes
2020-09-20T14:32:06.077455+00:00 app[web.1]: thunder lock: disabled (you can enable it with --thunder-lock)
2020-09-20T14:32:06.078001+00:00 app[web.1]: uwsgi socket 0 bound to TCP address :13940 fd 3
2020-09-20T14:32:06.078116+00:00 app[web.1]: unable to find user nginx
2020-09-20T14:32:06.078731+00:00 app[web.1]: 2020-09-20 14:32:06,078 INFO exited: uwsgi (exit status 1; not expected)
2020-09-20T14:32:07.081401+00:00 app[web.1]: 2020-09-20 14:32:07,081 INFO spawned: 'nginx' with pid 14
2020-09-20T14:32:07.082947+00:00 app[web.1]: 2020-09-20 14:32:07,082 INFO spawned: 'uwsgi' with pid 15
2020-09-20T14:32:07.089800+00:00 app[web.1]: 2020/09/20 14:32:07 [warn] 14#14: the "user" directive makes sense only if the master process runs with super
-user privileges, ignored in /etc/nginx/nginx.conf:1
2020-09-20T14:32:07.089801+00:00 app[web.1]: nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ig
nored in /etc/nginx/nginx.conf:1
2020-09-20T14:32:07.090605+00:00 app[web.1]: 2020/09/20 14:32:07 [emerg] 14#14: bind() to 0.0.0.0:80 failed (13: Permission denied)
2020-09-20T14:32:07.090606+00:00 app[web.1]: nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
2020-09-20T14:32:07.091044+00:00 app[web.1]: 2020-09-20 14:32:07,090 INFO exited: nginx (exit status 1; not expected)
2020-09-20T14:32:07.093198+00:00 app[web.1]: [uWSGI] getting INI configuration from /app/uwsgi.ini
2020-09-20T14:32:07.093264+00:00 app[web.1]: 2020-09-20 14:32:07,093 INFO gave up: nginx entered FATAL state, too many start retries too quickly
2020-09-20T14:32:07.093377+00:00 app[web.1]: [uWSGI] getting INI configuration from /etc/uwsgi/uwsgi.ini

看起来,它无法绑定端口 80。 这是我的__主要__:

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    webapp.run(debug=True, host='0.0.0.0', port=port)

这是我的 uswgi.init:

[uwsgi]
module = app.ourtale
callable = webapp
master=true
http-socket = :$(PORT)

这是我的过程文件:

web: uwsgi --http-socket :$PORT --ini uwsgi.ini

这是我的 dockerfile:

FROM tiangolo/uwsgi-nginx-flask:python3.6

COPY requirements.txt /tmp/
RUN pip install -U pip
RUN pip install -r /tmp/requirements.txt

USER nginx //EDIT: Added this line, but it doesn't seem fix the "unable to find user nginx"

COPY . /app

知道为什么它无法绑定端口 80 吗? 提前致谢

更新: 我加了

ENV LISTEN_PORT 8080
EXPOSE 8080

而且它似乎解决了无权限问题。 但它似乎仍然崩溃。这些是日志:

2020-09-20T18:14:00.337298+00:00 app[web.1]: *** Starting uWSGI 2.0.18 (64bit) on [Sun Sep 20 18:14:00 2020] ***
2020-09-20T18:14:00.337298+00:00 app[web.1]: compiled with version: 8.3.0 on 09 May 2020 21:28:24
2020-09-20T18:14:00.337299+00:00 app[web.1]: os: Linux-4.4.0-1076-aws #80-Ubuntu SMP Thu Aug 6 06:48:10 UTC 2020
2020-09-20T18:14:00.337300+00:00 app[web.1]: nodename: bb979578-c952-46fc-9710-02e06bbfe136
2020-09-20T18:14:00.337300+00:00 app[web.1]: machine: x86_64
2020-09-20T18:14:00.337301+00:00 app[web.1]: clock source: unix
2020-09-20T18:14:00.337301+00:00 app[web.1]: pcre jit disabled
2020-09-20T18:14:00.337301+00:00 app[web.1]: detected number of CPU cores: 8
2020-09-20T18:14:00.337302+00:00 app[web.1]: current working directory: /app
2020-09-20T18:14:00.337302+00:00 app[web.1]: detected binary path: /usr/local/bin/uwsgi
2020-09-20T18:14:00.337303+00:00 app[web.1]: your processes number limit is 256
2020-09-20T18:14:00.337303+00:00 app[web.1]: your memory page size is 4096 bytes
2020-09-20T18:14:00.337304+00:00 app[web.1]: detected max file descriptor number: 10000
2020-09-20T18:14:00.337304+00:00 app[web.1]: lock engine: pthread robust mutexes
2020-09-20T18:14:00.337398+00:00 app[web.1]: thunder lock: disabled (you can enable it with --thunder-lock)
2020-09-20T18:14:00.337835+00:00 app[web.1]: uwsgi socket 0 bound to TCP address :13532 fd 3
2020-09-20T18:14:00.337932+00:00 app[web.1]: unable to find user nginx
2020-09-20T18:14:00.338440+00:00 app[web.1]: 2020-09-20 18:14:00,338 INFO exited: uwsgi (exit status 1; not expected)
2020-09-20T18:14:01.339776+00:00 app[web.1]: 2020-09-20 18:14:01,339 INFO gave up: uwsgi entered FATAL state, too many start retries too quickly
2020-09-20T18:14:51.678718+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-09-20T18:14:51.698324+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-09-20T18:14:51.807290+00:00 heroku[web.1]: Process exited with status 137
2020-09-20T18:14:51.849446+00:00 heroku[web.1]: State changed from starting to crashed

它似乎试图找到一个名为“nginx”的用户,但没有成功

【问题讨论】:

    标签: python flask heroku uwsgi


    【解决方案1】:

    小于 1024 的端口是特权端口,需要 root 用户权限才能绑定。我猜你没有以 root 用户身份运行入口点。

    尝试更新默认端口 nginx 将绑定到 HTTP,如下所示:https://github.com/tiangolo/uwsgi-nginx-flask-docker#custom-listen-port

    只要您在运行docker run 时将端口 80(外部)映射到此自定义端口,这无关紧要。

    【讨论】:

    • 我添加了“ENV LISTEN_PORT 8080 EXPOSE 8080”,它似乎解决了无权限问题。但它仍然崩溃。查看我添加的日志。谢谢老兄。
    • 尝试在您的 dockerfile 中创建用户 nginxgithub.com/tiangolo/uwsgi-nginx-docker/blob/master/… 使用默认用户为 nginx 参考在 docker 中添加用户:docs.docker.com/engine/reference/builder/#user
    • 我已经添加了 USER nginx,但似乎不影响它。知道为什么吗?
    • 分享您为添加用户nginx所做的更改
    • 我已经在原始帖子中编辑了 dockerfile 并添加了一行。
    猜你喜欢
    • 2014-11-17
    • 2015-05-10
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    相关资源
    最近更新 更多