How to Automate Nginx Service Down/Up and Reload with Bash Scripts
This guide shows how to create Bash scripts that mark an Nginx upstream server as down or up by editing the upstream configuration via SSH and then hot‑reload Nginx, providing a repeatable automation workflow for service deployment and rollback.
1. Configure Nginx down script
Create nginx/nginx-biz-service-down.sh with the following content:
#!/bin/bash
nginx_ip=$1
remote_ip=$2
port=$3
ssh -p 22 root@${nginx_ip} "sed -i 's/server ${remote_ip}:${port};/server ${remote_ip}:${port} down;/' /usr/local/nginx/conf/vhosts/upstream.conf"
# Hot‑load Nginx
ssh root@${nginx_ip} "/usr/local/nginx/sbin/nginx -s reload"This script receives the Nginx host IP, the target upstream IP, and the port, then marks the upstream entry as down and reloads Nginx to apply the change.
2. Configure Nginx up script
Create nginx/nginx-biz-service-up.sh with the following content:
#!/bin/bash
nginx_ip=$1
remote_ip=$2
port=$3
ssh -p 22 root@${nginx_ip} "sed -i 's/server ${remote_ip}:${port} down;/server ${remote_ip}:${port};/' /usr/local/nginx/conf/vhosts/upstream.conf"
# Hot‑load Nginx
ssh root@${nginx_ip} "/usr/local/nginx/sbin/nginx -s reload"This script reverses the previous change by removing the down flag from the upstream entry and then reloads Nginx.
Both scripts can be invoked with the Nginx server IP, the upstream IP, and the service port, enabling quick toggling of service availability without manual configuration edits.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
