Add a header globally for all servers

Xiufeng Guo showfom at gmail.com
Tue Nov 11 22:31:57 UTC 2025


Hi,

1. Don’t place it under /etc/nginx/conf.d. You can use a separate
folder such as /etc/nginx/snippets.

2. Remove the http {} block and only include the following line:

add_header X-Content-Type-Options "nosniff" always;

3. Then, in each website’s configuration file (inside the server {} block), add:

include /etc/nginx/snippets/nosniff.conf;

On Wed, Nov 12, 2025 at 5:38 AM Jeffrey Walton <noloader at gmail.com> wrote:
>
> 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.



-- 
Best Regards,
Xiufeng Guo


More information about the nginx mailing list