ÔõÑùʵÏÖNginxµÄTCP/UDPÊðÀíÉèÖÃ
NginxÊÇÒ»¸ö¹¦Ð§Ç¿Ê¢µÄWebЧÀÍÆ÷Èí¼þ£¬²»µ«¿ÉÒÔ´¦ÀíHTTP/HTTPSÐÒéµÄÇëÇó£¬Ò²¿ÉÒÔͨ¹ýTCP/UDPÊðÀíʵÏÖÆäËûÐÒéµÄת·¢¡£
ÏÂÃ棬ÎÒÃǽ«ÏÈÈÝÔõÑùͨ¹ýNginxʵÏÖTCP/UDPÊðÀíÉèÖ㬲¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
ÉèÖÃTCPÊðÀí
Ê×ÏÈ£¬ÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
stream { server { listen 80; proxy_pass backend:8080; } }
µÇ¼ºó¸´ÖÆ
Õâ¶ÎÉèÖÃʵÏÖÁ˽«ËùÓÐ80¶Ë¿ÚµÄTCPÇëÇóת·¢µ½backendÖ÷»úµÄ8080¶Ë¿ÚÉÏ¡£ÐèҪעÖصÄÊÇ£¬backend¿ÉÒÔÊÇÒ»¸öIPµØµã£¬Ò²¿ÉÒÔÊÇNginxÉèÖÃÎļþÖÐÆäËûserverµÄÃû³Æ¡£
±ðµÄ£¬ÎªÁËÈÃNginx¼àÌýTCPÇëÇ󣬻¹ÐèÒªÔÚÆô¶¯NginxµÄÏÂÁîÖÐÌí¼Ó-g ‘daemon off;’Ñ¡Ïî¡£ÍêÕûµÄÆô¶¯ÏÂÁîÈçÏ£º
nginx -g 'daemon off;'
µÇ¼ºó¸´ÖÆ
ÉèÖÃUDPÊðÀí
Õë¶ÔUDPÊðÀí£¬NginxÐèҪʹÓõÚÈý·½Ä£¿éStream Module¡£ÐèÒªÔÚ±àÒëNginxʱÌí¼Ó–with-streamÑ¡Ï²¢ÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
stream { server { listen 53 udp; proxy_pass backend:53; proxy_timeout 1s; proxy_responses 1; } }
µÇ¼ºó¸´ÖÆ
Õâ¶ÎÉèÖÃʵÏÖÁ˽«ËùÓÐUDPµÄ53¶Ë¿ÚµÄÇëÇóת·¢µ½backendÖ÷»úµÄ53¶Ë¿ÚÉÏ£¬Í¬Ê±ÉèÖÃÁË1ÃëµÄ³¬Ê±Ê±¼äºÍ×î¶à·µ»ØÒ»¸öÏìӦЧ¹û¡£
ÐèҪעÖصÄÊÇ£¬ÔÚÒÔÉÏÉèÖÃÖУ¬backend¿ÉÒÔÊÇÒ»¸öIPµØµã£¬Ò²¿ÉÒÔÊÇNginxÉèÖÃÎļþÖÐÆäËûserverµÄÃû³Æ¡£
ÍêÕûʾÀý
×îÖÕ£¬ÎÒÃÇÌṩһ¸öÍêÕûµÄTCPºÍUDPÊðÀíÉèÖÃʾÀý£¬´úÂëÈçÏ£º
events {} http {} stream { server { listen 80; proxy_pass backend:8080; } server { listen 53 udp; proxy_pass backend:53; proxy_timeout 1s; proxy_responses 1; } }
µÇ¼ºó¸´ÖÆ
ÐèҪעÖصÄÊÇ£¬ÒÔÉÏʾÀýÖеÄbackend¿ÉÒÔÊÇÒ»¸öIPµØµã£¬Ò²¿ÉÒÔÊÇNginxÉèÖÃÎļþÖÐÆäËûserverµÄÃû³Æ¡£
×ܽá
ͨ¹ýÒÔÉÏʾÀý´úÂ룬ÎÒÃÇÏàʶÁËÔõÑùÔÚNginxÖÐʵÏÖTCP/UDPÊðÀíÉèÖã¬Éæ¼°µÄÒªº¦µã°üÀ¨Ê¹ÓÃStream ModuleÄ£¿é¡¢Ìí¼ÓlistenºÍproxy_passµÈÒªº¦×ֵȡ£Ï£ÍûÕâƪÎÄÕÂÄÜΪ¸÷ÈËʵÏÖNginxÊðÀíÉèÖÃÌṩһЩ×ÊÖú£¡
ÒÔÉϾÍÊÇÔõÑùʵÏÖNginxµÄTCP/UDPÊðÀíÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡