Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in production #116

Open
juanretamales opened this issue Oct 25, 2022 · 5 comments
Open

Error in production #116

juanretamales opened this issue Oct 25, 2022 · 5 comments

Comments

@juanretamales
Copy link

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

@laurentS
Copy link
Owner

laurentS commented Nov 8, 2022

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.

@juanretamales
Copy link
Author

juanretamales commented Nov 12, 2022

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.

@thentgesMindee
Copy link
Collaborator

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.

@thentgesMindee
Copy link
Collaborator

thentgesMindee commented Nov 14, 2022

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

@laurentS
Copy link
Owner

@juanretamales did the comment above help? if so can you close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants