


Running and controlling Nginx - command line parameters and signals
Reference materials:
Nginx Chinese documentation:
Nginx start, stop, smooth restart, signal control and smooth upgrade:
Command line parameters:
Common commands:
-c filename: Set the configuration file.
-t: Do not run, but only test the configuration file. nginx will check the syntax of the configuration file for correctness and try to open the files referenced in the configuration file.
-s: Pass a signal, stop closes quickly, quit closes calmly, reopen reopens the log file, switches log files, and reloads the configuration file.
-v: Display the nginx version.
-V: Display nginx version, compiler version and configuration parameters
------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---
[root@localhost /]# alias nginx='/usr/local/nginx/sbin/nginx'<br>[root@localhost /]# alias

<br>
[root@localhost /]# nginx -h nginx version: nginx/1.11.13Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -T : test configuration, dump it and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /usr/local/nginx/) -c filename : set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file[root@localhost /]#
---------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------------------
control signal :
You can use the signal system to control the main process. By default, nginx writes the pid of its main process to the /usr/local/nginx/logs/nginx.pid file. Change the location of this file by passing arguments to ./configure or using the pid command.
The main process can handle the following signals:
TERM,INT | Quick shutdown |
QUIT | Close gracefully |
HUP |
Reconfigure Start a new worker process with a new configuration Close the old worker process calmly |
USR1 | Reopen the log file, which is more useful when cutting logs |
USR2 | Smoothly upgrade the executable program |
WINCH | Close the worker process gracefully |
启动:
启动代码格式:nginx安装目录地址 -c nginx配置文件地址
[root@localhost ~]# alias nginx='/usr/local/nginx/sbin/nginx' [root@localhost ~]# nginx -c /usr/local/nginx/conf/nginx.conf [root@localhost ~]# ps -ef|grep nginx root 2073 1 0 10:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 2074 2073 0 10:37 ? 00:00:00 nginx: worker process root 2076 2022 0 10:37 pts/0 00:00:00 grep nginx [root@localhost ~]#
停止:
从容停止:
# ` 字符是数字键盘 1 字符左边的那个字符
[root@localhost ~]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
快速停止:
[root@localhost ~]# kill -TERM `cat /usr/local/nginx/logs/nginx.pid`
或
[root@localhost ~]# kill -INT `cat /usr/local/nginx/logs/nginx.pid`
强行停止:
[root@localhost ~]# kill -9 nginx
重启:
1.普通重启:关闭进程,修改配置后,重启进程
2.重新加载配置文件,不重启进程,不会停止处理请求
3.平滑更新nginx二进制,不会停止处理请求
=========================================================================
注意:在重载前,测试一下配置文件:
# -t 参数将检查配置文件的语法是否正确,默认会检查 /usr/local/nginx/conf/nginx.conf 文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 如果要对指定配置文件进行语法检查,可以继续添加 -c 参数
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost ~]#
=========================================================================
nginx的平滑重启:
如果改变了nginx的配置文件,想重启nginx,同样可以发送系统型号给nginx主进程的方式来进行,重启之前,要确认配置文件是否正确
[root@localhost ~]# kill -HUP 【nginx主进程号】
当 nginx 接收到 HUP 信号时,它会尝试先解析配置文件,如果成功,就应用新的配置文件(例如,重新打开日志文件或监听的套接字)。之后,nginx 运行新的工作进程并从容关闭旧的工作进程。通知工作进程关闭监听套接字,但是继续为当前连接的客户提供服务。所有的客户端的服务完成后,旧的工作进程被关闭。如果新的配置文件应用失败,nginx 将继续使用旧的配置文件进行工作。
nginx的平滑升级:
当需要将正在运行中的nginx升级、添加/删除服务器模块时,可以在不中断服务的情况下,使用新的nginx可执行程序替换旧的:
1.使用新的可执行程序替换旧的可执行程序,对于编译安装的nginx,可以将新版本编译安装到nginx安装路径中,替换之前,备份一下旧的可执行文件
2.发送以下指令:
[root@localhost ~]# kill -USR2 【旧版本的nginx主进程号】
3.旧版本的nginx的主进程将重命名它的pid文件为.oldbin(例如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新版本的nginx可执行程序,依次启动新的主进程和新的工作进程。
4.此时,新、旧版本的nginx实例会同时运行,共同处理输入的请求,要逐步停止旧版本的nginx,必须发送WINCH信号给旧的主进程,然后,它的工作进程就开始从容关闭:
[root@localhost ~]# kill -WINCH 【旧版本的nginx主进程号】
5.一段时间后,旧的工作进程(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求了。
6.这时候,可以决定是使用新版本,还是恢复到旧版了:
kill -HUP 【旧的主进程号】:nginx将在不重载配置文件的情况下启动它的工作进程
kill -QUIT 【新的主进程号】:从容关闭其工作进程(worker process)
kill -TERM 【新的主进程号】:强制退出
kill 【新的主进程号或旧的主进程号】:如果因为某些原因新的工作进程不能退出,则向其发送kill信号
新的主进程退出后,旧的主进程会移除.oldbin前缀,恢复为它的.pid文件,这样,一切就都恢复到升级之前了。
如果尝试升级成功,而你也希望保留新的服务器时,可发送QUIT信号给旧的主进程,使其退出而只留下新的服务器运行。
The above is the detailed content of Running and controlling Nginx - command line parameters and signals. For more information, please follow other related articles on the PHP Chinese website!

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
