nginx操作命令、端口查询及进程关闭命令。
目录导航
1、nginx操作相关
1.1、启动
- 启动时指定nginx使用的配置文件
/a/b/nginx -c /d/e/nginx.conf
- 启动时不指定,使用默认安装路径下的配置文件
/a/b/nginx
1.2、停止
- 快速关闭Nginx,并立即终止web服务,相关信息可能不保存。
/a/b/nginx -s stop
- 平稳关闭Nginx,有安排的结束web服务,相关信息会保存。
/a/b/nginx -s quit
1.3、重启
- 重新加载nginx的配置文件
/a/b/nginx -s reload
1.4、检查配置文件
- 不启动nginx,只是检查配置文件及所有引用文件的语法正确性。
/a/b/nginx -t
1.5、将日志重新写到默认文件中(日志备份时使用)
当nginx运行时,如果将默认日志文件access.log的名字改成access.log-1,新日志就会写到access.log-1。(据说是因为linux内核是根据文件描述符来定位文件的,待考证)
nginx提供了下面的命令,实现新建默认的nginx日志文件,后续日志写到默认文件中。当nginx默认日志文件存在时,该命令执行没有效果。
/a/b/nginx -s reopen
1.6、查看nginx 的版本
- 只查看nginx 的版本
/a/b/nginx -v
#输出打印如下:
nginx version: nginx/1.21.6
- 查看nginx 的版本、编译器版本和配置参数。
/a/b/nginx -V
#输出打印如下:
nginx version: nginx/1.21.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1k 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/a/b/nginx --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_image_filter_module --with-threads --with-openssl=../openssl-1.1.1k --with-openssl-opt=no-weak-ssl-ciphers --with-openssl-opt=no-ssl3 --with-pcre=../pcre-8.44 --with-pcre-jit --with-zlib=../zlib-1.2.11 --with-compat --with-file-aio --add-module=./ngx_http_substitutions_filter_module-master --with-http_ssl_module
2、其他辅助命令
nginx启动时,当遇到端口冲突时会启动失败,此时需要排查哪个进程占用了端口、然后关闭该进程。
2.1、查看某个端口是否被占用
- Windows环境
命令格式如:netstat -aon|findstr [端口关键字]
示例如下(4856即占用该端口的进程的ID):
C:\Users\test>netstat -ano|findstr :80
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 4956
TCP 172.16.35.137:60826 120.221.62.26:8081 ESTABLISHED 9152
- Linux环境
命令格式如:netstat -tunlp|grep [端口关键字]
示例如下(705即占用该端口的进程的ID):
[root@test82 ~]# netstat -tunlp|grep :1443
tcp 0 0 0.0.0.0:1443 0.0.0.0:* LISTEN 705/nginx: master p
- MacOS环境
MacOS上netstat不同于Linux,losf(list of file的缩写)命令更方便查询端口占用。
命令格式如:lsof -i :[端口] | grep LISTEN
示例如下(81156和81157即占用该端口的进程的ID,父子进程是可以绑定同一个端口的):
lsof -i :80|grep LISTEN
nginx 81156 yuji 6u IPv4 0xe62c37e39d8426f1 0t0 TCP *:http (LISTEN)
nginx 81157 yuji 6u IPv4 0xe62c37e39d8426f1 0t0 TCP *:http (LISTEN)
2.2、查看某个进程的PID
tasklist /fi “IMAGENAME eq nginx.exe”
- Windows环境
根据进程名查看,命令格式:tasklist|findstr [进程名称关键字]
示例如下(5388和4936即两个进程的ID):
C:\Users\test>tasklist|findstr nginx.exe
nginx.exe 5388 Console 1 13,412 K
nginx.exe 4956 Console 1 13,784 K
另外一种根据进程名查看的命令格式:tasklist /fi “IMAGENAME eq [process name]”
输出带有表头信息,示例如下:
C:\Users\test>tasklist /fi "IMAGENAME eq nginx.exe"
映像名称 PID 会话名 会话# 内存使用
========================= ======== ================ =========== ============
nginx.exe 5388 Console 1 13,368 K
nginx.exe 4956 Console 1 13,736 K
- Linux/MacOS环境
根据进程名查看,命令格式:ps -ef|grep [进程名称关键字]
示例如下(81156和81157即两个进程的ID):
ps -ef|grep nginx
501 81156 1 0 12:46下午 ?? 0:00.00 nginx: master process nginx
501 81157 81156 0 12:46下午 ?? 0:00.01 nginx: worker process
501 82289 81086 0 2:00下午 ttys000 0:00.01 grep nginx
Linux环境还可以利用netstat查询进程ID,命令格式如:netstat -tunlp|grep [进程名称关键字]
示例如下(705即占用该进程的ID):
[root@test82 ~]# netstat -tunlp|grep nginx
tcp 0 0 0.0.0.0:1443 0.0.0.0:* LISTEN 705/nginx: master p
2.3、关闭某个进程
- Windows环境
根据进程ID关闭进程,命令格式如:taskkill /f /t /pid [进程ID]。
示例如下:
taskkill /f /t /pid 80
根据进程名称关闭进程,命令格式如:taskkill /f /t /pid [进程名称]。
示例如下:
taskkill /f /t /im nginx.exe
- Linux/MacOS环境
根据进程ID关闭进程,命令格式如:kill -9 [进程ID]
示例如下(多个端口空格隔开):
kill -9 82690 82689
2.4、组合命令-查询进程ID后关闭进程
利用4个命令:
- kill: 根据进程ID关闭进程
- ps:查看进程信息
- grep:文本搜索
- awk:通过指定分隔符(默认空格和制表符分隔),将一行分为多个字段,依次用 $1、$2 … $n 表示第一个字段、第二个字段… 第n个字段。
组合命令格式:kill -9 `ps -ef | grep [进程名称] | grep -v ‘grep’ | awk ‘{print $2}’`
示例如下:
kill -9 `ps -ef | grep nginx | grep -v 'grep' | awk '{print $2}'`