- 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/denyand an introduction to Fail2Ban
- 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.
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.- Install
mkcertfollowing the project README. - Create a local CA and generate a certificate for
localhost:
- The generated
localhost+2.pemandlocalhost+2-key.pem(filenames may vary) can be referenced in your Nginxssl_certificateandssl_certificate_keydirectives for local testing.

- 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.
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.
- 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.
htpasswd from Apache’s httpd-tools or apache2-utils to create password files, then protect Nginx locations with auth_basic.
Create an htpasswd file:
- 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 and deny in Nginx to restrict access by IP or network:
allowanddenyare evaluated in order; when a client matchesallow, access is granted. If no allow matches, thedeny allwill block the request.
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.
- 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.
- Generate and test local certs with
mkcertand configure Nginx to serve HTTPS. - Add security headers incrementally and verify site behavior in different browsers.
- Protect sensitive locations with
auth_basicfor quick access control. - Use
allow/denyfor IP-based restrictions in trusted network segments. - Consider deploying Fail2Ban on production servers with full log and firewall access.