Nginx get domainname without TLD or suffix

Post Reply
sparkzara
Posts: 22
Joined: Mon Jun 07, 2021 9:15 am

Hi,
I am trying to add reverse proxy template to show content from a subdomain based on domainname.
For ex: example.com should show content from example.maindomain.com

I tried to use %domain%, but it gets full name including TLD value like example.com.maindomain.com

Is there any nginx variable or code available to get only domain name without domain suffix or TLD?

Tags:
User avatar
myVesta
Site Admin
Posts: 928
Joined: Fri Jun 19, 2020 9:59 am
Has thanked: 8 times
Been thanked: 6 times

Nope.
You can only make it static string in tpl file, if you need it only for one domain?

Other possibility is to make yourtemplate.sh that alter apache/nginx config files, something like this - http://dl.myvestacp.com/vesta/vesta-cp- ... forward.sh

Code: Select all

file=/home/$user/conf/web/$domain.nginx.conf
if [ -f "$file" ]; then
    sed -i "s/FORWARDTO/$FORWARDTO/g" $file
fi
sparkzara
Posts: 22
Joined: Mon Jun 07, 2021 9:15 am

hi, thanks for the reply.
but i wanted this to be applied for all the domain added on myvestacp using a specific template.
Note: i'm doing domain addition through api.

but here is the list of things i did so far.
created 2 files on /usr/local/vesta/data/templates/web/nginx/
1. reverse-force-https.tpl ( clone of force-https.tpl ) - no changes done.
2. reverse-force-https.stpl - clone of force-https.stpl and added proxy_header addition.

here is the code of reverse-force.stpl

Code: Select all

server {
    listen      %ip%:%proxy_ssl_port% ssl http2;
    server_name %domain_idn% %alias_idn%;

    ssl_certificate      %ssl_pem%;
    ssl_certificate_key  %ssl_key%;
    error_log  /var/log/%web_system%/domains/%domain%.error.log error;

    location / {
        proxy_pass      https://%ip%:%web_ssl_port%;
        proxy_set_header Host %domain%.actor.com;
        location ~* ^.+\.(%proxy_extentions%)$ {
            root           %sdocroot%;
            access_log     /var/log/%web_system%/domains/%domain%.log combined;
            access_log     /var/log/%web_system%/domains/%domain%.bytes bytes;
            expires        max;
            # try_files      $uri @fallback;
        }
    }

    location /error/ {
        alias   %home%/%user%/web/%domain%/document_errors/;
    }

    location @fallback {
        proxy_pass      https://%ip%:%web_ssl_port%;
    }

    location ~ /\.ht    {return 404;}
    location ~ /\.env   {return 404;}
    location ~ /\.svn/  {return 404;}
    location ~ /\.git/  {return 404;}
    location ~ /\.hg/   {return 404;}
    location ~ /\.bzr/  {return 404;}

    disable_symlinks if_not_owner from=%docroot%;

    include %home%/%user%/conf/web/*nginx.%domain_idn%.conf_letsencrypt;
    include %home%/%user%/conf/web/s%proxy_system%.%domain%.conf*;
}
on location / , i'am added proxy_header
proxy_set_header Host %domain%.actor.com;

but after adding domain i was expecting a value of domain name without suffix on web config generated,
so if i add testing.com domain, the proxy header should be
proxy_set_header Host testing.actor.com;
but what i get is
proxy_set_header Host testing.com.actor.com;

do we have something like php explode %domain% and get first array value.
so i can simply replace %domain% with that ?

Thanks.
sparkzara
Posts: 22
Joined: Mon Jun 07, 2021 9:15 am

hi. any update on this.
i hope many developers will be looking for similar solution for SaSS product hosted on single domain and served to individual users by subdomains, and when end user requires custom domain to show content from a sub-domain this workaround is required.

based on some solutions i tried below steps.

1.
add map function on /etc/nginx/nginx.conf inside http block
Screenshot 2023-12-31 012342.png
Screenshot 2023-12-31 012342.png (20.84 KiB)

Code: Select all

map $domain $domain_without_tld {
    ~^(?<domain_name>[^.]+)\..+$ $domain_name;
}
when i do this, i get $domain undefined variable error. without $ symbol, there is no error, but again it doesnt seems to be working.

2.
added proxy pass on forcessl.tpl file at /usr/local/vesta/data/templates/web/nginx/force-https.stpl
Screenshot 2023-12-31 013023.png
Screenshot 2023-12-31 013023.png (54.76 KiB)

Code: Select all

location / {
        proxy_pass      https://%ip%:%web_ssl_port%;
        proxy_set_header Host $domain_without_tld.%domain_idn%;
        location ~* ^.+\.(%proxy_extentions%)$ {
            root           %sdocroot%;
            access_log     /var/log/%web_system%/domains/%domain%.log combined;
            access_log     /var/log/%web_system%/domains/%domain%.bytes bytes;
            expires        max;
            # try_files      $uri @fallback;
        }
    }
I need to use map function to get value of domain withoud tld on tpl file.
can you please help me with a solution for this?
Post Reply