Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to create and remove subdomains automatically using nginx. I know I can do it using the following approach:

server {
    listen 80;
    server_name *;
    root /var/www/$subdomain;
    set $subdomain "";
    if ($host ~* ^([a-z0-9-\.]+)\$) {
        set $subdomain $1;
    if ($host ~* ^$) {
        set $subdomain "";

I can create folders in root /var/www/ using a script and nginx will create appropriate subdomains for me.

However I need to set such parameters as number of simultaneous connections and maximum connection bandwidth and values of that parameters will be passed to my script.

So the question is how can I set those additional parameters?

If there's no way to solve my problem using nginx you can advice me how to solve it using apache.


share|improve this question
up vote 1 down vote accepted

I include a subfolder ( /etc/nginx/confs ) and create a new file for each subdomain. I'm running rails apps so I need a fair bit of configuration in each subdomain. I have a background beanstalkd worker that when someone signs up installs the application, creates the nginx conf and reloads nginx.

share|improve this answer

If I'm getting this right what you have now can be achieved with mod_vhost_alias on apache so you have the exact same behaviour as you have on nginx as per this manual page.

Concerning the rest of the parameters i'd recommend you use mod_qos and set them in .htaccess files in the directory where the files to be served live.

I'm available for questions if you comment on this post :)

Hope this helps!

share|improve this answer
Does nginx support such external configuration files where I can put my additional parameters? Will user be able to modify my limitations if I set those parameters in an .htaccess file or any other external configuration file supported by nginx? – Roman Prykhodchenko Aug 18 '10 at 13:14
Hi again. Anyone with write access to .htaccess will be able to change anything you set there (obviously). About the .htaccess equivalent on nginx... to my knowledge there's no such thing but if you do find a solution for that i'd be interested in learning too :) – Khai Aug 18 '10 at 14:38

There is no way to do exactly what you want. Nginx is not written with mass hosting in mind but rather written for high traffic sites.

The proper way to handle this with Nginx is to not have a catch-all server block but rather have a server block for each subdomain. Creating these by hand is obviously tedious so writing a script that will generate them is highly recommended and not too complicated.

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.