繁体   English   中英

Nginx / Flask / Python应用程序,Nginx抛出502 Bad Gateway错误

[英]Nginx/Flask/Python App, Nginx throwing 502 Bad Gateway Error

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没有找到任何侦听localhost,端口8001的内容。也许你的烧瓶应用程序正在侦听另一个端口。 默认情况下,flask会侦听端口5000。

你可以尝试:

  • 修改nginx conf: proxy_pass http://localhost:5000;
  • 或者让烧瓶应用监听端口8001: app.config['SERVER_NAME'] = "127.0.0.1:8001"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM