Created
September 17, 2016 09:19
-
-
Save jgontrum/6870ee2c6ab39b227a918fd0db6fa182 to your computer and use it in GitHub Desktop.
Redirect a subdomain/domain to a service running on a local port.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "$#" -ne 2 ] | |
then | |
echo "Add a subdomain redirection to nginx." | |
echo "Usage: <subdomain> <port>" | |
exit 1 | |
fi | |
echo "Redirecting the subdomain '$1' to local port '$2'." | |
read -r -p "Are you sure? [y/N] " response | |
response=${response,,} | |
if [[ $response =~ ^(yes|y)$ ]] | |
then | |
touch /etc/nginx/sites-available/$1 | |
echo "# Redirect $1 to the local port $2" > /etc/nginx/sites-available/$1 | |
echo "" >> /etc/nginx/sites-available/$1 | |
echo "server {" >> /etc/nginx/sites-available/$1 | |
echo " listen 80;" >> /etc/nginx/sites-available/$1 | |
echo " server_name $1;" >> /etc/nginx/sites-available/$1 | |
echo " " >> /etc/nginx/sites-available/$1 | |
echo " location / {" >> /etc/nginx/sites-available/$1 | |
echo " proxy_pass http://127.0.0.1:$2/;" >> /etc/nginx/sites-available/$1 | |
echo " proxy_set_header X-Real-IP \$remote_addr;" >> /etc/nginx/sites-available/$1 | |
echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;" >> /etc/nginx/sites-available/$1 | |
echo " proxy_set_header Host \$host;" >> /etc/nginx/sites-available/$1 | |
echo " proxy_redirect off;" >> /etc/nginx/sites-available/$1 | |
echo " }" >> /etc/nginx/sites-available/$1 | |
echo "}" >> /etc/nginx/sites-available/$1 | |
cd /etc/nginx/sites-enabled | |
ln -s /etc/nginx/sites-available/$1 $1 2>/dev/null; true | |
service nginx reload | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment