python Flask 博客,内容过长时发生502异常

发布于 2022-09-07 03:27:14 字数 3726 浏览 16 评论 0

项目结构:react + python + flask + flask_sqlalchemy
请求异常:提交一个新建博文的申请,当字数超过976左右就会出现502异常; 当字数小于这个数字时,一切正常;
请码神们略给指点

以下是post 数据模型:

class Post(db.Model):
        __tablename = 'post'
        __table_args__ = {
                'mysql_charset': 'utf8'
        }
        id = db.Column(db.Integer, primary_key = True, autoincrement = True)
        title = db.Column(db.String(64))
        text = db.Column(db.Text)
        publish_date = db.Column(db.DateTime, default=datetime.datetime.now)
        publish_able = db.Column(db.String(1))
        user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
        tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'))

        comments = db.relationship('Comment', backref='post', lazy='dynamic')

        def __init__(self, title, text, publish_able, tag_id):
                self.title = title
                self.text = text
                self.publish_able = publish_able
                self.tag_id = tag_id

        def __repr__(self):
                return '<Post {}>'.format(self.title)

以下为前端FETCH 提交数据:

Request URL: http://112.74.185.214/blog/create
Request Method: POST
Status Code: 502 Bad Gateway
Remote Address: 112.74.185.214:80
Referrer Policy: no-referrer-when-downgrade
Connection: keep-alive
Content-Length: 537
Content-Type: text/html
Date: Sun, 22 Apr 2018 02:55:26 GMT
ETag: "59e604dd-219"
Server: nginx/1.12.2
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: en,zh;q=0.9,zh-CN;q=0.8
Connection: keep-alive
Content-Length: 1521
Content-Type: application/json
Cookie: session=eyJ1c2VyX2lkIjoxfQ.Db2KGQ.jRMFn70VzZO4qbQjkwsXL-jcXA8
Host: 112.74.185.214
Origin: http://112.74.185.214
Referer: http://112.74.185.214/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 OPR/52.0.2871.64 (Edition Baidu)


{title: "1212", tagId: 3, publishable: "1",…}
publishable: "1"
tagId: 3
title: "1212"
text: "<p>捕获try except中间代码发生的异常,如果发生异常执行except的代码,不管是否发生异常都执行finally中的代码</p><p>except可以有0个或多个,如果有多个从上到下依次根据异常类型匹配,匹配某个Exception这执行对应的except中代码捕获try except中间代码发生的异常,如果发生异常执行except的代码,不管是否发生异常都执行finally中的代码</p><p>except可以有0个或多个,如果有多个从上到下依次根据异常类型匹配,匹配某个Exception这执行对应的except中代码捕获try except中间代码发生的异常,如果发生异常执行except的代码,不管是否发生异常都执行finally中的代码</p><p>except可以有0个或多个,如果有多个从上到下依次根据异常类型匹配,匹配某个Exception这执行对应的except中代码捕获try except中间代码发生的异常,如果发生异常执行except的代码,不管是否发生异常都执行finally中的代码</p><p>except可以有0个或多个,如果有多个从上到下依次根据异常类型匹配,匹配某个Exception这执行对应的except中代码捕获try except中间代码发生的异常,如果发生异常执行except的代码,不管是否发生异常都执行finally中的代码</p><p>except可以有0个或多个,如果有多个从上到下依次根据异常类型匹配,匹配某个Exception这执行对应的except中代码</p>"

以下服务器错误

An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check the error log for details.

Faithfully yours, nginx.

读取nginx error_log

*9 upstream prematurely closed connection while reading response header from upstream, client: ***.***.**.***, server: ***.***.***.***, request: "POST /blog/create HTTP/1.1",

问题已经解决,以下是解决过程:

  1. 打开服务器进度,发送指令top,发现 在发送几个请求后 uwsgi 占用率100%;
  2. 杀死对应的uwsgi pid 后,再次uwsgi config.ini
  3. 一切正常

比较本次启动uwsgi 和 前几次的不同,发现之前的uwsgi根本没有启动成功。
验证如下:

  1. 之前发送uwsgi config.ini 时,返回log显示启动不成功,端口被占用。(被我傻逼地忽略了,呵呵)
  2. 本次发送后,服务器终端正常打出log并输出;

为什么之前uwsgi没有启动成功也会偶然的请求成功(以下为我瞎猜):
怀疑为nginx 在分发时, 发现没有uwsgi服务就自行 启动了一个uwsgi;在多个请求后,因为uwsgi实例过多,导致cpu被沾满;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文