nginx etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
nginx etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

15 Şubat 2023 Çarşamba

Dcoker Compose ve NGINX

Örnek
Şöyle yaparız
version: '3'
services:
  lb:
    build:
      context: nginx
      dockerfile: Dockerfile
    ports:
      - "9090:9090"
    networks:
      - my-network
    depends_on:
      - service1
      - service2

  service1:
    build:
      context: service1
      dockerfile: Dockerfile
    ports:
      - "8181:8080"
    networks:
      - my-network

  service2:
    build:
      context: service2
      dockerfile: Dockerfile
    ports:
      - "8282:8080"
    networks:
      - my-network

networks:
  my-network:
    driver: bridge


NGINX - nginx.conf Dosyası Load Balancer

1. upstream İle Sunucular Tanımlanır
Şöyle yaparız.
upstream app1 {
  server host.docker.internal:5000;
}

upstream app2 {
  server host.docker.internal:5001;
}

server {
  ...
}
HTTP Load Balancer
proxy_pass ile istek upstream block ile tanımlı sunucular gönderilir

Örnek
Şöyle yaparız. http://localhost:9090 adresine gelen isteklerin %10'u service1'e, %90'ı ise service2'ye gönderiliyor. http://localhost:9090;
# here we must point to the internal port of application ;) upstream servers { server service1:8080 weight=1 fail_timeout=15s; server service2:8080 weight=9 fail_timeout=15s; } server { listen 9090; location / { proxy_redirect off; proxy_pass http://servers; } }
gRPC Load Balancer
grpc_pass ile istek upstream block ile tanımlı sunucular gönderilir

Örnek
Şöyle yaparız
upstream grpcnodes {
  server ip_address:8001;
  server ip_address:8002;
  server ip_address:8003;
}
server {

  listen 1443 http2;
    ssl_certificate /home/ubuntu/http2/certificates/localhost-certificate.pem;
    ssl_certificate_key /home/ubuntu/http2/certificates/localhost-privatekey.pem;

    location / {
      grpc_pass grpcnodes;
      ##try_files $uri $uri/ =404;//Comment this else you will get 404 as a response
    }
}

9 Ekim 2022 Pazar

Docker ve NGINX

Giriş
Elle hazırlanan conf dosyası /etc/nginx/conf.d/ dizinine kopyalanır. Yeni ismi default.conf veya başka bir conf ismi olabilir

Örnek
Şöyle yaparız
FROM nginx
LABEL "Project"="Vproject"
LABEL "Author"="Onumaku chibuike"

RUN rm -rf /etc/nginx/conf.d/default.conf
COPY nginvproapp.conf /etc/nginx/conf.d/vproapp.conf
nginvproapp.conf dosyası şöyledir
upstream vproapp {
 server vproapp:8080;
}
server {
  listen 80;
  location / {
    proxy_pass http://vproapp;
  }
}
Örnek
Şöyle yaparız
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf



11 Mart 2021 Perşembe

NGINX - nginx.conf Dosyası - Limit Request

Giriş
Açıklaması şöyle
If the requests rate exceeds the rate configured for a zone, their processing is delayed such that requests are processed at a defined rate. Excessive requests are delayed until their number exceeds the maximum burst size in which case the request is terminated with an error. By default, the maximum burst size is equal to zero
Örnek
Şöyle yaparız
limit_req_zone $server_name zone=one:10m rate=100r/s;

server {
    location /search/ {
        limit_req zone=one burst=200;
    }
  ...
}

20 Ocak 2021 Çarşamba

NGINX - nginx.conf Dosyası - HTTP proxy

Giriş
NGINX kendisine gelen istekleri bir başka adrese yönlendirir (forward). Yani sunucularımız önüne koyduğumuz ve kendisine gelen istekleri sunucularımıza yönlendiren reverse proxy olarak çalışır.

upstream kavramı
Reverse proxy kullanırken bazen karşımıza upstream kelimesi çıkar. Açıklaması şöyle. Yani upstream ile kendi sunucumuz kastediliyor.
In the context of a reverse proxy server, an upstream server is a server that the reverse proxy forwards requests to. The upstream server can be any server that can handle the request, such as a web server, application server, or microservice.
server_name Alanı
server_name ile Nginx'in dinleyeceği adres belirtilir. Bu adresin altına açılan location XXX şeklindeki alanlarla server_name için gelen isteklerin nereye yönlendirileceği belirtilir. proxy_pass ile yönlendirilecek adres belirtilir.

Böylece hem Reverse Proxy, hem de Forward Proxy olarak görev yapabilir.

Örnek - forward proxy
Şöyle yaparız
server {
  listen 81;

  location / {
    resolver 8.8.8.8;
    proxy_http_version 1.1;
    proxy_pass https://$host$request_uri;
  }
}
Örnek - reverse proxy
Aynı birden fazla virtual server çalışıyorsa şöyle yaparız.
server {
  server_name device1.example.com;
  location / {
    proxy_pass http://192.168.0.1:80;
  }
}
server {
  server_name device2.example.com;
  location / {
    proxy_pass http://192.168.0.2:80;
  }
}
server {
  server_name device3.example.com;
  location / {
    proxy_pass http://192.168.0.3:80;
  }
}
Örnek - reverse proxy
Şöyle yaparız. Burada niçin proxy_set_header  alanlarını atamak lazım bir gün öğrenirsem yazarım.
worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Host $server_name;
    proxy_set_header   X-Forwarded-Port $server_port;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   X-Forwarded-Prefix $contextpath;
    proxy_set_header   X-Request-Id $pid-$msec-$remote_addr-$request_length;

    server {
        listen 8080;

        resolver 127.0.0.11 valid=30s;

        location /messaging {
            set $upstream messaging-api;
            set $contextpath /messaging;

            client_max_body_size 50M;
            rewrite            ^/messaging(/|$)(.*) /$2 break;
            proxy_pass         http://$upstream:8080;
            proxy_redirect     off;
        }
    }
}
Örnek - reverse proxy + url rewrite
Şöyle yaparız. Burada "rewrite ^/lekana(.*)$ $1 break;" ile url içindeki lekana siliniyor.
server {
    listen          80;
    server_name     bassa.com www.bassa.com;

    location / {
        proxy_pass http://bassa-api:7656;
    }

    location /lekana {
        rewrite ^/lekana(.*)$ $1 break;
        proxy_pass http://lekana-api:7654;
    }

    location /siddhi {
        rewrite ^/siddhi(.*)$ $1 break;
        proxy_pass http://siddhi-api:7655;
    }
}

15 Haziran 2020 Pazartesi

NGINX - nginx.conf Dosyası

nginx komutu
-g seçeneği
Set global configuration directives anlamına gelir.
Örnek
Docker içinde nginx daemon gibi çalışmamalıdır. Bunun için şöyle yaparız. Böylece ön planda çalışır
nginx.exe -g daemon off
-s seçeneği
Send signal to the master process anlamına gelir.
Örnek
nginx sunucusunu durdurmak için Task Manager' dan öldürmeye gerek yok. Şöyle yaparız
nginx.exe -s quit
nginx.conf Yolu
Windows'ta bu dosya şuna benzer bir yerde.
C:\nginx-1.15.1\conf\nginx.conf
Directive Çeşitleri
Açıklaması şöyle. İki çeşit directive var
1. Simple Directive : listen 80 gibidir
2. Block Directive {...} şeklindedir. Örneğin http{...} bir block directive
The configuration file consists of directives that form the modules or contexts. There are two kinds of directives: simple directives and block directives. A simple directive has names and parameters separated by a space and ends with a semicolon like this listen 80; . A block directive is the same but has additional information and surrounded by braces like this { listen 80; root /usr/share/nginx/html; }.
worker_process Directive
Açıklaması şöyle.
NGINX processes are divided into one master process and several worker processes. The master process takes care of evaluating configuration and maintaining worker processes and the worker processes take care of actual requests. We can define the number of worker processes in the configuration file ...
http Block
http scope içinde sadece http trafiği ile ilgilenilir ve birden fazla server açılabilir.
Örnek - Tek react uygulaması
Şöyle yaparız. Root Dosya "html/index.html" dosyasıdır
worker_process 1;

events {
worker_connections 1024;
}
http {
  include mime.types
  default_type application/octet-stream
  sendfile on;
  server {
    listen 80;
    server_name localhost;
    location / {
      root html;
      try_file $uri /index.html;
    }
    location = /50x.html {
      root html;
    }
} }
Örnek - İki react uygulaması
Şöyle yaparız. Burada farklı portları dinleyen iki tane react uygulaması var
worker_process 1;

events
  worker_connections 1024;
}

http {
  include mime.types
  default_type application/octet-stream
  sendfile on;
  keepalive_timeout 65;
  server {
    listen 8080;
    server_name localhost;
    location / {
      root html;
      try_file $uri /index.html;
    }
    location = /50x.html {
      root html;
    }
  }
  server {
    listen 8081;
    server_name localhost;
    location / {
      root htmldemo;
      try_file $uri /index.html;
    }
    location = /50x.html {
      root htmldemo;
    }
  }
}
Örnek
Http load balancer için şöyle yaparız.
http {
  server {
    listen 0.0.0.0:443 ssl;
    include /etc/nginx/snippets/letsencrypt.conf;
    root /var/www/html;
    server_name XXXX;

    location / {
        proxy_pass http://rancher_servers_http;
    }
    location /.well-known/carddav {
      proxy_pass http://$host:$server_port/remote.php/dav;
    }
    location /.well-known/caldav {
      proxy_pass http://$host:$server_port/remote.php/dav;
    }
  }

  server {
    listen 80 default_server;
    listen [::]:80 default_server;

    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
        root /var/www/letsencrypt;
    }
    root /var/www/html;
    server_name xxxx;

    location / {
        proxy_pass http://rancher_servers_http;
    }
  }
}
http/include mime.types
Açıklaması şöyle.
We should include this directive in the nginx.conf file otherwise all the styles are rendered as plain text in the browser.
http/server/access_log seçeneği
Örnek
Şöyle yaparız
# nginx.conf
http {
  server {
    listen              443 ssl;
    server_name         myapp.com;
    charset utf-8;
    access_log off;
   
    location / {
      proxy_pass http://myapp:8080;
    }
  }
}
events { worker_connections 1024; }
http/server/charset seçeneği
utf-8 verilir.
Örnek
Şöyle yaparız
#nginx.conf http { server { listen 443 ssl; server_name myapp.com; ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; charset utf-8; access_log off; location / { proxy_pass http://myapp:8080; } } } events { worker_connections 1024; }
http/server/listen seçeneği
Dinlenilecek port numaraları belirtilir. Birden fazla numara verilebilir.

Örneğin reverse proxy olarak kullanıyorsak dışarıdan gelen trafiği dinleyen ve içeriden gelen trafiği dinleyen iki tane port gerekir.

Örnek
Şöyle yaparız.
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

}
http/server/gzip seçeneği
Örnek
Şöyle yaparız
server {
  listen 80;
  server_name localhost;

  gzip              on;
  gzip_comp_level   2;
  gzip_min_length   1024;
  gzip_vary         on;
  gzip_proxied      expired no-cache no-store private auth;
  gzip_types        application/x-javascript application/javascript application/xml
application/json text/xml text/css text$
  ...
}
http/server/location seçeneği
Açıklaması şöyle
(none): If no modifiers are present, the location is interpreted as a prefix match. This means that the location given will be matched against the beginning of the request URI to determine a match.

=: If an equal sign is used, this block will be considered a match if the request URI exactly matches the location given.

~: If a tilde modifier is present, this location will be interpreted as a case-sensitive regular expression match.

~*: If a tilde and asterisk modifier is used, the location block will be interpreted as a case-insensitive regular expression match.

^~: If a carat and tilde modifier is present, and if this block is selected as the best non-regular expression match, regular expression matching will not take place.
Örnek
Şöyle yaparız. /root altındaki hrer şeye 404 döndürülür. Ancak ^~ ile belirtilen adres ve altındaki her şeye cevap verilir
http {
  server {
    listen 80;
    server_name rtmp.example.com;

    location ^~ /.well-known/acme-challenge/ {
      root /var/www/letsencrypt;
    }
    location / {
      return 404;
    }
  }
}
http/server/location/root seçeneği
Kendi diskimizdeki başlangıç dizinini yerini temsil eder.

http/server/location/proxy_pass seçeneği
HTTP Proxy yazısına taşıdım

http/server/server_name seçeneği
Aynı bilgisayarda koşan "virtual server" lar için kullanılır. 

Örnek
reverse proxy için şöyle yaparız. / altındaki her şeye cevap verilir.
server {
  server_name device1.example.com;
  location / {
    proxy_pass http://192.168.0.1:80;
  }
}
server {
  server_name device2.example.com;
  location / {
    proxy_pass http://192.168.0.2:80;
  }
}
server {
  server_name device3.example.com;
  location / {
    proxy_pass http://192.168.0.3:80;
  }
}
http/server/ssl seçenekler
- ssl_certificate ile sertifika dosyası
- ssl_certificate_key ile private key dosyası belirtilir.

Örnek
Şöyle yaparız.
server {
  listen 443 ssl;
  listen 8080;

  ssl_certificate /etc/asterisk/certs/example.crt;
  ssl_certificate_key /etc/asterisk/certs/example.key;
  ssl_session_timeout 5m;
  ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers  HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;


  location / {

    # prevents 502 bad gateway error
    proxy_buffers 8 32k;
    proxy_buffer_size 64k;

    # redirect all HTTP traffic to localhost:8000;
    proxy_pass http://${GATEWAY}:8000;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    #proxy_set_header X-NginX-Proxy true;
    # enables WS support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 999999999;
  }
}
Açıklaması şöyle
The above example nginx.conf adds uses a certificate file named example.crt and a key file named example.key. Any certificate key-pair from a trusted source will work. Simply needs to be placed in a safe location that is accessible to NGINX. The acceptable protocols are explicitly set using the ssl_protocols directive, and the allowed ciphers are set with the ssl_ciphers directive. The two proxy_set_header directives are the ones responsible for upgrading the connection. Also, since WS and WSS connections support only HTTP 1.1, another directive called proxy_http_version sets the HTTP version to 1.1.