NGINX

Category: Youtube Created time: December 15, 2022 11:55 PM Source: FullCycle Status: Done Tags: Nginx Type: Page

Nginx: do básico ao Load Balancer

Advanced Load Balancer, Web Server, & Reverse Proxy - NGINX

Untitled

Open source software that has several uses such as: Webserver, reverse proxy, caching, load balancer, media streaming, as well as proxy for email services.

Main features

  • One of the most powerful web servers on the market
  • Performs better than Apache
  • Can support high volume of simultaneous connections
  • Uses low memory and supports high concurrency
  • Usually used as a webserver and reverse proxy
  • More than 50% of the most accessed websites in the world use Nginx
  • Supports the most modern web resources such as Websockets, HITP 2, video streaming, etc.

How it works

Untitled

Untitled

Reverse proxy

Reverse proxy is an intermediary service that receives requests from different clients, processes them and forwards them to the corresponding service.

Untitled

Load Balancing

Untitled

📝 Goals Commits

https://github.com/adlerweb3/nginx-load_balancer

⚽ [GOAL] NGINX | Reverse Proxy: 1st container DONE

⚽ [GOAL] NGINX | Node1: 2nd container DONE

⚽ [GOAL] NGINX | Node2: 3rd container and NGINX Load Balancer DONE

⚽ [GOAL] NGINX | Enable Logs DONE

NGINX | Reverse Proxy

Docker | 📂 docker-compose.yml

version: '3'

services:

  nginx:
    image: nginx:alpine
    container_name: nginx
    ports:
      - "8000:80" #Libera a porta 8000 do localhost e direciona para porta 80 do nginx

CLI

# run nginx containers
docker compose up -d

# install bash and vim inside nginx container
docker compose exec nginx apk add bash vim

# Enter to nginx container bash
docker compose exec nginx bash
⚠️ Use CLI `nginx -t` to confirm it’s all ok. ⚠️ Use CLI `nginx -s reload` to restart nginx with updates.

NGINX base configuration file /etc/nginx/nginx.conf

Focused for system use/tunning.

# open nginx.conf file inside nginx container
vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

The settings of the site(s) must be placed in separate files through includes.

include /etc/nginx/conf.d/*.conf

Initial installation comes with a default file:

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

In this case, it is redirecting to the Welcome page of NGINX

Untitled

Edited configuration file for testing:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
⚠️ Use CLI `nginx -t` to confirm it’s all ok. ⚠️ Use CLI `nginx -s reload` to restart nginx with updates.

Untitled

Untitled

First NGINX Container Configured

NGINX | Node1

Docker | 📂 docker-compose.yml

version: '3'

services:

  nginx:
    image: nginx:alpine
    container_name: nginx
    ports:
      - "8000:80" #Libera a porta 8000 do localhost e direciona para porta 80 do nginx

  node1:
    image: nginx:alpine
    container_name: node1
    ports:
      - "80" #Libera apenas a porta 80 do nginx

CLI

# run nginx containers
docker compose up -d

# install bash and vim inside nginx container
docker compose exec node1 apk add bash vim

# Enter to nginx container bash
docker compose exec node1 bash

Edit to identify Node1 correctly:

vim /usr/share/nginx/html/index.html
<h1> NODE 1 </h1>

ByPassing 1st NGINX to 2nd

server {
    listen       80;
    server_name  localhost;

    location / {
        # root   /usr/share/nginx/html;
        # index  index.html index.htm;

        # byPassing to another container || nginx || service ...
        proxy_pass http://node1;
    }
}

NGINX - default.conf

⚠️ Use CLI `nginx -t` to confirm it’s all ok. ⚠️ Use CLI `nginx -s reload` to restart nginx with updates.

Untitled

NGINX | Node2

Docker | 📂 docker-compose.yml

version: '3'

services:

  nginx:
    image: nginx:alpine
    container_name: nginx
    ports:
      - "8000:80" # Release localhost port 8000 and direct to nginx port 80

  node1:
    image: nginx:alpine
    container_name: node1
    ports:
      - "80" # Release only nginx port 80

  node2:
    image: nginx:alpine
    container_name: node2
    ports:
      - "80" # Release only nginx port 80

Edit to identify Node2 correctly:

vim /usr/share/nginx/html/index.html
<h1> NODE 2 </h1>

NGINX | Load Balancer

upstream nodes {
    server node1;
    server node2;
}

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://nodes;
    }
}
⚠️ Use CLI `nginx -t` to confirm it’s all ok. ⚠️ Use CLI `nginx -s reload` to restart nginx with updates.

Logs

All access to NGINX create a log

upstream nodes {
    server node1;
    server node2;
}

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://nodes;
    }

    access_log /var/log/nginx/nginx-access.log;
}
⚠️ Use CLI `nginx -t` to confirm it’s all ok. ⚠️ Use CLI `nginx -s reload` to restart nginx with updates.
path: /var/log/nginx/access.log

Untitled

Tunning ON Logs

Node1

nano /etc/nginx/conf.d/default.conf

Untitled

access_log  /var/log/nginx/nginx-access.log main;
tail —f /var/log/nginx/nginx—access.log

IP Configs

Passing the access external IPs and not the NGINX PROXY IP

The IPs logged into the NODES are those of the NGINX PROXY

Untitled

Untitled

NGINX | Load Balancer

upstream nodes {
    server node1;
    server node2;
}

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://nodes;
        proxy_set_header X—Real—IP $remote_addr;
    }

    access_log /var/log/nginx/nginx-access.log;
}
⚠️ Use CLI `nginx -t` to confirm it’s all ok. ⚠️ Use CLI `nginx -s reload` to restart nginx with updates.

Node1 && Node2

📂 nano /etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    log_format  main  '$http_x_real_ip - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}