[HTTPS] Two-way SSL – SSL雙向驗證 on Nginx

Intro Two-way SSL即Server端也要求Client端提供certificate做驗證,handshake流程上是Client端先驗證Server端後才換Server端驗證Client端。 圖片來源:web-service-principles – HTTP Client端憑證 Client端提供終端憑證並設定使用該憑證對應的Private Key; Server端設定Client端的CA憑證,以用於驗證Client端提供的終端憑證。 [Server] TLS/SSL憑證(Certificate)常用指令 – 製作CSR Nginx設定 Nginx可以在Server block上啟用驗證Client端certificate: server { listen 443 ssl; ssl_certificate /etc/nginx/ssl/public.crt; ssl_certificate_key /etc/nginx/ssl/private.rsa; # Client certificate verification ssl_client_certificate /etc/nginx/ssl/client_ca.pem; ssl_verify_client on; server_name api.my_domain.com; location / { # […]

[Web] Nginx limit_req 指南 – ngx_http_limit_req_module

Intro Nginx限制訪問速率模組:Nginx http_limit_req 原理 使用leaky bucket原理(可以稱漏桶原理),桶的概念如下: 桶中存放request (令牌的概念) 桶上方由Nginx照設定速率計算補充request 桶下方提供提取request,即一個Request會消耗掉桶中存放的一個request Nginx對於桶的特性如下: 單一IP對應使用一個桶 limit_req_zone中rate是以request數量回推時間做補充的,依照兩次領取的時差補充相應的量 limit_req中burst即定義桶的request最大存放量,在每個新IP(不在Zone中)進入時會拿到全滿request(burst量)的桶 重啟Nginx會清空Zone,意即所有桶的剩餘Quota記憶清除,重新以全滿計算 比照實際設定,limit_req_zone $binary_remote_addr zone=one:10m rate=6r/m;即每間隔10秒可以得到一個request補充量。以rate=100r/s速率來說明,即每0.01秒得一個request補充量,間距0.1秒計算得10個request補充量; 而limit_req zone=one burst=5 nodelay;則表示每個IP桶最大預存的request量為5,有用掉則需等待補充。 Nginx實際上實作leaky bucket補充的方式可能為Trigger回填間隔補充量方式,本篇僅為概念說明,不代表實際Nginx演算法 Reference Nginx下limit_req模块burst参数超详细解析

[Network] Fail2ban 指南 – 搭配 SSH/Nginx (DDoS、Request Limit)

Outline SSH 設定方針 Nginx 防禦設定方針 Access log Error log by llimit_req_zone 進階 Fail2ban 設定 Email Notifications * Intro Fail2ban – Manuals 架構 Fail2ban 是依照設定檔執行過濾,原始設定檔路徑: /etc/fail2ban/jail.conf 如要修改新增可以複製建立成local的設定檔名稱,如有此檔會以此檔優先: /etc/fail2ban/jail.local /etc/fail2ban/jail.d/* Ubuntu Fail2ban 0.9 版本中可以找到預設的/etc/fail2ban/jail.d/defaults-debian.conf,內容是對[sshd]做enabled = true。 所以安裝好後預設即會開啟SSH保護(預設port為22) [過濾器名稱]會對應到/etc/fail2ban/filter.d/過濾器名稱.conf。 參數解析 官方文件 – Options […]

[PHP] PHP-FPM Pool Socket (Multi-Sock-User) 設定指南

Intro 情境:想要某個Site的PHP-FPM改變執行使用者(例如Webhook trigger特殊程式) 設定方式 概念上,一個pool sock擁有一組設定,包含執行使用者(user)或群組(group),所以可以利用開立多個pool sock來提供給Web server多種PHP-FPM Conf達到應用。 1. 開立新的pool PHP-FPM Pool設定複製原始www.conf出新的自定義設定檔,將listen sock自定義改掉,讓var/run/php下自動產生新定義的socket,例如/etc/php/7.0/fpm/pool.d/deployer.conf: ; pool name (‘www’ here) [deployer] user = deployer group = www-data listen = /run/php/php7.0-fpm_deployer.sock 設定完後restart PHP-FPM即會自動生成對應的新Sock 2. 提供新的Socket給Web server site 以Nginx為例,換上新的Socket: location ~ \.php$ { […]

[Web Server] Nginx/Apache 環境變數設定 (Environment Variables)

Intro Nginx/Apache 環境變數設定速查 Nginx設定 fastcgi_param RUNTIME_ENVIROMENT ‘DEV’ For example: server { listen 80; root /var/www/html; index index.php; server_name localhost; location / { index index.php; } location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param RUNTIME_ENVIROMENT ‘DEV’ } } Nginx 加入 […]

[Nginx] HTTP to HTTPS – 80 to 443 – 自動轉導

設定檔 這裡拿一個非Default的Site Host作範例。 添加80port的設定檔範例: server { listen 80; listen [::]:80; server_name code.yidas.com; return 301 https://$server_name$request_uri; } 至於原來的設定檔則由80改至443: #listen 80; #listen [::]:80; # SSL configuration # listen 443 ssl; listen [::]:443 ssl;

[Nginx] 使用Proxy實作Load Balancer

Nginx設定: 直接給範例: # nginx conf http { upstream myapp1 { server srv1.example.com; server srv2.example.com; server srv3.example.com; } server { listen 80; location / { proxy_pass http://myapp1; } } } 簡而言之,在Http設定內註冊Upstream,在Server內使用Proxy Pass調用該Upstream Server Group。 分派演算法: round-robin — requests to the application […]