Nginx
HTTPS & SSL
Terminate TLS with certificates and HTTP/2.
By EZ4Code Team
ssltlshttps
Code
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.pem;
ssl_certificate_key /etc/ssl/private/example.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
# HSTS
add_header Strict-Transport-Security "max-age=31536000" always;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
# Redirect HTTP to HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}Explanation
Nginx terminates TLS by holding the certificate and key and serving HTTPS to clients while talking plain HTTP to backends. ssl_protocols restricts to modern TLS versions, and HSTS forces browsers to use HTTPS for a year. A separate 80-listening server redirects all HTTP traffic to HTTPS.
More Nginx Snippets
Server Block
Configure a virtual host with root, index, and try_files.
Reverse Proxy
Forward requests to upstream HTTP services.
Load Balancing
Distribute traffic across multiple upstream servers.
Location Rules
Match request URIs with prefix, regex, and exact locations.
Rewrite & Redirect
Rewrite URIs and issue HTTP redirects.
Gzip Compression
Compress text responses to reduce bandwidth.