


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!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

There are two ways to solve the Nginx cross-domain problem: modify the cross-domain response header: add directives to allow cross-domain requests, specify allowed methods and headers, and set cache time. Use CORS modules: Enable modules and configure CORS rules that allow cross-domain requests, methods, headers, and cache times.
