Skip to main content
In this lesson we’ll cover essential web security concepts and practical Nginx configurations you can apply in hands-on exercises. The goal is to give you a pragmatic toolkit for securing local development and preparing you for production deployment patterns. Summary of what you’ll learn
  • Why HTTPS matters and why plain HTTP is insecure
  • How TLS (formerly SSL) protects data in transit
  • Key HTTP security headers and how to add them in Nginx
  • How to protect site areas with Nginx basic authentication
  • How to allow/block traffic with allow/deny and an introduction to Fail2Ban
Why HTTPS matters
  • HTTPS encrypts traffic between client and server, preventing eavesdropping and tampering.
  • Modern browsers mark plain HTTP sites as insecure and will limit functionality (e.g., geolocation, service workers).
  • TLS also enables server identity via certificates, which helps prevent man-in-the-middle attacks.
For demos and local hands-on exercises in this material we use mkcert to generate locally trusted TLS certificates: mkcert. In production you would normally use a public CA such as Let’s Encrypt with an automation tool like Certbot. That approach requires control of a public domain and DNS — something most learners don’t have for local exercises.
For local development and hands-on exercises, mkcert provides a convenient way to create certificates trusted by your machine. For production deployments, use a public CA like Let’s Encrypt with Certbot.
Quick mkcert usage (local)
  • Install mkcert following the project README.
  • Create a local CA and generate a certificate for localhost:
  • The generated localhost+2.pem and localhost+2-key.pem (filenames may vary) can be referenced in your Nginx ssl_certificate and ssl_certificate_key directives for local testing.
A presentation slide titled "Objectives" with a teal gradient sidebar and three numbered items about web security: why HTTPS matters, how SSL/TLS secures data, and HTTP headers and their uses. Each objective has a colorful numbered tag (01–03) along the right edge of the sidebar.
TLS fundamentals (brief)
  • TLS provides:
    • Confidentiality: traffic is encrypted.
    • Integrity: tampering is detected.
    • Authentication: server identity via certificates (optionally client certs).
  • Browsers verify the certificate chain, validity period, and hostname match.
HTTP security headers — what matters most We won’t cover every available header, but these are the most effective and commonly used headers to improve security posture. Add them at the server or location level in Nginx as appropriate. Nginx example: adding headers Place these inside your server block (or specific location) to apply them. Use always so headers are sent even on error responses.
Important CSP note
  • Content-Security-Policy is powerful but can break site functionality if it is too restrictive. Start with a permissive policy and tighten gradually while testing.
Nginx basic authentication (protecting paths) Use htpasswd from Apache’s httpd-tools or apache2-utils to create password files, then protect Nginx locations with auth_basic. Create an htpasswd file:
Nginx config snippet:
Notes on authentication
  • This is simple HTTP Basic Auth suitable for small, controlled areas or staging environments.
  • For production, consider federated solutions (OAuth, OpenID Connect) or single sign-on (SSO) options for user management and stronger security.
Allow / deny directives (IP-based access control) Use allow and deny in Nginx to restrict access by IP or network:
  • allow and deny are evaluated in order; when a client matches allow, access is granted. If no allow matches, the deny all will block the request.
Automated blocking with Fail2Ban Fail2Ban can monitor logs (e.g., Nginx access/error logs) and automatically add firewall rules to block IPs that show malicious behavior (repeated login failures, scan attempts, etc.). Configuring and running Fail2Ban requires elevated privileges and persistent access to system logs.
Fail2Ban requires access to system logs and the ability to modify firewall rules. In restricted environments we will show configuration examples, but a full live demo may not be possible.
When to use Fail2Ban
  • Protects SSH, login endpoints, and services exposed to the public internet.
  • Works well as a complementary defense; it is not a substitute for secure application logic or proper authentication.
Recommended resources and next steps
  • Generate and test local certs with mkcert and configure Nginx to serve HTTPS.
  • Add security headers incrementally and verify site behavior in different browsers.
  • Protect sensitive locations with auth_basic for quick access control.
  • Use allow/deny for IP-based restrictions in trusted network segments.
  • Consider deploying Fail2Ban on production servers with full log and firewall access.
Links and references

Watch Video