【问题标题】:Nginx/Flask/Python App, Nginx throwing 502 Bad Gateway ErrorNginx/Flask/Python 应用程序,Nginx 抛出 502 Bad Gateway 错误
【发布时间】:2016-12-12 21:53:11
【问题描述】:

Nginx 之前工作正常,然后我在html 中添加了一个form,nginx 开始抛出此错误:

2016/12/12 16:37:24 [error] 983#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xxx.xxx.xxx, server: site.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "site.com"

我的 forms.py 看起来像:

from flask_wtf import FlaskForm
from wtforms import TextField, StringField, SubmitField, validators

class EmailForm(FlaskForm):
  email = TextField("Email")
  submit = SubmitField("Send")

我的 app.py 看起来像:

from flask import Flask, render_template, request
from flask_mail import Mail, Message
from forms import EmailForm

app.config['DEBUG'] = True

app = Flask(__name__)
app.secret_key = 'secrets'

# add mail server config
app.config['MAIL_SERVER'] = 'site.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'contact@site.com'
app.config['MAIL_PASSWORD'] = 'pass'

mail = Mail(app)

@app.route('/', methods=('GET', 'POST'))
def email():
    form = EmailForm()

    if request.method == 'POST':
        if form.validate() == False:
            return 'Please fill in all fields <p><a href="/">Try Again</a></p>'
        else:
            msg = Message("Message from your visitor",
                          sender='contact@site.com',
                          recipients=['contact@site.com'])
           msg.body = """
            From: %s <%s>,
            %s
            """ % (form.email.data)
            mail.send(msg)
            return "Successfully  sent message!"
    elif request.method == 'GET':
        return render_template('index.html', form=form)

if __name__ == '__main__':
    app.run()

index.html(在templates/):

        <form action="{{ url_for('email') }}" method="post">
            {{ form.hidden_tag() }}
            {{ form.email }}
            {{ form.submit }}
        </form>

sites-enabled/ 中的 nginx 配置:

server {
   server_name mysite.com;
   listen 80;
    location / {
        proxy_pass http://localhost:8001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    location /static {
        alias  /home/www/flask-deploy/mysite/static/;
    }
}

我一直在不知疲倦地看着这个,但似乎无法查明问题所在。有人看到我在这里做错了吗?

谢谢。

【问题讨论】:

  • 我们需要你的 nginx 配置的相关部分...
  • 请看上面@StephaneMartin
  • nginx 说它没有找到在 localhost 端口 8001 上侦听的任何人。msg.body=""" 之后有缩进问题。所以也许你的 python 脚本根本没有运行?
  • 就是这样粘贴的。我验证msg.body 信息在同一行
  • 和端口 8001 ?在烧瓶中,默认值为 5000

标签: python nginx flask


【解决方案1】:

“连接被拒绝”表示 Nginx 在 localhost 端口 8001 上没有发现任何监听。可能你的烧瓶应用正在监听另一个端口。默认情况下,flask 侦听端口 5000。

你可以试试:

  • 修改nginx conf:proxy_pass http://localhost:5000;
  • 让烧瓶应用程序在端口 8001 上侦听:app.config['SERVER_NAME'] = "127.0.0.1:8001"

【讨论】:

    猜你喜欢
    • 2018-11-12
    • 2020-12-14
    • 2013-07-16
    • 1970-01-01
    • 2018-04-23
    • 2020-08-01
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    相关资源
    最近更新 更多