Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I got 2 domains dev.domain.com and beta.domain.com. At the moment I have one server section (default) with a bunch of specific locations and rules. Now I need to password protect beta.domain.com. Is there a way to do this without creating additional server section and essentially duplicating all my location and other rules?

Generally I would like to know how other people manage complex nginx configurations. Do they just copy sections (duplicate) or include common rules somehow?

share|improve this question
You can place the common rules in a file and use the include directive to include that file - that way any changes only need to made in one location. Alternatively, you can use regex captures to set a variable to the subdomain, and apply some logic to include the the authentication when the variable matches the beta subdomain. –  cyberx86 Nov 22 '11 at 22:18
That's what I was trying to do actually, define subdomain variable and then include basic authentication inside if statement. But it seems like auth statements are not allowed inside if, same with try_files.. –  gansbrest Nov 22 '11 at 22:25

1 Answer 1

The simplest way to share a configuration between multiple server blocks, is using the include directive. For instance:


#...locations and rules...


server {
    server_name dev.domain.com;
    root /path/to/root;
    include /etc/nginx/conf.d/mysite.inc;


server {
    server_name beta.domain.com;
    root /path/to/root;
    include /etc/nginx/conf.d/mysite.inc;
    location / {
        auth_basic "Authentication Required";
        auth_basic_user_file  /path/to/authfile;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.