Copy & Paste

__FILE_DIR__="$HOME/__TMP_CREATE_NGINX_CONF__.sh"
sudo mkdir -p $(dirname ${__FILE_DIR__}) \\
&& sudo touch ${__FILE_DIR__} && sudo chmod 757 ${__FILE_DIR__} && cat << 'EOF' > ${__FILE_DIR__}
#!/bin/bash
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# 사용자 입력
read -p "Port(From): " PORT_FROM
read -p "Port(To)  : " PORT_TO

CONF_DIR="/etc/nginx/sites-available/rdt_${PORT_FROM}_${PORT_TO}.conf"

sudo touch $CONF_DIR
sudo chmod 777 $CONF_DIR

echo "server {" > ${CONF_DIR}
echo "    listen ${PORT_FROM} ;" >> ${CONF_DIR}
echo "    listen [::]:${PORT_FROM} ;" >> ${CONF_DIR}
echo "" >> ${CONF_DIR}
echo "    location / {" >> ${CONF_DIR}
echo "         proxy_pass <http://127.0.0.1>:${PORT_TO};" >> ${CONF_DIR}
echo "         proxy_set_header X-Real-IP \\$remote_addr;" >> ${CONF_DIR}
echo "         proxy_set_header X-Forwarded-For \\$proxy_add_x_forwarded_for;" >> ${CONF_DIR}
echo "         proxy_set_header Host \\$http_host;" >> ${CONF_DIR}
echo "    }" >> ${CONF_DIR}
echo "}" >> ${CONF_DIR}
echo "" >> ${CONF_DIR}

sudo chmod 755 $CONF_DIR

sudo ln -s ${CONF_DIR} /etc/nginx/sites-enabled/

sudo service nginx reload

sudo rm ${__FILE_DIR__}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EOF
export __FILE_DIR__ && bash -c "${__FILE_DIR__}"