nginx django uwsgi:服务器错误(500)-访问没有“q”参数的视图模型时

u5rb5r59  于 2023-05-28  发布在  Nginx
关注(0)|答案(1)|浏览(242)

我开始面临一个问题,因为我的服务器从Ubuntu 18.04 LTS升级到Ubuntu 20.04 LTS。
似乎我遇到了一个问题(Server Error (500)),只有一个模型,特别是URI /admin/mymodule/mymodel/。请参考以下uwsgi日志:

[pid: 27771|app: 0|req: 4/8] 192.168.1.101 () {72 vars in 1623 bytes} [Fri May 26 06:11:11 2023] GET /admin/mymodule/mymodel/ => generated 145 bytes in 210 msecs (HTTP/1.1 500) 6 headers in 184 bytes (1 switches on core 0)

但是当尝试使用查询参数(/admin/mymodule/mymodel/?all=&q=2022-12-19)的相同URI时,它工作得很好。请参考以下uwsgi日志:

[pid: 27768|app: 0|req: 3/9] 192.168.1.101 () {70 vars in 1627 bytes} [Fri May 26 06:11:15 2023] GET /admin/mymodule/mymodel/?all=&q=2022-12-19 => generated 100205 bytes in 1013 msecs (HTTP/1.1 200) 9 headers in 463 bytes (1 switches on core 0)
[pid: 27770|app: 0|req: 3/10] 192.168.1.101 () {68 vars in 1468 bytes} [Fri May 26 06:11:16 2023] GET /admin/jsi18n/ => generated 3187 bytes in 12 msecs (HTTP/1.1 200) 6 headers in 189 bytes (1 switches on core 0)

我不理解这种行为,因为我能够成功访问所有其他视图模型和自定义视图,没有任何问题。并能够使用任何查询参数值访问此视图。
我已经尝试了一些步骤来解决这个问题,但没有运气:

  • 重新启动uwsgi服务
  • 重启nginx
  • 重新启动虚拟机

任何见解是什么可能导致这个问题,或者如果有什么可能是失踪?了解其他人是否遇到过类似的问题以及如何解决也会很有帮助。如能就如何处理这一情况提供任何意见或指导,将不胜感激。
谢谢你。

lx0bsm1f

lx0bsm1f1#

我从中学到的教训是,当你遇到Django的一些问题时,使用DEBUG = True
在我的例子中,我在使用DEBUG = True时得到了下面的错误。

Traceback (most recent call last):
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/contrib/admin/options.py", line 614, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 233, in inner
    return view(request, *args, **kwargs)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1811, in changelist_view
    'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/models/query.py", line 269, in __len__
    self._fetch_all()
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/models/query.py", line 1308, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1156, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/MySQLdb/cursors.py", line 320, in _query
    self._do_get_result(db)
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/MySQLdb/cursors.py", line 145, in _do_get_result
    self._result = result = self._get_result()
  File "/usr/local/share/track_vm/lib/python3.6/site-packages/MySQLdb/cursors.py", line 352, in _get_result
    return self._get_db().store_result()
django.db.utils.OperationalError: (1038, 'Out of sort memory, consider increasing server sort buffer size')

为了解决这个问题,我在/etc/mysql/mysql.conf.d/mysql.cnf文件中设置了sort_buffer_size值。

[mysqld]
sort_buffer_size = 4M

我认为我们正面临这个问题,因为作为Ubuntu升级的一部分,我们也将MySQL从5.7升级到8.0.33。这可能已经删除了之前设置的MySQL配置。
再一次,@Razenstein,非常感谢。

相关问题