SREãã¼ã ã®@cubicdaiyaã§ããä»åã¯nginxã«ããTCPã¬ã¤ã¤ã¼ã§ã®ãã¼ããã©ã³ã¹ã«ã¤ãã¦è§£èª¬ãã¾ãã
ãã¼ããã©ã³ãµã¼ã¨ãã¦ã®nginx
nginxã¯HTTPãTCPãUDPçã®è¤æ°ã®ã¬ã¤ã¤ã¼ã§ãã¼ããã©ã³ãµã¼ã¨ãã¦ç¨¼åããããã¨ãã§ãã¾ããï¼TCPãã¼ããã©ã³ãµã¼ã¯1.9.0以éãUDPãã¼ããã©ã³ãµã¼ã¯1.9.13以éã§å©ç¨å¯è½ã§ãï¼
ã¾ããngx_http_ssl_module
ã ngx_stream_ssl_module
ãå©ç¨ãããã¨ã§ããããã®ã¬ã¤ã¤ã¼ã§TLSãæå¹åãããã¨ãå¯è½ã§ãã
TCPãã¼ããã©ã³ãµã¼ç¨ã®ã¢ã¸ã¥ã¼ã«ãæå¹ã«ãã
HTTPã¬ã¤ã¤ã¼ã§ãã¼ããã©ã³ã¹ããããã®ã¢ã¸ã¥ã¼ã«ã¯ããã©ã«ãã§çµã¿è¾¼ã¾ãã¾ãããTCP(ã¨UDP)ã¬ã¤ã¤ã¼ã§ãã¼ããã©ã³ã¹ããã«ã¯nginxã®configure
ã¹ã¯ãªããã«--with-stream
(ããã㯠--with-stream=dynamic
)ãä»ä¸ãã¦ãã«ãããå¿
è¦ãããã¾ãã
cd nginx-1.11.3 ./configure --with-stream make sudo make install
以ä¸ã¯TCPãã¼ããã©ã³ãµã¼åãã«æ示çã«çµè¾¼ãå¿ è¦ãããã¢ã¸ã¥ã¼ã«ã®ä¸è¦§ã§ãã
$ ./configure --help | grep with-stream --with-stream enable TCP/UDP proxy module --with-stream=dynamic enable dynamic TCP/UDP proxy module --with-stream_ssl_module enable ngx_stream_ssl_module --with-stream_geoip_module enable ngx_stream_geoip_module --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
nginxã®æ¨æºã¢ã¸ã¥ã¼ã«ã§æ示çã«çµã¿è¾¼ãå¿
è¦ãããã¢ã¸ã¥ã¼ã«ã«ã¤ãã¦ã¯ãã®ããã« --with-
ã§å§ã¾ããªãã·ã§ã³ãç¨æããã¦ãã¾ããä¸æ¹ããã©ã«ãã§çµã¿è¾¼ã¾ããã¢ã¸ã¥ã¼ã«ã«ã¤ãã¦ã¯ --without-
ã§å§ã¾ããªãã·ã§ã³ãç¨æããã¦ãããç¹å®ã®ã¢ã¸ã¥ã¼ã«ãç¡å¹ããéã«æå®ãã¾ãã
# ngx_stream_geo_moduleã¨ngx_stream_map_moduleãç¡å¹ã«ãã ./configure \ --with-stream \ --without-stream_geo_module \ --without-stream_map_module
nginxãTCPãã¼ããã©ã³ãµã¼åãã«è¨å®ãã
nginxã®TCPãã¼ããã©ã³ãµã¼è¨å®ã¯ stream
ã³ã³ããã¹ãã«è¨è¿°ãã¾ãã(ãã¼ãçªå·ãIPã¢ãã¬ã¹ã¯ãããã¼ã§ã)
http { # ãã£ã¡ãããªã } stream { error_log /var/log/nginx/stream.log info; proxy_protocol on; upstream grpc { server 192.168.0.1:12345; server 192.168.0.2:12345; } server { listen 12345; proxy_pass grpc; } }
ã¡ãªã¿ã«UDPãã¼ããã©ã³ãµã¼ã¨ãã¦åããã«ã¯ listen
ãã£ã¬ã¯ãã£ãã®å¼æ°ã« udp
ã追å ãã¾ãã
listen 1234 udp;
ãã®ããã«è¥å¹²ã®éãã¯ãããã®ã®ã¢ããã¹ããªã¼ã ããããã·ã®è¨å®æ¹æ³ã¯HTTPã®ãã¼ããã©ã³ãµã¼ã®å ´åã¨ã»ã¨ãã©åãã§ããä¸æ¹ã§ãHTTPã¬ã¤ã¤ã¼ã§ã®ãã¼ããã©ã³ãµã¼ã«æ¯ã¹ãã¨ã§ãããã¨ãéãããã®ã§æ³¨æãå¿ è¦ã§ããä¾ãã°HTTPãããã®æä½ãªããã¯å½ç¶ã§ãã¾ãããããã°ãã©ã¼ãããã®è¨å®ãHTTPã®å ´åã¨éã£ã¦ã§ãã¾ããã
追è¨(20160926)ï¼ nginx-1.11.4ããstreamã³ã³ããã¹ãã§log_formatãã£ã¬ã¯ãã£ããå©ç¨å¯è½ã«ãªãã¾ããã
次ã«nginxã®TCPãã¼ããã©ã³ãµã¼ç¹æã®è¨å®ã«ã¤ãã¦è§£èª¬ãã¦ããã¾ãã
TCPãã¼ããã©ã³ãµã¼ã§ã®ãã®ã³ã°
nginxã§ã¢ã¯ã»ã¹ãã°ãåºåããã«ã¯é常 access_log
ãã£ã¬ã¯ãã£ããå©ç¨ãã¾ãããstream
ã³ã³ããã¹ãã§ã¯ access_log
ãã£ã¬ã¯ãã£ãã使ããªãã®ã§ãããã« error_log
ãã£ã¬ã¯ãã£ããå©ç¨ãã¾ããã¾ãããã°ã¬ãã«ã¯ info
以ä¸ã§ããå¿
è¦ãããã¾ãã
追è¨(20160926)ï¼ nginx-1.11.4ããstreamã³ã³ããã¹ãã§access_logãã£ã¬ã¯ãã£ããå©ç¨å¯è½ã«ãªãã¾ããã
error_log /var/log/nginx/stream.log info;
ãã°ã¯ãããªæãã§åºåããã¾ãã
2016/08/09 11:38:16 [info] 76796#0: *4 client 127.0.0.1:63501 connected to 0.0.0.0:9999 2016/08/09 11:38:16 [info] 76796#0: *4 proxy 127.0.0.1:63502 connected to 127.0.0.1:9001 2016/08/29 11:38:16 [info] 76796#0: *4 client disconnected, bytes from/to client:78/171, bytes from/to upstream:171/78
ã¢ããã¹ããªã¼ã ã®ãµã¼ãã«æ¥ç¶å ã®IPã¢ãã¬ã¹ãä¼æ¬ãã
ä¸è¬ã«HTTPã¬ã¤ã¤ã¼ã®ãã¼ããã©ã³ãµã¼ã§ã¯ X-Forwarded-For
ã X-Real-IP
ã¨ãã£ãããããå©ç¨ãã¦ã¢ããã¹ããªã¼ã ã®ãµã¼ãã«æ¥ç¶å
ã®IPã¢ãã¬ã¹ãä¼æ¬ããã¨ãã£ããã¨ãããè¡ããã¾ãã
TCPãã¼ããã©ã³ãµã¼ã§åããã¨ãããã«ã¯PROXY protocolãå©ç¨ãã¾ããnginxã§ã¯ proxy_protocol
ãã£ã¬ã¯ãã£ãã§PROXY protocolã®ON/OFFãåãæ¿ãå¯è½ã§ããï¼ããã©ã«ãã¯OFF)
proxy_protocol on;
ãã®éãã¢ããã¹ããªã¼ã ã®ãµã¼ããPROXY protocolã«å¯¾å¿ããå¿ è¦ãããç¹ã«æ³¨æãã¾ããããã¡ã«ã«ãªã§ã¯gRPCãå©ç¨ãããµã¼ãã®å段ã«nginxãé ç½®ãã¦TCPã¬ã¤ã¤ã¼ã§ãã¼ããã©ã³ã¹ãã¦ããã®ã§ãããå½æéçºãã¦ããéã«gRPCã®ãµã¼ããPROXY protocolã«å¯¾å¿ãã¦ããªããã¨ãããã£ãã®ã§@kazegusuriã«å¯¾å¿ãã¦ããã£ããã¨ãããã¾ããã以ä¸ã®ã¹ã©ã¤ãã«ãã®ã¸ãã®è©±ãå°ãè¼ã£ã¦ãã¾ãã
GRPCã®å®è·µã¨ç¾ç¶ã§ã®å©ç¹æ¬ ç¹ / Go Conference 2016 Spring
ãµã¼ãã¹ãã¼ã¿ã¹ã®åå¾
nginxãTCPãã¼ããã©ã³ãµã¼ã¨ãã¦åããå ´åã§ãå種ãµã¼ãã¹ãã¼ã¿ã¹ã®åå¾ã¯HTTPã®å ´åã¨åãããngx_http_stub_status_moduleãå©ç¨ãã¾ãã
http { server { listen 80; location /status { stub_status on; allow 127.0.0.1; deny all; } } } stream { # ãã£ã¡ãããªã }
ãªããHTTPã®å ´åã¨éã£ã¦nginxãå¦çãã¦ããåãªã¯ã¨ã¹ãã®ã¹ãã¼ã¿ã¹ï¼ReadingãWritingãWaitingï¼ã¯ã«ã¦ã³ãããªãã®ã§æ³¨æãã¾ããããï¼ Active Connections
ãserver accepts handled requests
ã¯ã«ã¦ã³ãããã¾ãï¼
ã¾ã¨ã
nginxã«ããTCPã¬ã¤ã¤ã¼ã§ã®ãã¼ããã©ã³ã¹ã«ã¤ãã¦è§£èª¬ãã¾ãããè¥å¹²çã¯ãããã®ã®ãnginxã«æ £ãã¦ããéçºè ã§ããã°HTTPã¬ã¤ã¤ã¼ã§ãã¼ããã©ã³ã¹ããã®ã¨åããããªæè¦ã§è¨å®ã§ããã®ã§ä½¿ããããã®ã§ã¯ãªããã¨æãã¾ãã