Reverse proxy to another IP
Posted: Sat Jul 25, 2020 8:56 pm
You can install nginx template that will do reverse proxy to another IP.
If you want to apply it to some domain:
Last 'yes' is to restart nginx after each domain, so it can be 'no', and in that case restart nginx manuly.
In case you have custom enpoint port, do:
In this scenario, both http and https will be proxied to 12345 port (which is probably bad idea).
Therefore, in case you have different enpoint port for HTTPS traffic, then we will create one additional file where we will separately define HTTPS port:
In previous example, http interface will be proxied to 12345 port, and https interface will be proxied to 23456 port.
Sometimes you have only http endpoint (NodeJS app, for example), and you want both http and https:// interface to be proxied to http:// endpoint.
In that case do:
In case you want http visitor to redirect to https, then just copy force-https.tpl to forward.tpl:
in that case only https interface will be proxied to "https://$newip:23456", while http interface will just redirect your visitors to https://domain address.
To apply redirection to all domains on ONE hosting account:
To apply redirection to all domains on the server:
Code: Select all
wget -nv -O /usr/local/vesta/data/templates/web/nginx/forward.sh http://dl.myvestacp.com/vesta/vesta-cp-nginx-tpl/fwd-http2/forward.sh
wget -nv -O /usr/local/vesta/data/templates/web/nginx/forward.tpl http://dl.myvestacp.com/vesta/vesta-cp-nginx-tpl/fwd-http2/forward.tpl
wget -nv -O /usr/local/vesta/data/templates/web/nginx/forward.stpl http://dl.myvestacp.com/vesta/vesta-cp-nginx-tpl/fwd-http2/forward.stpl
chmod a+x /usr/local/vesta/data/templates/web/nginx/forward.sh
Code: Select all
user='your-vesta-user' # enter
domain='your-domain.com' # enter
newip='xxx.xxx.xxx.xxx' # enter
echo "$newip" > /home/$user/conf/web/forward-$domain.txt
/usr/local/vesta/bin/v-change-web-domain-proxy-tpl "$user" "$domain" 'forward' 'txt' 'yes'
In case you have custom enpoint port, do:
Code: Select all
user='your-vesta-user' # enter
domain='your-domain.com' # enter
newip='xxx.xxx.xxx.xxx' # enter
port='12345' # enter
echo "$newip:$port" > /home/$user/conf/web/forward-$domain.txt
/usr/local/vesta/bin/v-change-web-domain-proxy-tpl "$user" "$domain" 'forward' 'txt' 'yes'
Therefore, in case you have different enpoint port for HTTPS traffic, then we will create one additional file where we will separately define HTTPS port:
Code: Select all
user='your-vesta-user' # enter
domain='your-domain.com' # enter
newip='xxx.xxx.xxx.xxx' # enter
port='12345' # enter
porthttps='23456' # enter
echo "$newip:$port" > /home/$user/conf/web/forward-$domain.txt
echo "$newip:$porthttps" > /home/$user/conf/web/forward-$domain-https.txt
/usr/local/vesta/bin/v-change-web-domain-proxy-tpl "$user" "$domain" 'forward' 'txt' 'yes'
Sometimes you have only http endpoint (NodeJS app, for example), and you want both http and https:// interface to be proxied to http:// endpoint.
In that case do:
Code: Select all
user='your-vesta-user' # enter
domain='your-domain.com' # enter
newip='xxx.xxx.xxx.xxx' # enter
port='12345' # enter
echo "$newip:$port" > /home/$user/conf/web/forward-$domain.txt
echo "1" > /home/$user/conf/web/forward-$domain-only-http.txt
/usr/local/vesta/bin/v-change-web-domain-proxy-tpl "$user" "$domain" 'forward' 'txt' 'yes'
In case you want http visitor to redirect to https, then just copy force-https.tpl to forward.tpl:
Code: Select all
cp /usr/local/vesta/data/templates/web/nginx/force-https.tpl /usr/local/vesta/data/templates/web/nginx/forward.tpl
To apply redirection to all domains on ONE hosting account:
Code: Select all
user='your-vesta-user' # enter
newip='xxx.xxx.xxx.xxx' # enter
echo "$newip" > /home/$user/conf/web/forward-all.txt
for domain in $(/usr/local/vesta/bin/v-list-web-domains $user plain |cut -f 1); do
/usr/local/vesta/bin/v-change-web-domain-proxy-tpl "$user" "$domain" 'forward' 'txt' 'no'
echo "=== Redirected: $domain"
done
service nginx reload
To apply redirection to all domains on the server:
Code: Select all
newip='xxx.xxx.xxx.xxx' # enter
echo "$newip" > /home/forward-all.txt
for user in $(grep '@' /etc/passwd |cut -f1 -d:); do
if [ ! -f "/usr/local/vesta/data/users/$user/user.conf" ]; then
continue;
fi
for domain in $(/usr/local/vesta/bin/v-list-web-domains $user plain |cut -f 1); do
/usr/local/vesta/bin/v-change-web-domain-proxy-tpl "$user" "$domain" 'forward' 'txt' 'no'
echo "=== Redirected: $domain"
done
done
service nginx reload