I do realise this question has been asked about 6 years ago Bind nginx on the same port for tcp and http
However, has anything changed, is it possible for me to do something like:
stream {
listen 80;
location / {
protocol http;
}
location /mysql {
protocol tcp;
}
}
Or must thetcp
and http
modules still be separate?
This is not just a missing feature in Nginx that could be improved somehow in the future, but a protocol level incompatibility making it completely impossible.
In your example, the location path
/mysql
only exists within the HTTP protocol. In HTTP, the client starts the communication with a request:The MySQL Client/Server Protocol is a totally different protocol which even starts the other way around: the server sending an Initial Handshake Packet, e.g.
Theoretically, TLS could make it possible to distinguish different services on the same TCP port based on the Server Name Indication (SNI) and forward the traffic to different upstream servers, even with different underlying protocols. However,
STARTTLS
), and we are not running out of TCP ports, anyway. Therefore, there's no high demand for this feature.They still need to be separate. Otherwise nginx would need to be able to proxy mysql calls which it can't.