We're making a move over to some Nginx servers from Apache. While we've used Nginx for various sites its been in a mostly basic config. However, one of the 'sites' (multiple actually) we host is a sandbox type of site such that our developers can login to the server create a directory and they have a site up and running. I'm trying to figure out how to replicate this with Nginx but am unable to wrap my head around it thus far. Any help would be appreciated.

Server Directory Setup Example:

... etc.

Apache Config:

<VirtualHost *:80>   
    ServerName example.com
    ServerAlias *.example.com    
    ServerAlias *.*.example.com    

    RewriteEngine   on

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

    <Directory />
        AllowOverride All 
        Require all granted

Resulting Domain:

.... etc.

What would be the equivalent in Nginx to parse the host and correctly route internally to said directory?


Maybe something like this:

server {
    server_name ~^(?<user>\w+)\.(?<site>\w+)\.example\.com$;

    #Then you can use $user and $site variables in config, e.g.:
    root /home/$user/sandbox/$site;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.