Sharing One Domain with Multiple DocumentRoot in a Apache Web Server

Feb. 27, 2017 | smlee36 | #apache, #virtualhost, #rewrite, #deveopment

Restriction

All developers can use the same domain and a port 80.

Environment

Developers should be able to access their own workspace through a single domain.


                                    http://test.server.io/

                   cookie:tom        +--------+
     Developer1                      |        |
     Tom           +------------->   | Apache |     +---------->  /home/tom/project1
                                     |        |
                                     |        |
                                     |        |
                   cookie:jane       |        |
                   +------------->   |        |     +---------->  /home/jane/project1
     Developer2                      |        |
     Jane                            |        |
                   cookie:jane2      |        |
                   +------------->   |        |     +---------->  /home/jane/project2
                                     |        |
                                     +--------+

                                       +    ^
                                       |    |
                                       v    +    docroot.map

                                     +------------------------------+
                                     | tom      /home/tom/project1  |
                                     | jane     /home/jane/project1 |
                                     | jane2    /home/jane/project2 |
                                     +------------------------------+



Suggestion

This case has a constraint that delvelopers can not use different domains or ports. Using an alias cookie, multiple developers can access their own workspace through the same domain and a port.

Solution

1. Apache Configuration

!) Replace 'test.server.io' to your domain.

* /etc/httpd/conf/


<VirtualHost *:80>
    ServerName test.server.io
    DocumentRoot /var/www/stage

    RewriteEngine On
    ## SET Cookie
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    ## SET alias
    RewriteRule ^\/SET\/([^\/]+)  - [CO=guru:$1:.server.io:86400:/,R=200,L]
    ## ERASE alias
    RewriteRule ^\/ERASE\/([^\/]+)  - [CO=guru:$1:.server.io:0:/,R=200,L]

    RewriteMap  docroot txt:/etc/httpd/conf/docroot.map

    RewriteCond %{HTTP_COOKIE} guru=([^;]+)
    RewriteRule ^/(.*)   ${docroot:%1}/$1

</VirtualHost>

* /etc/httpd/conf/docroot.map

#
# Syntax:
# alias     docroot
#

tom      /home/tom/project1  
jane     /home/jane/project1 
jane2    /home/jane/project2 

2. Restart Web Server

]# httpd -t
Syntax OK
]# killall -USR1 httpd

3. Test

Set Developer Cookie

OK

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@timeticket.co.kr and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Unset Developer Cookie

Done!