Issue with TCP server - Nginx & Python (socketserver)

October 8, 2019 195 views
DigitalOcean Nginx Python

Hi all,

I have been developing a TCP Server in python (socketserver framework), in order to receive data from a GPS tracking unit. It just needs the IP, Port and type of server TCP/UDP.

The problem is that the Server does not receive data, it receives some information, I will show you below:

(The server code is at the end of this question)

b'PROXY TCP4 190.130.120.61 10.46.0.5 7840 3000'
45
b'PROXY TCP4 190.130.120.61 10.46.0.5 11159 3000'
46

When I checked nginx error logs

$ sudo tail –F /var/log/nginx/error.log

2019/10/08 11:19:33 [error] 3569#3569: *559 connect() failed (111: Connection refused) while connecting to upstream, client: 191.156.38.225, server: 0.0.0.0:3000, upstream: "127.0.0.1:7077", bytes from/to client:0/0, bytes from/to upstream:0/0
2019/10/08 11:19:45 [error] 3569#3569: *561 connect() failed (111: Connection refused) while connecting to upstream, client: 191.156.44.76, server: 0.0.0.0:3000, upstream: "127.0.0.1:7077", bytes from/to client:0/0, bytes from/to upstream:0/0
2019/10/08 11:19:58 [error] 3569#3569: *563 connect() failed (111: Connection refused) while connecting to upstream, client: 191.156.43.39, server: 0.0.0.0:3000, upstream: "127.0.0.1:7077", bytes from/to client:0/0, bytes from/to upstream:0/0

So, it confirms that the data was not received

Then I decided to test the TCP server with another alternative, hence I used the following code in other machine.

HOST, PORT = "myDigitalOceanPublicIP", 3000
#HOST, PORT = "127.0.0.1", 7077 Testing in local machine

data = "DataFromOtherMachine"

# Create a socket (SOCK_STREAM means a TCP socket)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data + "\n", "utf-8"))

    # Receive data from the server and shut down
    received = str(sock.recv(1024), "utf-8")

print("Sent:     {}".format(data))
print("Received: {}".format(received))

Unfortunatly, I got the next error:

Traceback (most recent call last):
  File "client.py", line 16, in <module>
    received = str(sock.recv(1024), "utf-8")
ConnectionResetError: [Errno 54] Connection reset by peer

I do not undestand why this is occuring, I also have edited the
/etc/nginx/nginx.conf file as follow:

stream {
    server {
      listen 0.0.0.0:3000;
      proxy_pass 127.0.0.1:7077;
      proxy_protocol on;
    }
}

I have allowed port 3000 using ufd command:
$sudo ufw allow 3000/tcp
$sudo ufw allow 3000

Thank you for your time reading my question, I will appreciate your help

1 Answer

Hello,

First you need to make sure that your python script is actually listening on prot 7077. You can do that with the following command:

netstat -plant | grep 7077

If you can see the service there, then the problem is with the Nginx setup. I would recommend not making the changes to the /etc/nginx/nginx.conf file but to the /etc/nginx/sites-enabled/default. The file should look something like this:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    location / {
        proxy_pass 127.0.0.1:7077;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Then after applying the changes run a config test:

nginx -t

And then if you get Syntax OK restart Nginx:

systemctl restart nginx

Let me know how it goes!
Regards,
Bobby

Have another answer? Share your knowledge.