nignx的常用命令
注意:源码目录需要绝对路径;
选项
- -v:查看nginx的版本
- -V:可以查看版本、也可以查看编译参数
- ?:获取命令帮助
- -t:测试nginx的配置文件语法
- -T:通常结合重定向使用—>测试nginx的配置文件语法并且生成备份文件
- -q:如果配置文件没有错误信息时,不会有任何提示,如果有错误,则提示错误信息,与-t配合使用
- -s:常用选项–>用于给nginx发送信号
- stop:立刻停止nginx,不管请求是否完成
- quit:优雅退出,等待请求完成后退出
- reopen:重新打开日志,用于日志备份
- reload:重载配置文件
- -p:设置nginx家目录路径,默认是编译时的安装路径
- -c :设置nginx的配置文件,默认是家目录下的配置文件
- -g :设置nginx的全局变量,这个变量会覆盖配置文件中的变量。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12#查看版本信息以及编译参数 [root@node1 nginx]# /usr/local/nginx/sbin/nginx -V nginx version: JFWF/2.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) configure arguments: --prefix=/usr/local/nginx #备份nginx配置文件语法 [root@node1 nginx]# /usr/local/nginx/sbin/nginx -T >> /usr/local/nginx/conf/nginx.conf.bak 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
nginx配置文件的语法高亮
把nginx的源码vim语法复制到系统中–>即可完成高亮
复制代码
1
2
3[root@node1 nginx]# cp -a /usr/src/nginx-1.16.1/contrib/vim/* /usr/share/vim/vimfiles/
nginx的命令简化步骤
源码编译的nginx,启动都需要绝对路径,可以编辑,是的命令快速启动
软连接方式:
复制代码
1
2
3
4
5
6
7#做软连接: [root@node1 nginx]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin #然后重新读取下配置文件 [root@node1 nginx]#. /etc/profile ps:软连接做在PATH路径是第一位,因为yum安装的在/usr/sbin/目录下,which安装PATH的顺序找到第一个,就不找了。
环境变量方式
复制代码
1
2
3
4
5
6
7
8#配置环境变量: [root@node1 nginx]#echo "export PATH=/usr/local/nginx/sbin:$PATH" >/etc/profile.d/nginx.sh 然后重新读取下配置文件 [root@node1 nginx]#source /etc/profile ps:最好写在$PATH前面,否则,如果安装了yum版的nginx,直接执行nginx会启动yum版的nginx,因为which nginx,会先找到/usr/sbin/nginx文件
别名方式–>常用
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13#写入别名的文件 [root@node1 nginx]# vim ~/.bashrc alias nginx='/usr/local/nginx/sbin/nginx' #重新读取文件 [root@node1 nginx]# source ~/.bashrc #临时设置别名 [root@node1 nginx]#alias nginx='/usr/local/nginx/sbin/nginx' ps:which优先找别名
nginx的日志切割试验
1)备份日志目录名
复制代码
1
2
3
4
5
6
7[root@node1 nginx]# ls /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access.log #备份日志目录 [root@node1 nginx]# mv /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access.log.`date` [root@node1 nginx]# mv /usr/local/nginx/logs/access.log. /usr/local/nginx/logs/access.log.1
2)然后重新打开日志
复制代码
1
2
3
4
5
6
7
8
9
10#先查看一下nginx日志, [root@node1 nginx]# ls /usr/local/nginx/logs/ access.log.1 error.log nginx.pid #然后重载 [root@node1 nginx]# /usr/local/nginx/sbin/nginx -s reopen #新的nginx日志已经生成 [root@node1 nginx]# ls /usr/local/nginx/logs/ access.log access.log.1 error.log nginx.pid
3)结合定期任务使用
nginx的多实例讲解–>未完成
结合-c选项使用–>需要指定一下nginx的配置文件路径
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#复制nginx的配置文件到/data/目录下 [root@node1 nginx]# cp /usr/local/nginx/conf/nginx.conf /data/ [root@node1 nginx]# ls /data/nginx.conf /data/nginx.conf #修改data下的nginx文件 [root@node1 nginx]# vim /data/nginx.conf #user nobody; worker_processes 2; #修改这里为2,方便比较 ............ #当前只有一个nginx进程在运行 [root@node1 nginx]# ps -ef | grep nginx root 28220 1 0 07:31 ? 00:00:00 nginx: master process /usr/local/nginx/sbinnginx nobody 28403 28220 0 07:56 ? 00:00:00 nginx: worker process root 28446 7918 0 08:06 pts/1 00:00:00 grep --color=auto nginx #重新打开一个nginx,指定配置文件 [root@node1 nginx]# nginx -c /data/nginx.conf [root@node1 nginx]# ps -ef | grep nginx root 28553 1 0 08:14 ? 00:00:00 nginx: master process /usr/local/nginx/sbinnginx -c /data/nginx.conf nobody 28554 28553 0 08:14 ? 00:00:00 nginx: worker process nobody 28555 28553 0 08:14 ? 00:00:00 nginx: worker process root 28557 7918 0 08:14 pts/1 00:00:00 grep --color=auto nginx #多实例的配置文件错了-->导致nginx的多实例无法打开 #这里需要修改一下用户名 #修改配置文件 [root@node-130 ~]# vim /data/nginx.conf .......... user www; #这里指定用户名修改一下-->改为www ..... http { #include mime.types; #这里引用的配置文件也需要修改,或者注释掉 #添加www用户 [root@node-130 ~]# useradd -r -s /sbin/nologin www #开启多实例 [root@node-130 ~]# nginx -c /data/nginx.conf #这个实验好像还是失败了;
nginx全局变量
复制代码
1
2
3
4
5
6[root@node1 nginx]# /usr/local/nginx/sbin/nginx -g "daemon off;" 现在当前nginx在前端运行, 输入ctrl +c,则nginx就退出了。 可以使用ctrl +z放置后台运行。
最后
以上就是无奈向日葵最近收集整理的关于nginx的常用命令讲解的全部内容,更多相关nginx内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复