Nginx常用命令

Nginx常用命令

本次实验nginx安装目录为/usr/local/nginx/

使用/usr/local/nginx/sbin/nginx -h命令查看可用参数:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.16.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

命令解读:

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

命令演示:
如果觉得每次都需要输入绝对路径执行命令麻烦,可以通过以下几种方法实现直接使用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优先找别名

启动nginx:

 nginx

立即停止nginx:

nginx -s stop

优雅停止nginx:

nginx -s quit

重新打开日志文件(该命令可用于日志切割,定期执行。)

[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

重载配置文件:
修改工作进程数,重启服务,对比前后进程数

[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

启动指定的配置文件:
在/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

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

[root@localhost logs]# /usr/local/nginx/sbin/nginx -g "daemon off;"
现在当前nginx在前端运行,
输入ctrl +c,则nginx就退出了。
可以使用ctrl +z放置后台运行。

实现配置文件语法高亮:

cp -r /usr/src/nginx-1.20.1/contrib/vim/* /usr/share/vim/vimfiles/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值