Nginx vhost configuration

Posted by user101494 on Server Fault See other posts from Server Fault or by user101494
Published on 2011-11-19T14:00:20Z Indexed on 2011/11/19 17:55 UTC
Read the original article Hit count: 151

Filed under:
|

I am attempting to setup a new server with Nginx 1.0.10 on debian 6. The config below works perfectly on a server with nginx 0.8.36 on Ubuntu 10.04.3 but not on the new box.

The desired result is to:

  • Redirect non-www request on the tld to www, but not not subdomains
  • Use the the folder structure
    • /var/www/[domain]/htdocs
    • /var/www/[domain]/subdomains/[subdomain]/htdocs
    • Serve files any host for which files exist in this structure

On the new server domains are matching correctly but subdomains are matching to /var/www/[subdomain].[domain]/htdocs not /var/www/[domain]/subdomains/[subdomain]/htdocs

server {
  listen 80;
  server_name  _________ ~^[^.]+\.[^.]+$;
  rewrite ^(.*)$ $scheme://www.$host$1 permanent;
}

server {
  listen 80;
  server_name  _ ~^www\.(?<domain>.+)$;

  server_name_in_redirect off;


  location / {
    root  /var/www/$domain/htdocs;
    index index.html index.htm index.php;
    fastcgi_index   index.php;
  }

  location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    keepalive_timeout 0;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

  location ~ /\.ht {
    deny  all;
  }

}

server {
  listen 80;
  server_name  __ ~^(?<subdomain>\.)?(?<domain>.+)$$;

  server_name_in_redirect off;

  location / {
    root  /var/www/$domain/subdomains/$subdomain/htdocs;
    index index.html index.htm index.php;
    fastcgi_index   index.php;
  }

  location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    keepalive_timeout 0;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

  location ~ /\.ht {
    deny  all;
  }

}

© Server Fault or respective owner

Related posts about nginx

Related posts about virtualhost