nginx停止,平滑重启命令

TERM, INT 快速关闭;

QUIT 从容关闭;

HUP 平滑重启,重新加载配置文件;

USR1 重新打开日志文件,在切割日志时用途较大;

USR2 平滑升级可执行程序;

WINCH 从容关闭工作进程;


【荔枝】

[root@localhost nginx]# nginx -c conf/nginx.conf
[root@localhost nginx]# 
[root@localhost nginx]# ps -ef | grep nginx
root      2138     1  0 06:30 ?        00:00:00 nginx: master process nginx -c conf/nginx.conf
nobody    2139  2138  0 06:30 ?        00:00:00 nginx: worker process   
nobody    2140  2138  0 06:30 ?        00:00:00 nginx: worker process   
nobody    2141  2138  0 06:30 ?        00:00:00 nginx: worker process   
root      2143  2083  1 06:30 pts/0    00:00:00 grep nginx
[root@localhost nginx]# 
[root@localhost nginx]# ps -ef | grep nginx
root      2138     1  0 06:30 ?        00:00:00 nginx: master process nginx -c conf/nginx.conf
nobody    2139  2138  0 06:30 ?        00:00:00 nginx: worker process   
nobody    2140  2138  0 06:30 ?        00:00:00 nginx: worker process   
nobody    2141  2138  0 06:30 ?        00:00:00 nginx: worker process   
root      2147  2083  3 06:32 pts/0    00:00:00 grep nginx
[root@localhost nginx]# 
[root@localhost nginx]# kill -QUIT 2138
[root@localhost nginx]# 
[root@localhost nginx]# ps -ef | grep nginx
root      2149  2083  2 06:32 pts/0    00:00:00 grep nginx


【nginx.conf 文件如下】: 第2行的 woker_processes 3; 表示nginx主进程 带有3个工作进程;

# this config is from 3-6
worker_processes 3; 
events {
	worker_connections 1024;  
}
http
{
    # first virtual host 第一个虚拟主机
    server
    {
        # ip and port monitored 监听的ip和端口
        listen          192.168.186.100:80;
        # host name 主机名称
        server_name    192.168.186.100;
        # the dir access_log saved 访问日志文件存放路径
        access_log      logs/server1.access.log combined;
        location /
        {
	    # default index file, priority reduction from left to right
		# 默认首页文件,顺序从左到右,
            index index.html index.htm;
            # the dir html file saved, html网页文件存放目录
            root  /data0/htdocs/server1;
        }
    }
    # second virtual host
    server
    {
        listen          192.168.186.101:80;
        server_name    192.168.186.101;
        access_log      logs/server2.access.log combined;
       location /
        {
            index index.html index.htm;
            root  /data0/htdocs/server2;
        }
    }
    # third virtual host
    server 
    {
        listen          192.168.186.102:80;
        server_name    192.168.186.102;
        access_log      logs/server3.access.log combined;
        location /
        {
            index index.html index.htm;
            root  /data0/htdocs/server3.com;
        }
    }
}


<think>我们正在寻找在Linux系统中重启Nginx服务的命令。 根据引用[1],我们看到用户尝试了多种方法,包括使用`ps`命令查找nginx进程,但似乎没有找到nginx进程(只有grep命令本身),这表明nginx可能没有运行。 引用[3]中提到,用户使用了`pkill -9 nginx`来强制杀掉nginx进程,然后使用`nginx`命令重新启动。但用户也提到,重新配置后需要重启,并询问是否能够不掉线重启(即平滑重启)。 因此,我们不仅要提供重启命令,还要提供平滑重启(重载配置)的命令。 在Linux中,管理Nginx服务的常见方法有: 1. 如果Nginx是通过系统的init系统(如SysV或systemd)安装的,则可以使用系统服务管理命令。 2. 直接使用Nginx的可执行文件。 常见的重启命令包括: - 重启整个Nginx服务(会中断当前连接): ```bash systemctl restart nginx # 如果使用systemd service nginx restart # 如果使用SysV init ``` - 或者直接使用nginx二进制文件(先停止再启动): ```bash nginx -s stop # 停止 nginx # 启动 ``` 但是,如果只是修改了配置文件并希望重新加载而不中断服务,可以使用重载命令: ```bash systemctl reload nginx # systemd方式 service nginx reload # SysV init方式 nginx -s reload # 使用nginx命令发送重载信号 ``` 引用[3]中用户使用了`pkill -9 nginx`,这是一种强制终止进程的方法,可能会导致当前正在处理的请求中断,不推荐作为常规方法。推荐使用上述正常重启或重载命令。 另外,在重启之前,通常需要检查配置文件是否正确,以避免重启失败: ```bash nginx -t ``` 因此,完整的步骤应该是: 1. 检查配置文件:`nginx -t` 2. 如果配置文件正确,则执行重载(如果只是修改配置)或重启(如果需要完全重启服务)。 根据用户的问题,我们提供以下答案:</think>### Linux 中 Nginx 重启命令 在 Linux 系统中重启 Nginx 服务主要有以下几种方式,根据您的系统配置选择合适的方法: #### 1. **使用 systemctl(推荐,适用于 systemd 系统)** ```bash # 完全重启服务(会中断连接) sudo systemctl restart nginx # 平滑重载(仅重新加载配置,不中断连接) sudo systemctl reload nginx # 检查状态 sudo systemctl status nginx ``` > ✅ 适用于 Ubuntu 16.04+/CentOS 7+ 等主流发行版[^3] #### 2. **使用 service 命令(SysVinit 系统)** ```bash # 完全重启 sudo service nginx restart # 平滑重载配置 sudo service nginx reload ``` > 📌 适用于较旧的 Debian/Ubuntu/CentOS 6 等系统 #### 3. **直接调用 Nginx 二进制文件** ```bash # 平滑重载配置(推荐) sudo nginx -s reload # 完全重启(需先停止再启动) sudo nginx -s stop # 停止 sudo nginx # 启动 ``` > ⚠️ 注意:使用 `-s stop` 会立即终止所有连接[^3] --- #### **关键操作步骤** 1. **先检查配置语法**(避免重启失败): ```bash sudo nginx -t ``` 输出 `syntax is ok` 表示配置正确 2. **强制终止进程**(当常规命令失效时): ```bash sudo pkill -9 nginx # 强制杀死所有 nginx 进程 sudo nginx # 重新启动 ``` > 🔥 仅在其他方法无效时使用,会中断所有连接[^3] 3. **验证进程状态**: ```bash ps aux | grep nginx # 查看运行中的进程 curl -I 127.0.0.1 # 测试服务响应 ``` --- ### 常见问题场景 - **修改配置后** → 使用 `sudo nginx -s reload` 平滑重载 - **服务无响应** → 使用 `sudo systemctl restart nginx` 完全重启 - **端口占用/启动失败** → 检查错误日志:`tail -f /var/log/nginx/error.log` > 📚 参考实践:在配置 DNS 服务(如 dnsmasq)后也需类似重载操作[^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值