Operations 2 min read

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.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
How to Automate Nginx Service Down/Up and Reload with Bash Scripts

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.

operationsdeploymentbashscript
Practical DevOps Architecture
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.