Nginx and Apache Developer Sandboxes

We do a lot of development on many different projects concurrently as well as just having a place to play. With that, we didn't want to have to have folks create a vhost for every project. Having to maintain what was what (needed, old, etc.) would quickly become a big pain in the a$$. With that said, we just employed some simple rewrite rules with Nginx and Apache and our devs can now create a project/sandbox just by creating that directory.

 

Each user has a 'sandbox' directory in their home directory on the development server.

 

Nginx:

server {
    listen 80;
    listen [::]:80;

    server_name ~^(?\w+)\.(?\w+)\.sandbox\.domain\.com$;
    root /home/$user/sandbox/$site;

    access_log /var/log/nginx/sandbox.domain.com_access.log;
    error_log /var/log/nginx/sandbox.domain.com_error.log error;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

 

Apache:


     ServerAdmin support@domain.com
     ServerName sandbox.domain.com
     ServerAlias *.sandbox.domain.com    
     ServerAlias *.*.sandbox.domain.com    

     RewriteEngine  on
     RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
     RewriteRule .* - [F] 

     RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.sandbox\.domain\.com$ [NC]
     RewriteRule ^(.*)$ /home/%1/sandbox/%2/$1 [L]

     LogLevel warn 
     LogFormat "%V:%p %a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined_remote
     ErrorLog /var/log/apache2/sandbox.domain.com_error.log
     CustomLog /var/log/apache2/sandbox.domain.com_access.log vhost_combined_remote

        AllowOverride All 
        Require all granted

Share this Post: