BCards-Scripts-Server/nginx/nginx.conf
2025-07-22 21:24:07 -03:00

239 lines
7.0 KiB
Nginx Configuration File

# NGINX Configuration for BCards Load Balancer
# Configuração principal para produção com SSL e load balancing
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
# Otimizações de performance
worker_rlimit_nofile 65535;
events {
worker_connections 4096;
use epoll;
multi_accept on;
}
http {
# Configurações básicas
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Logging format
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time uct="$upstream_connect_time" '
'uht="$upstream_header_time" urt="$upstream_response_time"';
access_log /var/log/nginx/access.log main;
# Performance settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 20M;
# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml+rss
application/atom+xml
image/svg+xml;
# Rate limiting
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;
# Upstream configuration for load balancing
upstream bcards_backend {
# Configuração de load balancing com health checks
least_conn;
# Servidores backend
server 172.18.0.2:8080 max_fails=3 fail_timeout=30s;
server 172.18.0.3:8080 max_fails=3 fail_timeout=30s;
# Manter conexões vivas
keepalive 32;
}
# Configuração para health checks dos backends
upstream bcards_health {
server 172.18.0.2:8080;
server 172.18.0.3:8080;
}
# Rate limiting
limit_req_zone $binary_remote_addr zone=general:10m rate=10r/s;
# Configuração SSL/TLS
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# Security headers
add_header X-Frame-Options DENY always;
add_header X-Content-Type-Options nosniff always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# Redirect HTTP to HTTPS
server {
listen 80;
server_name bcards.site www.bcards.site;
# ACME challenge para Let's Encrypt
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
# Redirect para HTTPS
location / {
return 301 https://$server_name$request_uri;
}
}
# Main HTTPS server configuration
server {
listen 443 ssl http2;
server_name bcards.site www.bcards.site;
# SSL certificates
ssl_certificate /etc/letsencrypt/live/bcards.site/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/bcards.site/privkey.pem;
# Trusted certificate for OCSP stapling
ssl_trusted_certificate /etc/letsencrypt/live/bcards.site/chain.pem;
ssl_stapling on;
ssl_stapling_verify on;
# DNS resolver
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Rate limiting
limit_req zone=general burst=20 nodelay;
# Health check endpoint (não passar pelo backend)
location = /nginx-health {
access_log off;
return 200 "nginx ok\n";
add_header Content-Type text/plain;
}
# Backend health check
location = /backend-health {
access_log off;
proxy_pass http://bcards_health/health;
proxy_connect_timeout 5s;
proxy_read_timeout 5s;
}
# Main application proxy
location / {
# Proxy para o backend
proxy_pass http://bcards_backend;
# Headers para proxy reverso
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-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# Timeouts
proxy_connect_timeout 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
# Buffer settings
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
# Retry configuration
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_next_upstream_tries 2;
proxy_next_upstream_timeout 10s;
}
# Static files caching
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
proxy_pass http://bcards_backend;
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-Proto $scheme;
# Cache headers
expires 1y;
add_header Cache-Control "public, immutable";
# Disable logging for static files
access_log off;
}
# Error pages
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# Servidor para monitoramento interno (apenas localhost)
server {
listen 127.0.0.1:8081;
server_name localhost;
# Status do NGINX
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
# Status detalhado dos upstreams
location /upstream_status {
access_log off;
allow 127.0.0.1;
deny all;
return 200 "Backend Status Check\n";
add_header Content-Type text/plain;
}
}
}
# Stream configuration for TCP load balancing (se necessário)
# stream {
# upstream tcp_backend {
# server 172.18.0.2:9000;
# server 172.18.0.3:9000;
# }
#
# server {
# listen 9000;
# proxy_pass tcp_backend;
# proxy_timeout 1s;
# proxy_responses 1;
# }
# }