【发布时间】:2017-10-12 20:51:56
【问题描述】:
我想要什么
有https://github.com/jwilder/nginx-proxy 包。这对“HTTP”非常有用,但我也想将它用于 TCP。 (MySQL)
这是我的更改:https://github.com/fchris82/nginx-proxy/commit/33d80ea4d4be5d511e4dab0413d516770aa15262
如您所见,我已将stream {} 块添加到nginx.conf 和/etc/nginx/stream.conf.d 目录。这是为流块生成的default.conf:
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;
}
我做了什么,你怎么能重现错误
# 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
那么,有谁知道为什么在容器中有效,但为什么在主机中无效?
“解决方案”
对于我真正想要的,没有解决方案。我想要这样的东西:Nginx TCP forwarding based on hostname。 Tarun's answer 解决了错误信息,谢谢。
【问题讨论】:
-
您正在执行
serverproxy_pass 并期望它执行streamproxy_pass? -
@TarunLalwani 我不明白你的意思。包含的 conf 位于
stream {}块中。这里还有server-s:nginx.com/resources/admin-guide/tcp-load-balancing。 -
好吧,我的错,我没有正确阅读配置,没有看到它只是流块的一部分。你可以试试
telnet whoami.loc 81看看你得到了什么? -
本地机器:``` 正在尝试 127.0.0.1... 连接到 whoami.loc。转义字符是 '^]'。外部主机关闭连接。 ``` 来自容器:``` 正在尝试 127.0.0.1... 连接到 whoami.loc。转义字符是 '^]'。 J 5.6.37mKme3lI�BXvh&"vPmR2Dmysql_native_passwordConnection 被外部主机关闭。```
-
你能不能在
nginx-proxy容器内运行nginx -T并发布pastebin.com的链接