nginx读书笔记一----介绍及命令行

nginx特点

  1. 响应速度快
  2. 高扩展性:高度模块化,耦合度降低
  3. 高可靠性:每个worker进程相对独立, master进程在1worker进程出错时可以快速拉起新的worker子进程提供服务 
  4. 低内存消耗:一般情况下, 10000个非活跃的HTTP Keep-Alive连接在Nginx中仅消耗2.5MB的内存 
  5. 高并发:据说单机支持10万以上的并发连接
  6. 热部署:master管理进程与worker工作进程的分离,支持不停止服务就可以更新配置

nginx命令行

nginx启动

[root@ans3 ~]# /usr/sbin/nginx 

也可以指定配置文件启动

[root@ans3 ~]# /usr/sbin/nginx -c /etc/nginx/nginx.conf

指定全局配置项启动

[root@ans3 ~]# vim /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
#pid /run/nginx.pid;

[root@ans3 ~]# /usr/sbin/nginx -g "pid /root/test.pid;"

[root@ans3 ~]# ll
-rw-r--r--  1 root root    5 Apr 21 22:32 test.pid

停止的时候也要指定此配置项
[root@ans3 ~]# /usr/sbin/nginx -g "pid /root/test.pid;" -s stop

 配置文件语法检查

[root@ans3 ~]# `which nginx` -t

配置文件语法检测只输出error级别的信息

[root@ans3 ~]# `which nginx` -t -q

查看编译时的参数

[root@ans3 ~]# `which nginx` -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

停止nginx服务

-s参数是向正在运行的Nginx服务的发送Linux信号量,nginx通过pid文件得到master的pid,再向master进程发送TERM信号来关闭nginx服务。

[root@ans3 ~]# `which nginx` -s stop

也可以使用kill命令向master进程发送TERM或者INT信号。

[root@ans3 ~]# kill -s SIGTERM <nginx master pid>
或者
[root@ans3 ~]# kill -s SIGINT <nginx master pid>

 优雅地停止服务

使用-s quit可以使nginx处理完当前所有请求后再停止运行

[root@ans3 ~]# `which nginx` -s quit

或者使用QUIT信号优雅地停止nginx

[root@ans3 ~]# `which nginx` -s SIGQUIT <nginx master pid>

如果想单独停止某个worker进程,可以通过发送WINCH信号实现

[root@ans3 ~]# kill -s SIGWINCH <nginx worker pid>

使nginx重新读取配置项

[root@ans3 ~]# `which nginx` -s reload

也可以发送HUP信号达到同样效果

[root@ans3 ~]# `which nginx` -s SIGHUP <nginx master pid>

重新打开新的日志文件

使用-s reopen参数可以重新打开日志文件, 这样可以先把当前日志文件改名或转移到其他目录中备份。重新打开时就会生成新的日志文件。可以防止日志文件过大

[root@ans3 ~]# `which nginx` -s reopen

也可以使用USR1达到同样的效果

kill -s SIGUSR1 <nginx master pid>

平滑升级nginx

1) 通知正在运行的旧版本Nginx准备升级。 通过向master进程发送USR2信号
kill -s SIGUSR2 <nginx master pid>
这时, 运行中的Nginx会将pid文件重命名, nginx/logs/nginx.pid重命名为nginx/logs/nginx.pid.oldbin, 这样新的Nginx才有可能启动成功。
2) 启动新版本的Nginx, 可以使用以上介绍过的任意一种启动方法。 这时通过ps命令可以发现新旧版本的Nginx在同时运行。
3) 通过kill命令向旧版本的master进程发送SIGQUIT信号, 以“优雅”的方式关闭旧版本的Nginx。 随后将只有新版本的Nginx服务运行, 此时平滑升级完毕。
原文地址:https://www.cnblogs.com/zh-dream/p/12748509.html