3

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;
}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the include, imho it is the preferred way to go as not all directives can be placed in the server block, on top of that, you can use variables to substitute common strings in includes, which enables templating for automation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.