nginx-python-module icon indicating copy to clipboard operation
nginx-python-module copied to clipboard

Error when execute socket.getaddrinfo

Open seuoliveira opened this issue 8 years ago • 4 comments

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?

seuoliveira avatar Sep 28 '17 21:09 seuoliveira

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.

arut avatar Sep 29 '17 11:09 arut

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?

seuoliveira avatar Sep 29 '17 16:09 seuoliveira

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.

f-kalantari avatar May 25 '18 21:05 f-kalantari

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

lefedor avatar Jan 06 '20 00:01 lefedor