最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask中用gunicorn和Nginx出错:502 Bad Gateway

Flask crifan 7546浏览 0评论

折腾:

【已解决】Flask的gunicorn的启动任务管理supervisor

期间:

(SIPEvents) ➜  SIPEvents gunicorn app:hello
[2016-08-18 16:58:05 +0000] [19941] [INFO] Starting gunicorn 19.6.0
[2016-08-18 16:58:05 +0000] [19941] [ERROR] Connection in use: (‘127.0.0.1’, 8000)
[2016-08-18 16:58:05 +0000] [19941] [ERROR] Retrying in 1 second.
[2016-08-18 16:58:06 +0000] [19941] [ERROR] Connection in use: (‘127.0.0.1’, 8000)
[2016-08-18 16:58:06 +0000] [19941] [ERROR] Retrying in 1 second.
[2016-08-18 16:58:07 +0000] [19941] [ERROR] Connection in use: (‘127.0.0.1’, 8000)
[2016-08-18 16:58:07 +0000] [19941] [ERROR] Retrying in 1 second.
[2016-08-18 16:58:08 +0000] [19941] [ERROR] Connection in use: (‘127.0.0.1’, 8000)
[2016-08-18 16:58:08 +0000] [19941] [ERROR] Retrying in 1 second.
[2016-08-18 16:58:09 +0000] [19941] [ERROR] Connection in use: (‘127.0.0.1’, 8000)
[2016-08-18 16:58:09 +0000] [19941] [ERROR] Retrying in 1 second.
[2016-08-18 16:58:10 +0000] [19941] [ERROR] Can’t connect to (‘127.0.0.1’, 8000)

gunicorn -w 4 -b 127.0.0.1:8080 app:hello

(SIPEvents) ➜  SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 app:hello
[2016-08-18 16:59:03 +0000] [19957] [INFO] Starting gunicorn 19.6.0
[2016-08-18 16:59:03 +0000] [19957] [INFO] Listening at: http://127.0.0.1:8080 (19957)
[2016-08-18 16:59:03 +0000] [19957] [INFO] Using worker: sync
[2016-08-18 16:59:03 +0000] [19962] [INFO] Booting worker with pid: 19962
[2016-08-18 16:59:03 +0000] [19963] [INFO] Booting worker with pid: 19963
[2016-08-18 16:59:03 +0000] [19966] [INFO] Booting worker with pid: 19966
[2016-08-18 16:59:03 +0000] [19971] [INFO] Booting worker with pid: 19971

此处的nginx的配置为:

(SIPEvents) ➜  SIPEvents cat /etc/nginx/conf.d/default.conf 
server {
    listen      80;
    #listen       80 default_server;
    server_name  _;
    access_log  /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;
    location / {
        proxy_pass         http://127.0.0.1:8080/;
        proxy_redirect     off;
        proxy_set_header   Host                 $host;
        proxy_set_header   X-Real-IP            $remote_addr;
        proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto    $scheme;
    }
}
(SIPEvents) ➜  SIPEvents service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ] 
(SIPEvents) ➜  SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 app:hello
[2016-08-18 17:03:42 +0000] [20080] [INFO] Starting gunicorn 19.6.0
[2016-08-18 17:03:42 +0000] [20080] [INFO] Listening at: http://127.0.0.1:8080 (20080)
[2016-08-18 17:03:42 +0000] [20080] [INFO] Using worker: sync
[2016-08-18 17:03:42 +0000] [20085] [INFO] Booting worker with pid: 20085
[2016-08-18 17:03:42 +0000] [20086] [INFO] Booting worker with pid: 20086
[2016-08-18 17:03:42 +0000] [20091] [INFO] Booting worker with pid: 20091
[2016-08-18 17:03:42 +0000] [20092] [INFO] Booting worker with pid: 20092

去查看出错的log:

(SIPEvents) ➜  SIPEvents tail /var/log/nginx/error.log
。。。
2016/08/18 17:06:39 [crit] 20165#0: *1 connect() to unix:/tmp/uwsgi.sock failed (13: Permission denied) while connecting to upstream, client: 58.208.66.13, server: 127.0.0.1:8080, request: “GET / HTTP/1.1”, upstream: “uwsgi://unix:/tmp/uwsgi.sock:”, host: “115.29.173.126”

看到有之前用到的:

/tmp/uwsgi.sock

所以去:

rm /tmp/uwsgi.sock

然后再去试试:

(SIPEvents) ➜  SIPEvents service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

http://115.29.173.126/

502 Bad Gateway

搜:

flask gunicorn nginx 502 Bad Gateway

python – Nginx, Flask, Gunicorn 502 Error – Stack Overflow

http://115.29.173.126:8000/

Not Found
The requested URL / was not found on this server.

Flask install 502 Bad Gateway nginx | DigitalOcean

502 bad gateway: nginx & gunicorn – Server Fault

[总结]

此处,之所以出现:

502 Bad Gateway

的原因是:

nginx找不到服务器

解决办法是:

把nginx的配置中:

server_name _;

改为:

server_name 115.29.173.126;

就可以了。

之后又出现其它错误:

[已解决]Flask中用gunicorn和Nginx出错:Internal Server Error,TypeError: hello() takes no arguments (2 given)

转载请注明:在路上 » [已解决]Flask中用gunicorn和Nginx出错:502 Bad Gateway

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.295 seconds, using 22.15MB memory