无法在 Heroku 上为带有 uwsgi 的烧瓶应用程序绑定端口 80

Fails to bind port 80 on Heroku for a flask app with uwsgi

我写了一个简单的 flask 应用程序,我想将它部署到 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'

的用户但未成功

小于1024的端口为特权端口,需要root用户权限才能绑定。我猜你不是 运行ning 入口点作为 root 用户。

尝试像这样更新 nginx 将绑定到 HTTP 的默认端口:https://github.com/tiangolo/uwsgi-nginx-flask-docker#custom-listen-port

只要在 运行 docker run.

时将端口 80(外部)映射到此自定义端口,这就没有关系了