Skip to content

Instantly share code, notes, and snippets.

@jmeyo
Created October 6, 2014 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmeyo/aad75b7dcdb534575cce to your computer and use it in GitHub Desktop.
Save jmeyo/aad75b7dcdb534575cce to your computer and use it in GitHub Desktop.
#!/bin/bash
# add vhost for a new nginx project
# use like this : do_nginx_proxy_vhost subdir.example.com http://192.168.0.20:8080
function do_nginx_new_vhost() {
[ -z $1 -o -z $2 -o "$3" ] && echo "Give name, path and hosts" && return
name=$1
path=$2
hosts=$3
[ -f /etc/nginx/sites-available/project_$name ] && (echo "Updating proxy for host: $host" && sudo rm /etc/nginx/sites-enabled/project_$name) || echo "Creating vhost for project: $name"
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