---
title: "nginx操作总结"
author: "nicegege"
date: "2015-04-22T17:16:51Z"
tags: []
categories: ["企业架构"]
slug: "nginx"
draft: false
lastmod: "2015-07-27T09:09:31Z"
id: 2205128
url: "https://nicegege.iteye.com/blog/2205128"
---

最近在运维一个大型的网络在线教育系统，用ngnix做反向代理，做了静动分离。nginx功能强大，主要优点在于内存消耗低，处理高并发能力强。

1.**启动**nginx,找到ngnix的跟目录，执行命令:nginx

2.**关闭**运行的nginx服务器，执行命令:

nginx -s stop 

查询nginx主进程号

　　ps -ef | grep nginx

　　从容停止   kill -QUIT 主进程号

　　快速停止   kill -TERM 主进程号

　　强制停止   kill -9 nginx

　　若nginx.conf配置了pid文件路径，如果没有，则在logs目录下

　　kill -信号类型 '/usr/local/nginx/logs/nginx.pid'

3.**检查**nginx的配置文件是否正确：

nginx -t -c /usr/local/nginx/conf/nginx.conf

或

cd /usr/local/nginx/sbin

./nginx -t

**4.升级**nginx

1、先用新程序替换旧程序文件

　　2、kill -USR2 旧版程序的主进程号或者进程文件名

　　　　此时旧的nginx主进程会把自己的进程文件改名为.oldbin，然后执行新版nginx，此时新旧版本同时运行

　　3、kill -WINCH 旧版本主进程号

　　4、不重载配置启动新/旧工作进程

　　　　kill -HUP 旧/新版本主进程号

　　　　从容关闭旧/新进程

　　　　kill -QUIT 旧/新进程号

　　　　快速关闭旧/新进程

　　　　kill -TERM 旧/新进程号

 

**nginx命令参数说明**

\# /usr/local/nginx/sbin/nginx -h  
nginx version: nginx/0.7.63  
Usage: nginx \[-?hvVt] \[-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  
\-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

**5.重新**启动nginx，命令：nginx -s reload

**6.查看**nginx运行，命令：

```
ps命令最常用的还是用于监控后台进程的工作情况，因为后台进程是不和屏幕键盘这些标准输入/输出设备进行通信的，所以如果需要检测其情况，便可以使用ps命令了。
-ef是两个参数的合并写法
-e 显示所有进程。
-f 全格式。
```

    ps -ef|grep nginx  

```
root      1710     1  0 Feb27 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
apache    1711  1710  0 Feb27 ?        00:04:15 nginx: worker process           
apache    1712  1710  0 Feb27 ?        00:04:09 nginx: worker process           
apache    1713  1710  0 Feb27 ?        00:04:37 nginx: worker process           
apache    1714  1710  0 Feb27 ?        00:04:33 nginx: worker process           
apache    1715  1710  0 Feb27 ?        00:04:10 nginx: worker process           
apache    1717  1710  0 Feb27 ?        00:04:20 nginx: worker process           
apache    1718  1710  0 Feb27 ?        00:04:28 nginx: worker process           
apache    1719  1710  0 Feb27 ?        00:04:32 nginx: worker process           
apache    1720  1710  0 Feb27 ?        00:04:19 nginx: worker process           
apache    1721  1710  0 Feb27 ?        00:04:32 nginx: worker process           
101      13590 12973  0 15:10 pts/0    00:00:00 grep nginx
```

 显示了nginx的并发进程。