1.1 Achieving goals
- When studying or doing nGINX testing, spendnginx -s reload/stopTo change anything, you need to restart or stop the startup. Basically, the state is to switch back and forth between the following five states
- vim nginx.conf
- Modify the nginx. Conf
- Save the nginx. Conf
- Restart the nginx
- Refresh the browser…
- Restart time can actually be used to do more things, such as focus on the content rather than stop and restart, programmers don’t like to repeat things
- We can simplify the restart process by making any changes to nginx.conf automatically restart nginx and enable automatic loading
- Next we will touch the hand to achieve nGINX configuration file modification automatic loading
- [note] : the following operations are based on Linux (centos7) version, not applicable to Windows
1.2 Technical Difficulty
- Technical difficulty: ★★
- Principle difficulty: ★
Ii. Technical realization
2.1 Required Technologies
- Basic Linux command knowledge
- Linux Shell programming knowledge
2.2 Technical Principles
- If the configuration file is modified, the modification time must be changed
- Then we can record the modification time. If the modification time of the current file is different from the last modification time recorded, the configuration file was modified. In this case, we can run the restart command of nginx, and then take the current time as the last modification time
- By repeating the above operations, you can automatically load the latest configuration file when the nginx configuration file is modified
- The above principle is for reference only, there are many ways to achieve, this is relatively simple and crude
2.3 Script Demonstration
- Start the script (see script using a shell script written first) and wait
2. Modify the nginx.conf file and save it, then watch the configuration file reload3. Then refresh the browser to have a look, you can see the refreshed, dynamic effect demo5. Welcome to leave your comments in the comments section
2.4 shell script
- Just change the following nginx installation path
#! /bin/bash
#Nginx installation directory with no/at the endBASE_PATH = "/ opt/Nginx/Nginx - 1.8-1"#Configuration file Location
CONFIG_FILE="$BASE_PATH/conf/nginx.conf"
#The directory where the NGINX startup file is located, with no/at the end
NGINX_PATH="$BASE_PATH/sbin"
#Obtain the initial modification time
last_time=`stat $CONFIG_FILE | grep "Modify"`
stop=0
#Current Modification Time
current_time="";
#Infinite loop, can be changed as needed
while [ $stop -le 1 ]; do
#Gets the current modification time
current_time=`stat $CONFIG_FILE | grep "Modify"`
#Check whether the current time is consistent with the last time modified. If no, change the time
if [ "$last_time" != "$current_time" ]; then
echo "------------------ Config File Change ----------------"
echo "Last_time $last_time"
echo "Curr_time $current_time"
#Record the last modification time as the current time
last_time=$current_time
#Go to the nginx binary directory
cd $NGINX_PATH
#The configuration file to be loaded is the configuration file in the upper conf file. Modify the configuration file based on your own requirements
./nginx -s reload
#Close the configuration file and then restart it. You can customize the configuration file directory and use the configured file path
# ./nginx -s stop
# ./nginx -c $CONFIG_FILE
#More custom actions...
echo "------------------------------------------------------"
fi
done
Copy the code
Iii. Script use
- Create a new shell script, for example
vim reload.sh
Copy the code
- Copy the script in
- Change the nginx path and save the script
- Modify the permissions
chmod 744 reload.sh
Copy the code
- The startup script
./reload.sh
Copy the code
- After startup, the current window will enter the waiting state, and the configuration file will be loaded automatically after modification. You can interrupt it by pressing CTRL + C to exit