2021年7月

在上一篇中,我们主要讲到了nginx的安装,分别介绍了yum安装与源码编译安装。
这篇文章主要探讨nginx的相关指令的用法。

  1. 使用/usr/local/nginx/sbin/nginx -h命令查看可用参数:
[root@localhost ~]# /usr/local/nginx/sbin/nginx  -h
nginx version: nginx/1.18.0
Usage: 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
1.1 命令解读:
-v      可查看nginx的版本。
-V      可查看nginx的详细信息,包括编译的参数。
-t      可用来测试nginx的配置文件的语法错误。
-T      可用来测试nginx的配置文件语法错误,同时还可以通过重定向备份nginx的配置文件。
-q      如果配置文件没有错误信息时,不会有任何提示,如果有错误,则提示错误信息,与-t配合使用。
-s      发送信号给master处理:
        stop    立刻停止nginx服务,不管请求是否处理完
        quit    优雅的退出服务,处理完当前的请求退出
        reopen  重新打开日志文件,原日志文件要提前备份改名。
        reload  重载配置文件
-p      设置nginx家目录路径,默认是编译时的安装路径
-c      设置nginx的配置文件,默认是家目录下的配置文件
-g      设置nginx的全局变量,这个变量会覆盖配置文件中的变量。
1.2 命令演示:

如果觉得每次都需要输入绝对路径执行命令麻烦,可以通过以下几种方法实现直接使用nginx命令。

1、做软连接:
ln  -s /usr/local/nginx/sbin/* /usr/local/sbin
然后重新读取下配置文件
. /etc/profile

ps:软连接做在PATH路径是第一位,因为yum安装的在/usr/sbin/目录下,
which安装PATH的顺序找到第一个,就不找了。

2、配置环境变量:
echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh

然后重新读取下配置文件
source /etc/profile

ps:最好写在$PATH前面,否则,如果安装了yum版的nginx,
直接执行nginx会启动yum版的nginx,因为which nginx,会先找到/usr/sbin/nginx文件

3、设置别名:

alias  nginx='/usr/local/nginx/sbin/nginx'

ps:which优先找别名
1.2.1 启动nginx:
nginx
1.2.2 立即停止nginx:
nginx  -s stop
1.2.3 优雅停止nginx:
[root@node1 ]# nginx -s quit
[root@node1 ]# ps -ef | grep nginx
root       1547      1  0 14:48 ?        00:00:00 nginx: master process nginx
nginx      1548   1547  1 14:48 ?        00:00:09 nginx: worker process is shutting down
root       1576   1225  0 14:58 pts/0    00:00:00 grep --color=auto nginx
1.2.4 重新打开日志文件

该命令可用于日志切割,定期执行。

[root@localhost logs]# ls
access.log  error.log  nginx.pid
[root@localhost logs]# mv access.log{,.bak}
[root@localhost logs]# ls
access.log.bak  error.log  nginx.pid
[root@localhost logs]# /usr/local/nginx/sbin/nginx -s reopen
[root@localhost logs]# ls
access.log  access.log.bak  error.log  nginx.pid
1.2.5 重载配置文件:

修改工作进程数,重启服务,对比前后进程数

[root@localhost logs]# ps -ef|grep nginx
root      2685     1  0 23:56 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     2686  2685  0 23:56 ?        00:00:00 nginx: worker process
root      2691  2532  0 23:57 pts/1    00:00:00 grep --color=auto nginx
[root@localhost logs]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost logs]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost logs]# ps -ef|grep nginx
root      2685     1  0 23:56 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     2694  2685  0 23:58 ?        00:00:00 nginx: worker process
nginx     2695  2685  0 23:58 ?        00:00:00 nginx: worker process
nginx     2696  2685  0 23:58 ?        00:00:00 nginx: worker process
root      2698  2532  0 23:58 pts/1    00:00:00 grep --color=auto nginx
1.2.6 启动指定的配置文件:

在/data/目录下拷贝一份nginx的配置文件,然后修改用户名为www。

==注意==:配置文件中引用的其他配置文件路径也要做一个修改。

[root@localhost logs]# cp /usr/local/nginx/conf/nginx.conf /data/
[root@localhost logs]# vim /data/nginx.conf
[root@localhost logs]# /usr/local/nginx/sbin/nginx  -c /data/nginx.conf 
[root@localhost logs]# ps -ef|grep nginx
root      2736     1  0 00:05 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /data/nginx.conf
www       2737  2736  0 00:05 ?        00:00:00 nginx: worker process
www       2738  2736  0 00:05 ?        00:00:00 nginx: worker process
www       2739  2736  0 00:05 ?        00:00:00 nginx: worker process
root      2741  2532  0 00:05 pts/1    00:00:00 grep --color=auto nginx
1.2.7 设置全局变量

通过设置全局变量,让nginx在前端运行。

[root@localhost logs]# /usr/local/nginx/sbin/nginx  -g "daemon off;"

现在当前nginx在前端运行,
输入ctrl +c,则nginx就退出了。
可以使用ctrl +z放置后台运行。
1.2.8 实现配置文件语法高亮:
cp -a /usr/src/nginx-1.18.0/contrib/vim/* /usr/share/vim/vimfiles/

更多精彩内容更新,欢迎大家关注我的知乎:运维朱工