Flask WTForms 并不总是提交(gunicorn + nginx)

问题描述 投票:0回答:0

我最近将我的网站迁移到另一个环境(PythonAnywhere 到 Vultr),现在需要自己设置服务器。我已经让一切运行起来,但我注意到我的表单 POST 并不总是提交。似乎 50% 的 POST 有效,而其他 50% 无效。页面是否刷新并不重要,每次提交的成功似乎完全是随机的。我在使用 PythonAnywhere 或在 Windows 上本地开发网站时没有遇到这个问题,所以我怀疑这是 NGINX 和/或 gunicorn 配置的问题。

main.py - the route that submits 50/50. The other form routes have the same strange behaviour - they don't always submit, and the form remains filled out.

@app.route("/", methods=["GET", "POST"])
def home():
    form = CommentForm()

    if form.validate_on_submit():
        create_entry(form.comment.data)
        flash('<img src="/static/media/thank_you.jpg"/><h1>Thank you!</h1>')
        return redirect(url_for("home"))

    return render_template(
        "home.html", form=form, comments=get_comments(),
    )

home.html - the flash snippet which sometimes displays the <img> and <h1> flash message

{% with messages = get_flashed_messages() %}
    {% if messages %}
      {% for message in messages %}
        {{ message | safe }} <!-- safe used to render <img> and <h1> -->
      {% endfor %}
    {% endif %}
    {% endwith %}
    {% block body %}{% endblock %}

/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/ubuntu/NAME.com
Environment="PATH=/home/ubuntu/venv_flask/bin"
ExecStart=/home/ubuntu/venv_flask/bin/gunicorn -w 2 -b 127.0.0.1:9001 'main:app'

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-enabled

server {
    server_name NAME.com www.NAME.com;
    root /home/ubuntu/NAME.com;

    location / {
        proxy_pass http://127.0.0.1:9001/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Prefix /;
    }
}

编辑:我添加了一个额外的工人(现在总共 3 个),并完全卸载了 docker。我觉得也许是硬件限制......将继续调查。

nginx flask post gunicorn flask-wtforms
© www.soinside.com 2019 - 2024. All rights reserved.