Gabriel Caprini
Gabriel Caprini

Reputation: 13

How can I make nginx redirect subdomain to a folder?

How can I make nginx redirect all the requests to my subdomain to a folder?

Example:

http://sub2.sub1.domain.com/

that should indicate that sub2 is a folder in sub1.domain.com/sub2

How can I do this?

The main objective is to hide the folder to the user. So, it should continue as http://sub2.sub1.domain.com/

My wish is to use a wildcard in sub2.

UPDATE:

I've tried:

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

    server_name ~^(.*)\.sis\..*$;

location / {
    proxy_pass http://sis.mydomain.com/$1$request_uri;
    }
}

but it also didn't work, any error?

Upvotes: 1

Views: 964

Answers (1)

l'L'l
l'L'l

Reputation: 47284

In the nginx directives for sub2.sub1.domain.com you'd put:

server {
    listen 80;
    server_name sub2.sub1.domain.com;

location / {
    proxy_pass https://sub1.domain.com/sub2;
    }
}

So any request going to sub2.sub1.domain.com gets proxied to → sub1.domain.com/sub2 (while masked as sub2.sub1.domain.com); no need for a redirect or rewrite this way either.

Wildcard Method

server {
    listen 80;
    server_name ~^(.*)\.sub1\.domain\.com;

location / {
    proxy_pass https://sub1.domain.com/$1;
    }
}

*the wildcard method above is untested.

Upvotes: 1

Related Questions