Last active
June 19, 2019 12:02
-
-
Save stigfromsouth/966c26050d6f6a8ed2e5b7d628edb979 to your computer and use it in GitHub Desktop.
Script creates NGINX config files for proxy_pass on internal web hosts.
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 | |
############################################################################### | |
# | |
# Script creates NGINX config files for | |
# proxy_pass on internal web hosts | |
# | |
# begin: 19.06.2019 | |
# author: Alexander Belashev aka Stigfromsouth | |
# version : 0.0.1 | |
# | |
############################################################################## | |
# | |
# How to prepare: | |
# edit $NGINX_CONFDIR, $MAIN_DN, $IP_SUBNET variables | |
# sudo chmod +x proxypass.sh && mv proxypass.sh /usr/bin/proxypass | |
# | |
# Using example: | |
# sudo proxypass -n site1 -a 87 -p 3000 -c | |
# | |
############################################################################## | |
# | |
# TODO: | |
# | |
# | |
############################################################################## | |
# | |
# Config section | |
# | |
NGINX_CONFDIR="/etc/nginx" # NGINX config dir | |
MAIN_DN="example.com" # uplevel domain name | |
IP_SUBNET="192.168.1" # backend's subnet | |
############################################################################## | |
nginx_config_creator() | |
{ | |
PROJ=$1 | |
IP_ADDR=$2 | |
PORT=$3 | |
echo "server {" >> ${PROJ}.${MAIN_DN}.conf | |
echo " listen 0.0.0.0:80;" >> ${PROJ}.${MAIN_DN}.conf | |
echo " server_name ${PROJ}.${MAIN_DN}" >> ${PROJ}.${MAIN_DN}.conf | |
echo " www.${PROJ}.${MAIN_DN};" >> ${PROJ}.${MAIN_DN}.conf | |
echo " client_max_body_size 1024m;" >> ${PROJ}.${MAIN_DN}.conf | |
echo " proxy_send_timeout 12000;" >> ${PROJ}.${MAIN_DN}.conf | |
echo " proxy_read_timeout 12000;" >> ${PROJ}.${MAIN_DN}.conf | |
echo " " >>${PROJ}.${MAIN_DN}.conf | |
echo " access_log /var/log/nginx/${PROJ}.${MAIN_DN}_access.log;" >> ${PROJ}.${MAIN_DN}.conf | |
echo " error_log /var/log/nginx/${PROJ}.${MAIN_DN}_error.log;" >> ${PROJ}.${MAIN_DN}.conf | |
echo " " >>${PROJ}.${MAIN_DN}.conf | |
echo " location / {" >>${PROJ}.${MAIN_DN}.conf | |
echo " proxy_pass http://${IP_SUBNET}.${IP_ADDR}:${PORT};" >> ${PROJ}.${MAIN_DN}.conf | |
echo " include proxy_params;" >> ${PROJ}.${MAIN_DN}.conf | |
echo " }" >> ${PROJ}.${MAIN_DN}.conf | |
echo "}" >> ${PROJ}.${MAIN_DN}.conf | |
} | |
help () { | |
cat <<EOF | |
-h -- display its help & exit | |
-n -- 3rd part domain name (project name) | |
-a -- last part of IP address 192.168.1.x | |
for destination host for proxy_pass | |
-p -- destination port number | |
-c -- commit action | |
Example: | |
sudo proxypass -n site1 -a 87 -p 3000 -c | |
EOF | |
} | |
commit () { | |
cd ${NGINX_CONFDIR}/sites-available && | |
nginx_config_creator ${PROJ} ${IP_ADDR} ${PORT} && | |
ln -s ${NGINX_CONFDIR}/sites-available/${PROJ}.${MAIN_DN}.conf ${NGINX_CONFDIR}/sites-enabled/${PROJ}.${MAIN_DN}.conf && | |
service nginx reload # need to change this line for your init system | |
} | |
# __main__ | |
OPTS=`getopt -o hcp:n:a: -- "$@"` | |
if [ $? != 0 ] | |
then | |
exit 1 | |
fi | |
eval set -- "$OPTS" | |
while true ; do | |
case "$1" in | |
-h ) help; exit 0; shift ;; | |
-n ) PROJ=$2; shift 2 ;; | |
-a ) IP_ADDR=$2; shift 2 ;; | |
-p ) PORT=$2; shift 2 ;; | |
-c ) commit; shift ;; | |
-- ) shift; break ;; | |
* ) break ;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment