Error in production
Describe the bug Use Gunicorn in production
To Reproduce Use gunicorn for start and call a endpoint with limit
Expected behavior do nothing
Screenshots
If applicable, add screenshots to help explain your problem.
Oct 25 20:16:46 gunicorn[12540]: return await dependant.call(**values) Oct 25 20:16:46 gunicorn[12540]: File "/home/ubuntu/proyect/api/env/lib/python3.10/site-packages/slowapi/extension.py", line 635, in async_wrapper Oct 25 20:16:46 gunicorn[12540]: self._check_request_limit(request, func, False) Oct 25 20:16:46 gunicorn[12540]: File "/home/ubuntu/proyect/api/env/lib/python3.10/site-packages/slowapi/extension.py", line 535, in _check_request_limit Oct 25 20:16:46 gunicorn[12540]: self.__evaluate_limits(request, endpoint, all_limits) Oct 25 20:16:46 gunicorn[12540]: File "/home/ubuntu/proyect/api/env/lib/python3.10/site-packages/slowapi/extension.py", line 413, in __evaluate_limits Oct 25 20:16:46 gunicorn[12540]: limit_key = lim.key_func(request) Oct 25 20:16:46 gunicorn[12540]: File "/home/ubuntu/proyect/api/env/lib/python3.10/site-packages/slowapi/util.py", line 21, in get_remote_address Oct 25 20:16:46 gunicorn[12540]: return request.client.host or "127.0.0.1" Oct 25 20:16:46 gunicorn[12540]: AttributeError: 'NoneType' object has no attribute 'host'
Your app (please complete the following information):
- fastapi or starlette? use fastapi
- Version? - 0.79.0 and 0.85.1
- slowapi version (have you tried with the latest version)? yes, is 0.85.1
Additional context y set the api in production, and get this error
Hi @juanretamales I suspect this is a problem with your code, where File "/home/ubuntu/proyect/api/env/lib/python3.10/site-packages/slowapi/util.py", line 21, in get_remote_address Oct 25 20:16:46 gunicorn[12540]: return request.client.host or "127.0.0.1" Oct 25 20:16:46 gunicorn[12540]: AttributeError: 'NoneType' object has no attribute 'host' is the key point:
request.client is None in your execution, I don't think this has anything to do with slowapi.
i search and the problem is when you use gunicorn (here has the problem), i suggest any method or option for skip the error when appears.
Hi @juanretamales If I'm not wrong, this error happens when your application is running behind a proxy such as nginx (maybe gunicorn as well). You should make sure you correctly proxy the headers at the proxy level.
Here is an example configuration that should do the trick, if you use nginx
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
EDIT: also with this, for security reason you should add some middleware (for example this one from starlette) to avoid your clients forging their host/ip
@juanretamales did the comment above help? if so can you close this issue?