Derek Neely

...notes for thyself, but useful for all...

Nginx and Apache Developer Sandboxes

Nov 13, 2017 by derek

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 mod rewrite rules 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.


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

	server_name ~^(?<user>\w+)\.(?<site>\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;


     ServerAlias *    
     ServerAlias *.*    

     RewriteEngine	on
     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
Linux, Nginx, Apache


Tweet Tweet Tweet

© 2016 Derek Neely