问题原文
原帖链接 · Sammaye · CC BY-SA 4.0
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?
已采纳解答
原帖链接 · Esa Jokinen · CC BY-SA 4.0
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:
GET /mysql HTTP/1.1
Hostname: example.com
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.
36 00 00 00 0a 35 2e 35 2e 32 2d 6d 32 00 0b 00 6....5.5.2-m2... 00 00 64 76 48 40 49 2d 43 4a 00 ff f7 08 02 00. ..dvH@I-CJ...... 00 00 00 00 00 00 00 00 00 00 00 00 00 2a 34 64. .............* 4d 7c 63 5a 77 6b 34 5e 5d 3a 00 |cZwk4^]:.
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,
- AFAIK anything like that hasn't been implemented in Nginx.
- Your chosen example, MySQL Client/Server Protocol, doesn't start the communication with TLS, but the capability for TLS is announced in the Initial Handshake Packet, and the connection is upgraded to TLS, if both the server and the client supports it.
- Every standard protocol has its own port for TLS (or plain text with
STARTTLS), and we are not running out of TCP ports, anyway. Therefore, there's no high demand for this feature.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
