arut / nginx-python-module

Nginx Python Module
BSD 2-Clause "Simplified" License
137 stars 36 forks source link

Error when execute socket.getaddrinfo #4

Open seuoliveira opened 6 years ago

seuoliveira commented 6 years ago

Hi,

I'm having a error when trying to execute socket.getaddrinfo with included script:

[error] 15067#15067: *12 python error: missing resolver [/tmp/tests.py:4] while sending response to client, client: 192.168.1.177, server: pipz.io, request: "GET / HTTP/1.1", host: "coliveira.pipz.io"

But if I try to execute in python console it's works.

My nginx config file:

/etc/nginx/conf.d/tests.conf
python_include /tmp/tests.py;
python_set $result "check(r)";
server {
    listen       WWW-MAE:80;
    server_name   pipz.io *.pipz.io;
    access_log  /var/log/nginx/access.log;
    error_log   /var/log/nginx/error.log;
    root /tmp/nginx-test/;
    add_header X-custom-header $result;
}

My python script:

/tmp/tests.py
import socket

def check(r):
    socket.getaddrinfo('192.168.1.91', 6379, 0, socket.SOCK_STREAM)
    return "ok"

This is a module limitation or wrong configuration?

arut commented 6 years ago

The python_set directive handler cannot be blocked unlike python_access, python_content. That's why potentially blocking operations (like resolving) are unavailable in code called from this directive. You may do this in python_access, save the result in r.ctx['addr'] and use it later in the variable handler.

seuoliveira commented 6 years ago

I have a application that works with nginx + two django instances, they are connected through linux socket. Each client must be connected to specific django instance. What define the most apropried instance is stored in redis and/or mysql. So I need to intercept connection from nginx to django, after the correct instance is defined, nginx forward to apropried uwsgi upstream. I would like to do this in python, but my script can not connect to redis server (remote).

I tried to do what you said using python_access, but I received: python error: Error connecting to 192.168.1.91:6379. socket not connected. [/usr/local/lib/python2.7/site-packages/redis/connection.py:489]

Is there any way to do this using python module?

f-kalantari commented 6 years ago

Could you provide an example of using updated value for r.ctx['addr'] inside nginx.conf ? what I need is to use value of r.ctx['addr'] in a variable handler that could be used in a conditional branch later on. But for some reason it does not update.

lefedor commented 4 years ago

I have same problem. Still can't connect to Redis inside python_access and python_content .