Wednesday, June 7, 2017

redirect - Is there a way to dynamically forward subdomains in NGINX?



I've done all the googling I can, but no luck. I'm trying to figure out how to create a namespace for a user, say user.mysite.com, and have that redirect to his/her folder (mysite.com/user). Is there a way to make nginx forward all subdomains to their respective folders? The reason I'm doing it like this is because I'd like to be able to make subdomains like this on the fly without having to restart nginx.



My nginx.conf file looks like this right now FYI.



server
{


# add www.
if ($host ~ ^(?!www)) {
rewrite ^/(.*)$ http://www.$host/$1 permanent;
}


server_name www.mydomain.com;

access_log /var/log/nginx/mydomain.com.access.log;


error_log /var/log/nginx/mydomain.com.error.log;

root /usr/share/nginx/mydomain.com/;

index index.php index.html index.htm;

# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}

}


This is a little piece of code I found that will redirect all subdomains to the www.mydomain.com. If it could redirect to their respective directories instead that would be exactly what I need.



# remove subdomain
if ($host ~ "^www\.(.*?)\.(.{3,}\.([a-z]{2}\.[a-z]{2}|[a-z]{2,4}))$") {
set $host_without_sub $2;
rewrite ^/(.*)$ http://www.$host_without_sub/$1 permanent;
}


Answer



With another whole day of googling I found this gem:



http://publications.jbfavre.org/web/nginx-vhosts-automatiques-avec-SSL-et-authentification-version2.en



#######################  Magic  RewriteRules  #######################

uninitialized_variable_warn off;


##### Rewrite rules for domain.tld => www.domain.tld #####
if ($host ~* ^([^.]+\.[^.]+)$) {
set $host_without_www $1;
rewrite ^(.*) $scheme://www.$host_without_www$1 permanent;
}

##### Rewrite rules for subdomains with automatic SSL support #####
set $redirect_ssl 'no';
if ($host ~* ^(.*)\.([^.]+\.[^.]+)$) {
set $ssl_subdomain $1;

set $host_without_www $1.$2;
}
if (-e $document_root/config/ssl/$ssl_subdomain) {
set $redirect_ssl 'yes';
}
if ($scheme = 'https') {
set $redirect_ssl 'no';
}
if ($redirect_ssl = 'yes') {
rewrite ^(.*) https://$ssl_subdomain.$host_without_www$1 permanent;

}

##### Rewrite rules for automatic authentication #####
if ($host ~* ^([^.]+)\.[^.]+\.[^.]+$) {
set $auth_subdomain $1;
}
if (-e $document_root/config/auth/$auth_subdomain) {
rewrite ^(.*)$ /auth$1;
break;
}


##### Rewrite rules for automatic subdirectory rewriting #####
set $redirect_subdir 'yes';
if ($redirect_subdir_done = 'yes') {
set $redirect_subdir 'no';
}
if ($host ~* 'www\.[^.]+\.[^.]+$') {
set $redirect_subdir 'no';
}
if ($host ~* ^([^.]+)\.[^.]+\.[^.]+$) {

set $subdir_domain '$1';
}
if ($redirect_subdir = 'yes') {
set $redirect_subdir_done 'yes';
rewrite ^(.*)$ /$subdir_domain$1 break;
}

#################### End Of Magic RewriteRule ####################

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...