Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Beaudev simple script to add a nginx vhost reverse proxying to a docker instance
#!/bin/bash
# add vhost reverse proxy for new docker instance for nginx and restart nginx
# use like this : do_nginx_proxy_vhost subdir.example.com http://192.168.0.20:8080
function do_nginx_proxy_vhost() {
[ -z $1 -o -z $2 ] && echo "Give host and address" && return
host=$1
address=$2
[ -f /etc/nginx/sites-available/proxy_reverse_$host ] && (echo "Updating proxy for host: $host" && sudo rm /etc/nginx/sites-enabled/proxy_reverse_$host) || echo "Creating proxy for host: $host"
sudo bash <<EOF
echo 'server {
server_name $host;
location / {
proxy_redirect off;
proxy_set_header Host \$host ;
proxy_set_header X-Real-IP \$remote_addr ;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for ;
proxy_pass $address;
}
}' &> /etc/nginx/sites-available/proxy_reverse_$host
EOF
sudo ln -s /etc/nginx/sites-available/proxy_reverse_$host /etc/nginx/sites-enabled/proxy_reverse_$host
sudo service nginx restart
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.