← Back to all snippets
NGINX

Implement X-Frame-Options and X-Content-Type-Options Security Headers

Protect users from clickjacking and MIME-sniffing attacks by setting X-Frame-Options and X-Content-Type-Options headers in your web server configuration.

# In your Nginx server block or http block
# Protect against Clickjacking attacks
add_header X-Frame-Options "DENY"; # Other options: "SAMEORIGIN", "ALLOW-FROM uri"

# Protect against MIME-sniffing vulnerabilities
add_header X-Content-Type-Options "nosniff";

# Optional: Enable for all responses, or specific locations
# For example, to apply globally:
# server {
#     listen 80;
#     server_name example.com;
#
#     add_header X-Frame-Options "DENY";
#     add_header X-Content-Type-Options "nosniff";
#
#     location / {
#         # ... your application specific config
#     }
# }
How it works: This Nginx configuration snippet adds two important security headers. The `X-Frame-Options "DENY"` header prevents your website from being embedded within an `<frame>`, `<iframe>`, `<embed>`, or `<object>` tag on another domain, effectively protecting users from clickjacking attacks. The `X-Content-Type-Options "nosniff"` header prevents browsers from "sniffing" a response's content type away from the declared `Content-Type` header, which can prevent certain XSS attacks that arise from misinterpretation of file types. These headers are simple yet powerful additions to enhance web application security.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs