nginx redirect subdomain to a folder

5,278

Sorry Mike, your if statement solution is a bad idea... refer to the Nginx pitfalls wiki page for why (in this case nginx will evaluate the directive for every request, which is inefficient)...

Add another server block above the one you have containing this:

server {
   server_name subdomain.domain.com;
   rewrite ^ $scheme://domain.org/subdomain$request_uri redirect;
}

and magic will be done...

Share:
5,278

Related videos on Youtube

gforg
Author by

gforg

Updated on September 18, 2022

Comments

  • gforg
    gforg over 1 year

    In Nginx, I would like to redirect my subdomain.domain.com to domain.com/sub/ .how do I do that ? I would like to use the existing sites config file for domain.com instead of creating a new one.

    the config file I have is (how do I add to this)

    ======

    server {
    listen   80;
    server_name www.domain.com domain.com  *.domain.com;
    access_log /srv/www/domain.com/logs/access.log;
    error_log /srv/www/domain.com/logs/error.log;
    
    location / {
        root   /srv/www/domain.com/public_html;
        index  index.html index.htm;
    }
    

    location /phpmyadmin { root /usr/share; index index.php; }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  /srv/www/domain.com/public_html$fastcgi_script_name;
    }
    

    }

    =========