0

What I want

There is the https://github.com/jwilder/nginx-proxy package. This is very useful for "HTTP", but I want to use it to TCP too. (MySQL)

Here are my changes: https://github.com/fchris82/nginx-proxy/commit/33d80ea4d4be5d511e4dab0413d516770aa15262

As you can see, I have added stream {} block to nginx.conf and the /etc/nginx/stream.conf.d directory. Here is the generated default.conf for stream block:

access_log off;
error_log /var/log/nginx/debug.log debug;
resolver 127.0.0.11;
# whoami.loc
upstream whoami.loc {
    ## Can be connect with "nginxproxy_default" network
    # nginxproxy_mysql_1
    server 192.168.32.2:3306;
}
server {
    listen whoami.loc:81;
    proxy_pass whoami.loc;
}

What I did, how can you reproduce the error

# Set host
> sudo echo "127.0.0.1   whoami.loc" >> /etc/hosts
# Start containers
> docker-compose up -d
# "Login" the proxy container
> docker-compose exec nginx-proxy /bin/bash
# Test connect to MySQL from proxy container
root> mysql -u root -proot -h whoami.loc -P 81
# --> OK, it works! Let's exit.
mariadb> \q
# Exit from container
root> exit
# Check host
> ping whoami.loc
# --> OK, 127.0.0.1
# Check docker ports 
> docker-compose ps
          Name                        Command               State                    Ports                  
-----------------------------------------------------------------------------------------------------------
nginxproxy_mysql_1         docker-entrypoint.sh mysqld      Up      3306/tcp                                
nginxproxy_nginx-proxy_1   /app/docker-entrypoint.sh  ...   Up      0.0.0.0:180->80/tcp, 0.0.0.0:81->81/tcp 
nginxproxy_whoami_1        /app/http                        Up      8000/tcp                                
# --> OK
# Try to direct connection from host (You can read the IP from the /etc/nginx/stream.conf.d/default.conf file)
> mysql -u root -proot -h 192.168.32.2
# --> OK, exit
mysql> \q
# Try to connect from host with "domain" through docker proxy
> mysql -u root -proot -H whoami.loc -P 81 --protocol=tcp
# ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2

So, has anybody idea why works from container but why doesn't work from host?

The "solution"

There isn't solution for what I realy wanted. I wanted something like this: Nginx TCP forwarding based on hostname . Tarun's answer solved the error message, thank you.

  • You are doing a `server` proxy_pass and expecting it to do a `stream` proxy_pass? – Tarun Lalwani Oct 11 '17 at 15:08
  • @TarunLalwani I don't understand what do you mean. The included conf is in `stream {}` block. Here are `server`-s also: https://www.nginx.com/resources/admin-guide/tcp-load-balancing/ . – Krisztián Ferenczi Oct 11 '17 at 15:24
  • Okie my bad, I didn't read the config properly, didn't see it was part of a stream block only. Can you try `telnet whoami.loc 81` and see what you get? – Tarun Lalwani Oct 11 '17 at 15:27
  • Local machine: ``` Trying 127.0.0.1... Connected to whoami.loc. Escape character is '^]'. Connection closed by foreign host. ``` From container: ``` Trying 127.0.0.1... Connected to whoami.loc. Escape character is '^]'. J 5.6.37mKme3lI�BXvh&"vPmR2Dmysql_native_passwordConnection closed by foreign host. ``` – Krisztián Ferenczi Oct 11 '17 at 15:32
  • Can you run `nginx -T` inside the `nginx-proxy` container and post a link of http://pastebin.com – Tarun Lalwani Oct 11 '17 at 15:41
  • https://pastebin.com/RMBA5GNA – Krisztián Ferenczi Oct 11 '17 at 15:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156485/discussion-between-tarun-lalwani-and-krisztian-ferenczi). – Tarun Lalwani Oct 11 '17 at 15:54

1 Answers1

0

You need to make sure that

server {
    listen whoami.loc:81;
    proxy_pass whoami.loc;
}

I getting generated as

server {
    listen 81;
    proxy_pass whoami.loc;
}

Because for your original config to work, it needs a host entry in /etc/hosts for whoami.loc. And if you make the host entry as 127.0.0.1 then it will only listen to localhost inside the container. And the connections from outside won't be answered.

That is the reason after making the host entry and restart nginx, it worked from inside the container but not from the host. Because it became a effect listen as 127.0.0.1:81

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265