Django Redis 连接重置

Posted

技术标签:

【中文标题】Django Redis 连接重置【英文标题】:Django Redis Connection reset 【发布时间】:2021-12-14 12:47:18 【问题描述】:

我正在尝试将 django_redis 用于 Django 的 redis 缓存后端。该应用程序在本地主机上的开发阶段运行良好。但是在 Heroku 上部署并使用 django_redis 进行 redis 缓存后连接正在重置,页面崩溃并出现 500 内部服务器错误

2021-10-29T12:10:20.450924+00:00 heroku[router]: at=info method=GET path="/" host=www.humaurtum-matrimony.com request_id=37a90921-
9d8d-46c6-9d6a-888d34fb1d01 fwd="42.106.161.10" dyno=web.1 connect=0ms service=2701ms status=500 bytes=5280 protocol=https
2021-10-29T12:10:20.447599+00:00 app[web.1]: 2021-10-29 17:40:18,010 ERROR    Internal Server Error: /
2021-10-29T12:10:20.447606+00:00 app[web.1]: Traceback (most recent call last):
2021-10-29T12:10:20.447607+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django_redis/cache.py", line 27
, in _decorator
2021-10-29T12:10:20.447608+00:00 app[web.1]: return method(self, *args, **kwargs)
2021-10-29T12:10:20.447608+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django_redis/cache.py", line 94
, in _get
2021-10-29T12:10:20.447609+00:00 app[web.1]: return self.client.get(key, default=default, version=version, client=client)
2021-10-29T12:10:20.447609+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django_redis/client/default.py"
, line 222, in get
2021-10-29T12:10:20.447609+00:00 app[web.1]: raise ConnectionInterrupted(connection=client) from e
2021-10-29T12:10:20.447610+00:00 app[web.1]: django_redis.exceptions.ConnectionInterrupted: Redis ConnectionError: Error while rea
ding from socket: (104, 'Connection reset by peer')
2021-10-29T12:10:20.447610+00:00 app[web.1]:
2021-10-29T12:10:20.447611+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2021-10-29T12:10:20.447611+00:00 app[web.1]:
2021-10-29T12:10:20.447612+00:00 app[web.1]: Traceback (most recent call last):
2021-10-29T12:10:20.447612+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.
py", line 34, in inner
2021-10-29T12:10:20.447613+00:00 app[web.1]: response = get_response(request)
2021-10-29T12:10:20.447614+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py",
line 115, in _get_response
2021-10-29T12:10:20.447614+00:00 app[web.1]: response = self.process_exception_by_middleware(e, request)
2021-10-29T12:10:20.447614+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py",
line 113, in _get_response
2021-10-29T12:10:20.447615+00:00 app[web.1]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
2021-10-29T12:10:20.447615+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/views/generic/base.py",
line 71, in view
2021-10-29T12:10:20.447616+00:00 app[web.1]: return self.dispatch(request, *args, **kwargs)
2021-10-29T12:10:20.447616+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/decorators.py", li
ne 43, in _wrapper
2021-10-29T12:10:20.447616+00:00 app[web.1]: return bound_method(*args, **kwargs)
2021-10-29T12:10:20.447617+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/views/decorators/debug.p
y", line 76, in sensitive_post_parameters_wrapper
2021-10-29T12:10:20.447617+00:00 app[web.1]: return view(request, *args, **kwargs)
2021-10-29T12:10:20.447617+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/decorators.py", li
ne 43, in _wrapper
2021-10-29T12:10:20.447617+00:00 app[web.1]: return bound_method(*args, **kwargs)
2021-10-29T12:10:20.447618+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/decorators.py", li
ne 130, in _wrapped_view
2021-10-29T12:10:20.447618+00:00 app[web.1]: response = view_func(request, *args, **kwargs)
2021-10-29T12:10:20.447618+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/decorators.py", li
ne 43, in _wrapper
2021-10-29T12:10:20.447618+00:00 app[web.1]: return bound_method(*args, **kwargs)
2021-10-29T12:10:20.447619+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/views/decorators/cache.p
y", line 44, in _wrapped_view_func
2021-10-29T12:10:20.447619+00:00 app[web.1]: response = view_func(request, *args, **kwargs)
2021-10-29T12:10:20.447619+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/views.py",
line 63, in dispatch
2021-10-29T12:10:20.447619+00:00 app[web.1]: return super().dispatch(request, *args, **kwargs)
2021-10-29T12:10:20.447620+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/views/generic/base.py",
line 97, in dispatch
2021-10-29T12:10:20.447620+00:00 app[web.1]: return handler(request, *args, **kwargs)
2021-10-29T12:10:20.447620+00:00 app[web.1]: File "./member/views.py", line 243, in get
2021-10-29T12:10:20.447620+00:00 app[web.1]: return render(request, 'member/login_new.html', context)
2021-10-29T12:10:20.447621+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/shortcuts.py", line 19,
in render
2021-10-29T12:10:20.447621+00:00 app[web.1]: content = loader.render_to_string(template_name, context, request, using=using)
2021-10-29T12:10:20.447621+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/loader.py", lin
e 62, in render_to_string
2021-10-29T12:10:20.447622+00:00 app[web.1]: return template.render(context, request)
2021-10-29T12:10:20.447622+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/backends/django
.py", line 61, in render
2021-10-29T12:10:20.447622+00:00 app[web.1]: return self.template.render(context)
2021-10-29T12:10:20.447622+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line
171, in render
2021-10-29T12:10:20.447623+00:00 app[web.1]: return self._render(context)
2021-10-29T12:10:20.447623+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line
163, in _render
2021-10-29T12:10:20.447623+00:00 app[web.1]: return self.nodelist.render(context)
2021-10-29T12:10:20.447623+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line
936, in render
2021-10-29T12:10:20.447624+00:00 app[web.1]: bit = node.render_annotated(context)
2021-10-29T12:10:20.447624+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line
903, in render_annotated
2021-10-29T12:10:20.447624+00:00 app[web.1]: return self.render(context)
2021-10-29T12:10:20.447625+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/templatetags/cache.py",
line 45, in render
2021-10-29T12:10:20.447625+00:00 app[web.1]: value = fragment_cache.get(cache_key)
2021-10-29T12:10:20.447629+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django_redis/cache.py", line 87
, in get
2021-10-29T12:10:20.447630+00:00 app[web.1]: value = self._get(key, default, version, client)
2021-10-29T12:10:20.447630+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django_redis/cache.py", line 34
, in _decorator
2021-10-29T12:10:20.447630+00:00 app[web.1]: raise e.__cause__
2021-10-29T12:10:20.447630+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django_redis/client/default.py"
, line 220, in get
2021-10-29T12:10:20.447631+00:00 app[web.1]: value = client.get(key)
2021-10-29T12:10:20.447631+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/redis/client.py", line 1606, in
 get
2021-10-29T12:10:20.447632+00:00 app[web.1]: return self.execute_command('GET', name)
2021-10-29T12:10:20.447632+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/redis/client.py", line 898, in
execute_command
2021-10-29T12:10:20.447632+00:00 app[web.1]: conn = self.connection or pool.get_connection(command_name, **options)
2021-10-29T12:10:20.447632+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/redis/connection.py", line 1192
, in get_connection
2021-10-29T12:10:20.447633+00:00 app[web.1]: connection.connect()
2021-10-29T12:10:20.447633+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/redis/connection.py", line 567,
 in connect
2021-10-29T12:10:20.447634+00:00 app[web.1]: self.on_connect()
2021-10-29T12:10:20.447634+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/redis/connection.py", line 643,
 in on_connect
2021-10-29T12:10:20.447634+00:00 app[web.1]: auth_response = self.read_response()
2021-10-29T12:10:20.447634+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/redis/connection.py", line 739,
 in read_response
2021-10-29T12:10:20.447635+00:00 app[web.1]: response = self._parser.read_response()
2021-10-29T12:10:20.447635+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/redis/connection.py", line 470,
 in read_response
2021-10-29T12:10:20.447635+00:00 app[web.1]: self.read_from_socket()
2021-10-29T12:10:20.447635+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/redis/connection.py", line 453,
 in read_from_socket
2021-10-29T12:10:20.447636+00:00 app[web.1]: (ex.args,))
2021-10-29T12:10:20.447636+00:00 app[web.1]: redis.exceptions.ConnectionError: Error while reading from socket: (104, 'Connection
reset by peer')
2021-10-29T12:10:20.448350+00:00 app[web.1]: 2021-10-29 17:40:20,448 DEBUG    HTTP 500 response started for ['10.1.61.20', 31465]
2021-10-29T12:10:20.449096+00:00 app[web.1]: 2021-10-29 17:40:20,448 DEBUG    HTTP close for ['10.1.61.20', 31465]
2021-10-29T12:10:20.449355+00:00 app[web.1]: 2021-10-29 17:40:20,449 INFO     "10.1.61.20" - - [08/Jan/1970:14:23:33 +0000] "GET /
 HTTP/1.1" 500 5052 "https://www.humaurtum-matrimony.com/" "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, li
ke Gecko) Chrome/94.0.4606.61 Safari/537.36"
2021-10-29T12:10:20.449533+00:00 app[web.1]: 2021-10-29 17:40:20,449 DEBUG    HTTP response complete for ['10.1.61.20', 31465]
2021-10-29T12:10:20.449615+00:00 app[web.1]: 10.1.61.20:31465 - - [29/Oct/2021:17:40:20] "GET /" 500 5052
^CTerminate batch job (Y/N)? y

settings.py 有以下 CACHE 设置

CACHES = 
    "default": 
        "BACKEND": "django_redis.cache.RedisCache",     
        "LOCATION": 'redis://:p762f52e81c6786xxx@ec2-xx.compute-1.amazonaws.com:xx',
        "OPTIONS": 
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        
    

请建议如何解决该问题。谢谢

【问题讨论】:

【参考方案1】:

由于使用“REDIS_URL”而遇到错误

Heroku 确认的简单答案是使用“REDIS_TLS_URL”!

redis_cache 的设置如下,如文档中所述:

import os
CACHES = 
    "default": 
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": os.environ.get('REDIS_TLS_URL'),
        "OPTIONS": 
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": 
                "ssl_cert_reqs": None
            ,
        
    

【讨论】:

以上是关于Django Redis 连接重置的主要内容,如果未能解决你的问题,请参考以下文章

django errno 104 连接被对等方重置

Django连接redis

django连接redis集群问题排查思路和总结

AWS Elastic Beanstalk EC2 与 Redis - 无法连接 - Django_Redis

heroku 中的 Django Redis 连接错误:写入套接字时出现错误 110。连接超时

django2.0.6 连接使用redis集群