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.