Nginx. Server directive inheritance.

Is there any way to reduce construction like:

server { 
  server_name regular_site; 
  location /api/ { 
     proxy_pass 127.0.0.1:5000;
  } 
  location / { 
     proxy_pass 127.0.0.1:3000;
  } 
} 

server { 
  server_name mobile_site; 
  location /api/ { 
     proxy_pass 127.0.0.1:5000;
  } 
  location / { 
     proxy_pass 127.0.0.1:3001;
  } 
} 

to

server api { 
  location /api/ { 
     proxy_pass 127.0.0.1:5000;
  } 
}

server extends api { 
  server_name regular_site;
  location / { 
     proxy_pass 127.0.0.1:3000;
  } 
} 

server extends api { 
  server_name mobile_site;
  location / { 
     proxy_pass 127.0.0.1:3001;
  } 
} 

Any other advices for getting rid of api section are welcome.


You could do it quite easily with a include statement.

In /etc/nginx/conf/api_defaults.conf:

location /api/ { 
  proxy_pass 127.0.0.1:5000;
}    

Then in your main vhost config.

/etc/nginx/sites-enabled/my_new_api.conf

server my_new_api {  
  server_name mobile_site;

  include "/etc/nginx/conf/api_defaults.conf";

  location / { 
    proxy_pass 127.0.0.1:3001;
  }         
}