Can a single Nginx site have its own http block

Let's say I have an Nginx config for a site.

# /etc/nginx/sites-available/my-site
server {
    ...
}

But I want sendfile off; to be set for that site. Can I just add an http block at the beginning of the file?

# /etc/nginx/sites-available/my-site
http {
    sendfile off;
}
server {
    ...
}

Would that even work? Would it have a global effect on all sites?


I think nginx does unofficially support multiple http blocks but generally you would only have one.

The idea is that you have a single http block/context and then one or more server blocks within the http block. Within server blocks you can have one or more location blocks.

Configuration settings are inherited and can be overriden using the above nested approach.

Regarding sendfile, it can be configured within any block:

Syntax: sendfile on | off;
Default:    sendfile off;
Context:    http, server, location, if in location

(See: http://nginx.org/en/docs/http/ngx_http_core_module.html#sendfile)

Therefore you can simply add "sendfile off;" to your server block and this will override the sendfile value set in the http block.


Short answer: no need, in your site config you're already in http{} block.

Eg. default nginx.conf for CentOS 7 looks like:

#...
events {
    #...
}

http {
    #...
    include /etc/nginx/conf.d/*.conf;
    #...
    server {
    #...
    }
}

So, your example could be just:

# /etc/nginx/sites-available/my-site
sendfile off;
# Any http block specific directives (like map) can also go here
}
server {
    ...
}