{"id":3779,"date":"2017-11-17T15:47:28","date_gmt":"2017-11-17T11:47:28","guid":{"rendered":"https:\/\/nayarweb.com\/blog\/?p=3779"},"modified":"2017-11-17T15:48:41","modified_gmt":"2017-11-17T11:48:41","slug":"enabling-udp-load-balancing-with-nginx-on-debian-9-stretch","status":"publish","type":"post","link":"https:\/\/nayarweb.com\/blog\/2017\/enabling-udp-load-balancing-with-nginx-on-debian-9-stretch\/","title":{"rendered":"Enabling UDP load-balancing with Nginx on Debian 9 (Stretch)"},"content":{"rendered":"<p>User Datagram Protocol (UDP) is commonly used for DNS resolution and video\/voice streaming applications. The advantage of UDP over TCP is that it has less overhead (smaller packet size). You can therefore send more data on your network with less latency. However this comes at the expense of data reliability.<\/p>\n<p>Lemme guide you to how setup an Nginx server (10.0.0.5) which forwards UDP packets from port 514 to a Graylog server (10.0.0.10) on port 514 itself. We will be sending logs from a VM on 10.0.0.2<\/p>\n<p>On your nginx server:<\/p>\n<blockquote><p># echo &#8220;deb http:\/\/nginx.org\/packages\/debian\/ stretch nginx&#8221; &gt; \/etc\/apt\/sources.list.d\/nginx.list<br \/>\n# apt-get update<br \/>\n# apt-get install nginx<\/p><\/blockquote>\n<p>You should now have nginx installed. Paste the following snippet in your `\/etc\/nginx\/nginx.conf`<\/p>\n<blockquote>\n<pre>stream {\r\n  upstream graylog_upstreams {\r\n    server 10.0.0.10:514;\r\n  }\r\n\r\n  server {\r\n    listen 514 udp;\r\n    proxy_pass graylog_upstreams;\r\n    proxy_responses 0;\r\n    proxy_bind $remote_addr transparent;\r\n  }\r\n}<\/pre>\n<\/blockquote>\n<p>Check if Nginx is listening on UDP port 514<\/p>\n<blockquote>\n<pre>root@prod-r7-nginx:~# ss -ntplu\r\n<\/pre>\n<\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3782\" src=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/11\/Screenshot_20171117_153427.png\" alt=\"\" width=\"950\" height=\"34\" srcset=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/11\/Screenshot_20171117_153427.png 950w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/11\/Screenshot_20171117_153427-300x11.png 300w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/11\/Screenshot_20171117_153427-768x27.png 768w\" sizes=\"auto, (max-width: 950px) 100vw, 950px\" \/><\/p>\n<p>However if you sending data to this port from another machine, you&#8217;ll notice that no data is sent to the backend server. Despite `tcpdump` will see the data coming and being sent.<\/p>\n<blockquote>\n<pre>IP 10.0.0.2.38696 &gt; 10.0.0.5.514: SYSLOG kernel.info, length: 120\r\nIP 10.0.0.2.45605 &gt; 10.0.0.10.514: SYSLOG kernel.info, length: 120<\/pre>\n<\/blockquote>\n<p>We need to tell the kernel to actually route IP addresses which doesn&#8217;t belong to him thus acting like a router. We do so by the following command<\/p>\n<blockquote>\n<pre>sed -i \"s\/#net.ipv4.ip_forward.*\/net.ipv4.ip_forward=1\/\" \/etc\/sysctl.conf\r\necho 1 &gt; \/proc\/sys\/net\/ipv4\/ip_forward\r\n<\/pre>\n<\/blockquote>\n<p>And that&#8217;s it \ud83d\ude42<\/p>\n<p>Do you like nginx&#8217;s UDP loadbalancing feature?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>User Datagram Protocol (UDP) is commonly used for DNS resolution and video\/voice streaming applications. The advantage of UDP over TCP is that it has less overhead (smaller packet size). You can therefore send more data on your network with less latency. However this comes at the expense of data reliability. Lemme guide you to how &hellip; <a href=\"https:\/\/nayarweb.com\/blog\/2017\/enabling-udp-load-balancing-with-nginx-on-debian-9-stretch\/\" class=\"continue-reading\">Continue reading <span class=\"screen-reader-text\">Enabling UDP load-balancing with Nginx on Debian 9 (Stretch)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[210],"tags":[],"class_list":["post-3779","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/3779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/comments?post=3779"}],"version-history":[{"count":6,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/3779\/revisions"}],"predecessor-version":[{"id":3786,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/3779\/revisions\/3786"}],"wp:attachment":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/media?parent=3779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/categories?post=3779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/tags?post=3779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}