Conditional root folder based on server name with nginx

Errol Fitzgerald asked:

I need to set the root based on what server the request is coming from. This is what i would like to accomplish in nginx. I took out stuff I thought was irrelevant.

server {
    listen 81;
    charset utf-8;
    server_name d1.com d2.com d3.com;
    expires -1;

    location / {
        location ~* .(jpg|jpeg|rtf|json|png|gif|css|js|swf|flv|ico)$ {
            expires max;
            access_log off;
            log_not_found off;

            if (server_name = d1.com) {
                root /folder1
            }
            if (server_name = d2.com) {
                root /folder2
            }
            if (server_name = d3.com) {
                root /folder3
            }
        }
        try_files $uri $uri/ @proxy;
}

How can I go about setting this up?

My answer:


Why not just do something like..

root /srv/www/$http_host;

and change the directory structure to match:

/srv/www/d1.com
/srv/www/d2.com

etc.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.