有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解决了错误消息,谢谢。
答案 0 :(得分:0)
您需要确保
server {
listen whoami.loc:81;
proxy_pass whoami.loc;
}
我生成为
server {
listen 81;
proxy_pass whoami.loc;
}
由于您的原始配置有效,因此/etc/hosts
需要whoami.loc
的主机条目。如果您将主机条目设为127.0.0.1
,那么它只会监听容器内的localhost。而来自外部的联系将无法回答。
这是在进行主机输入并重新启动nginx之后的原因,它从容器内部开始工作,但不是从主机开始工作。因为它成为一种效果听取127.0.0.1:81