├── README.md ├── conf.d ├── dashboard.conf ├── lb.conf └── web.conf └── nginx.conf /README.md: -------------------------------------------------------------------------------- 1 | # load-balancing 2 | This is the supporting GitHub link for the YouTube tutorial on configuring NGINX/Plus as Load Balancer 3 | 4 | 5 | I have commented out all the load-balancing methods, so the default, round-robin would work with this configuration as is. 6 | 7 | While Loop which I used to extract and print a specific value from the header: 8 | 9 | while true; do curl -sI | tr -d '\r' | sed -En 's/^Custom-Header: (.*)/\1/p'; sleep 1; done 10 | 11 | 12 | 13 | ##### Useful Links: 14 | * [NGINX Plus - Load Balancing](https://www.nginx.com/products/nginx/load-balancing) 15 | * [NGINX Plus - Technical Documents - HTTP Load Balancing](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/) 16 | * [NGINX - Technical Docs - Upstream Directive](http://nginx.org/en/docs/http/ngx_http_upstream_module.html) 17 | 18 | 19 | 20 | ## Built With 21 | 22 | * [Ubuntu](https://ubuntu.com/) - My favourite Linux OS for testing 23 | * [NGINX Plus](https://www.nginx.com/free-trial-request/) - NGINX Plus Trial 24 | 25 | 26 | ## Author 27 | 28 | * **Jay Desai** - *Other Repos* - [jay-nginx](https://github.com/jay-nginx) 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /conf.d/dashboard.conf: -------------------------------------------------------------------------------- 1 | server { 2 | 3 | listen 8080; 4 | 5 | location /api { 6 | api write=on; 7 | allow all; 8 | } 9 | 10 | location / { 11 | root /usr/share/nginx/html; 12 | index dashboard.html; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /conf.d/lb.conf: -------------------------------------------------------------------------------- 1 | 2 | upstream backend_servers { 3 | 4 | #hash $request_uri; 5 | #ip_hash; 6 | #least_conn; 7 | zone backend_server_zone 64k; 8 | server 127.0.0.1:9001; 9 | server 127.0.0.1:9002; 10 | server 127.0.0.1:9003; 11 | #sticky cookie lb_example expires=1h; 12 | } 13 | 14 | server { 15 | listen 80; 16 | server_name www.example.com; 17 | 18 | proxy_set_header Host $host; 19 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 20 | proxy_set_header X-Real-IP $remote_addr; 21 | 22 | location / { 23 | proxy_pass http://backend_servers/; 24 | 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /conf.d/web.conf: -------------------------------------------------------------------------------- 1 | server { 2 | 3 | listen 9001; 4 | index index.html; 5 | add_header Custom-Header "Application 1"; 6 | 7 | location / { 8 | root /opt/services/App1; 9 | 10 | } 11 | } 12 | 13 | server { 14 | 15 | listen 9002; 16 | index index.html; 17 | add_header Custom-Header "Application 2"; 18 | 19 | location / { 20 | root /opt/services/App2; 21 | } 22 | } 23 | 24 | server { 25 | 26 | listen 9003; 27 | index index.html; 28 | add_header Custom-Header "Application 3"; 29 | 30 | location / { 31 | root /opt/services/App3; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /nginx.conf: -------------------------------------------------------------------------------- 1 | user nginx; 2 | worker_processes auto; 3 | 4 | error_log /var/log/nginx/error.log notice; 5 | pid /var/run/nginx.pid; 6 | 7 | 8 | events { 9 | worker_connections 1024; 10 | } 11 | 12 | 13 | http { 14 | include /etc/nginx/mime.types; 15 | default_type application/octet-stream; 16 | 17 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 18 | '$status $body_bytes_sent "$http_referer" ' 19 | '"$http_user_agent" "$http_x_forwarded_for"'; 20 | 21 | access_log /var/log/nginx/access.log main; 22 | 23 | sendfile on; 24 | 25 | keepalive_timeout 65; 26 | 27 | 28 | include /etc/nginx/conf.d/*.conf; 29 | } 30 | --------------------------------------------------------------------------------