cclloyd
cclloyd

Reputation: 9225

Nginx Global server block?

How can I define a server block that applies to all virtual hosts?

Aka have a universal block to configure SSL for all subdomains (they use the same cert)

server {
    listen      80;
    server_name *.example.com;
    return 301 https://$host$request_uri;
}

# Have a block like this that does SSL for all subdomains
server {
    listen          443 ssl ;
    listen          [::]:443 ssl;

    server_name         *.example.com;

    ssl_certificate     /certs/live/example.com/cert.pem;
    ssl_certificate_key /certs/live/example.com/privkey.pem;

    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;
}

Upvotes: 3

Views: 1082

Answers (1)

Richard Smith
Richard Smith

Reputation: 49772

If you have multiple server blocks that use common configuration, many nginx directives can be placed in the outer block (http { ... }) and will be inherited by any server block that does not specifically override the values. Notice the Context: when checking an nginx directive. See the list of directives.

Alternatively, use the include directive to pull common configuration statements from an external file into any part of the configuration file. See this document for details.

Upvotes: 1

Related Questions