Code Snippets Nginx

Short texts Nginx 🧑‍💻 Code snippets Nginx ✍️ Reading lists Nginx 👀

Automatic nginx virtual subdomains with sub-folders or sub-directories

# Automatic nginx virtual subdomains with sub-folders or sub-directories # # Since the original source where I found this code is now offline, I have decided to mirror it here. # All credit goes to: http://web.archive.org/web/20150307193208/http://www.messaliberty.com/2010/10/automatic-nginx-virtual-subdomains-with-sub-folders-or-sub-directories # # Description: In my web root directory I wanted create a folder called photos, and another called # […]

Prometheus metrics for NGINX OSS using stub_status and js_body_filter

export default { metrics, nolength } var res = “”; var buf = 0; function metrics(r, data, flags) { if (data.length) buf++; res += data; // Collect the entire response, if (flags.last) { // until we get the last byte. var metrics = []; var connection_vars = [‘active’, ‘reading’, ‘writing’, ‘waiting’]; connection_vars.forEach(m => metrics.push(“connections{” + […]

NGINX reverse proxy configuration for multiple nodes but using same domain

upstream api_auth { server api_auth:3001; keepalive 256; } upstream api_article { server api_article:3002; keepalive 256; } server { listen 3000; location / { try_files $uri @api_auth; } location @api_auth { proxy_pass http://api_auth; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection “upgrade”; error_page 404 =200 @api_article; } location […]

nginx.conf

user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; 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; keepalive_timeout 65; server_tokens off; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.php index.html […]

Deploying NGINX Plus as an API Gateway, Part 1

upstream warehouse_inventory { zone inventory_service 64k; server 10.0.0.1:80; server 10.0.0.2:80; server 10.0.0.3:80; } upstream warehouse_pricing { zone pricing_service 64k; server 10.0.0.7:80; server 10.0.0.8:80; server 10.0.0.9:80; } # vim: syntax=nginx include api_backends.conf; include api_keys.conf; server { access_log /var/log/nginx/api_access.log main; # Each API may also log to a # separate file listen 443 ssl; server_name api.example.com; # […]