Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Created May 31, 2023 14:38
Show Gist options
  • Save gilangvperdana/c7371802dc5e8d2273728b239ebe96e5 to your computer and use it in GitHub Desktop.
Save gilangvperdana/c7371802dc5e8d2273728b239ebe96e5 to your computer and use it in GitHub Desktop.
Nginx Block Generator with Bash Script

General

If you need to automate a NGINX block template, you can use this script.

  • add-tcpudcp.sh -> Generate TCP/UDP Nginx block.
  • del-tcpudp.sh -> Delete TCP/UDP Nginx block.
  • add-dnsNginx.sh -> Add HTTPS Nginx block.
  • del-dnsNginx.sh -> Delete HTTPS Nginx block.

This script just Nginx block reverse proxy template, if you need an unique template of Nginx, you must customize that script.

#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: ./script <domain> <IP>"
exit 1
fi
domain="$1"
ip="$2"
template=$(cat <<EOF
## $domain start
server {
listen 80;
server_name $domain;
return 301 https://\$host\$request_uri;
}
server {
listen 443 ssl;
server_name $domain;
ssl_certificate /etc/letsencrypt/live/res.adaptivenetworklab.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/res.adaptivenetworklab.org/privkey.pem;
location / {
proxy_pass $ip;
proxy_set_header host $domain;
}
}
## $domain end
EOF
)
echo "$template" >> ./final.conf
#!/bin/bash
if [ $# -ne 3 ]; then
echo "Usage: ./script <IP> <port> <publicport>"
exit 1
fi
ip="$1"
port="$2"
publicport="$3"
template=$(cat <<EOF
## $publicport start
server {
listen $3;
proxy_pass $1:$2;
}
## $publicport end
EOF
)
echo "$template" >> ./finall3.conf.sshd
#!/bin/bash
# Menerima argumen server_name yang akan dihapus
server_name=$1
# Membaca isi file final.conf
content=$(cat ./final.conf)
# Mencari posisi awal dan akhir bagian yang akan dihapus
start_marker="## ${server_name} start"
end_marker="## ${server_name} end"
start_position=$(echo "$content" | grep -n "$start_marker" | cut -d: -f1)
end_position=$(echo "$content" | grep -n "$end_marker" | cut -d: -f1)
# Memeriksa apakah bagian yang akan dihapus ditemukan
if [[ -z "$start_position" ]] || [[ -z "$end_position" ]]; then
echo "Bagian dengan server_name $server_name tidak ditemukan."
exit 1
fi
# Menghapus bagian yang sesuai
new_content=$(echo "$content" | sed "${start_position},${end_position}d")
# Menulis kembali file final.conf dengan isi yang telah diperbarui
echo "$new_content" >final.conf
echo "Bagian dengan server_name $server_name telah dihapus dari final.conf."
#!/bin/bash
# Menerima argumen server_name yang akan dihapus
publicport=$1
# Membaca isi file finall3.conf.sshd
content=$(cat ./finall3.conf.sshd)
# Mencari posisi awal dan akhir bagian yang akan dihapus
start_marker="## ${publicport} start"
end_marker="## ${publicport} end"
start_position=$(echo "$content" | grep -n "$start_marker" | cut -d: -f1)
end_position=$(echo "$content" | grep -n "$end_marker" | cut -d: -f1)
# Memeriksa apakah bagian yang akan dihapus ditemukan
if [[ -z "$start_position" ]] || [[ -z "$end_position" ]]; then
echo "Bagian dengan Port $publicport tidak ditemukan."
exit 1
fi
# Menghapus bagian yang sesuai
new_content=$(echo "$content" | sed "${start_position},${end_position}d")
# Menulis kembali file finall3.conf.sshd dengan isi yang telah diperbarui
echo "$new_content" >finall3.conf.sshd
echo "Bagian dengan Port $publicport telah dihapus dari finall3.conf.sshd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment