Add a header globally for all servers

Jeffrey Walton noloader at gmail.com
Tue Nov 11 20:37:55 UTC 2025


Hi Everyone,

I'd like to add the nosniff content option to all pages served by
Nginx. When I attempt to set the option:

    $ cat /etc/nginx/conf.d/nosniff.conf
    # Prevent MIME type sniffing attacks
    http {
        add_header X-Content-Type-Options "nosniff" always;
    }

It results in:

    $ sudo nginx -t
    nginx: [emerg] "http" directive is not allowed here in
/etc/nginx/conf.d/nosniff.conf:2
    nginx: configuration file /etc/nginx/nginx.conf test failed

I don't want to modify /etc/nginx/nginx.conf since it is owned by the
package, and not me.  On upgrade, my changes could/would be lost when
I take the maintainers version of the conf file.

What should I do to add the nosniff content option to all pages served by Nginx?

Thanks in advance.


More information about the nginx mailing list