深入理解Nginx笔记整理 - 1

基础

Nginx特点

  • 更快
    不仅仅是单体更快,高并发模式下也更快
  • 高扩展性
    从功能、层次、类型上低耦合设计
  • 高可靠性
    master管理的work进程间相互独立,master进程一旦关闭能快速拉起
  • 低内存
    10000非活跃的keepalive连接仅消耗2.5MB内存
  • 单机10万并发
    nginx先天事件驱动模型、全异步网络I/O模型、极少的进程间切换让nginx能够轻松应对单机10万并发
  • 热部署
  • BSD协议

准备

内核要求

要求linux kernel 2.6以上

uname -r

依赖包

  • gcc gcc++
yum install gcc gcc-c++ -y
  • pcre perl兼容正则表达式
yum install pcre pcre-devel -y
  • zlib gzip调用
yum install zlib zlib-devel -y
  • openssl 用于http ssl协议、MD5和sha1散列函数
yum install openssl openssl-devel -y

nginx常见Linux内核参数优化

  • sysctl.conf
fs.file-max = 999999 # 单进程能打开的文件句柄数
net.ipv4.tcp_tw_reuse = 1 # 允许将TIME-WAIT状态的socket重新用于新的TCP连接
net.ipv4.tcp_keepalive_time = 600 # TCP发送keepalive消息的频度,默认是2小时
net.ipv4.tcp_fin_timeout = 30 # 当服务器主动关闭连接时, socket保持在FIN-WAIT-2状态的最大时间
net.ipv4.tcp_max_tw_buckets = 5000 # 操作系统允许TIME_WAIT套接字数量的最大值
net.ipv4.ip_local_port_range = 1024 61000 # UDP和TCP连接中本地(不包括连接的远端)端口的取值范围
net.ipv4.tcp_rmem = 4096 32768 262142 # TCP接收缓存(用于TCP接收滑动窗口) 的最小值、 默认值、 最大值
net.ipv4.tcp_wmem = 4096 32768 262142 # TCP发送缓存(用于TCP发送滑动窗口) 的最小值、 默认值、 最大值
net.core.netdev_max_backlog = 8096 # 当网卡接收数据包的速度大于内核处理的速度时, 会有一个队列保存这些数据包
net.core.rmem_default = 262144 # 参数表示内核套接字接收缓存区默认的大小
net.core.wmem_default = 262144 # 这个参数表示内核套接字发送缓存区默认的大小
net.core.rmem_max = 2097152 # 这个参数表示内核套接字接收缓存区的最大大小。
net.core.wmem_max = 2097152 # 这个参数表示内核套接字接收缓存区的最大大小。
net.ipv4.tcp_syncookies = 1 # 这个参数表示内核套接字接收缓存区的最大大小。
net.ipv4.tcp_max_syn.backlog=1024 # TCP三次握手建立阶段接收SYN请求队列的最大长度

安装

下载

官网下载地址: http://nginx.org/en/download.html

  • mainline 单数
  • stable 双数

编译安装

./configure
make # 编译生成的nginx二进制文件会在objs文件夹中
make install

配置vim支持nginx语法

mkdir ~/.vim
cp -ar /ops-data/nginx-1.16.1/contrib/vim/* ~/.vim/

命令

  • 启动
    直接运行nginx二进制文件
  • -c 指定配置文件路径
  • -p 指定nginx工作目录
  • -g pid路径
  • -t 语法检测
  • -q 配合-t不打印error日志
  • -v 打印nginx版本
  • -V 打印nginx版本及编译带的参数
  • -s
    • stop 相当于kill -s SIGTERMkill -s SIGINT
    • quit 相当于kill -s SIGQUIT <master pid>kill -s SIGWINCH <worker pid>
    • reload 相当于kill -s SIGUP
    • reopen 相当于kill -s SIGUSR1

命令实战

日志切割

mv access.log access.$(date +%F:%H:%M:%S).log && ../sbin/nginx -s reopen
mv access.log access.$(date +%F:%H:%M:%S).log && kill -s SIGUSR1 $(cat nginx.pid)

热部署(平滑升级)

[root@nginx-node0 sbin]# ./nginx
[root@nginx-node0 sbin]# ps -ef | grep nginx
root      10729      1  0 23:37 ?        00:00:00 nginx: master process ./nginx
nobody    10730  10729  0 23:37 ?        00:00:00 nginx: worker process
root      10732   7269  0 23:38 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-node0 sbin]# ./nginx -v
nginx version: nginx/1.16.1
[root@nginx-node0 sbin]# cp /ops-data/nginx-1.17.4/objs/nginx .
cp: overwrite ‘./nginx’? y
cp: cannot create regular file ‘./nginx’: Text file busy
[root@nginx-node0 sbin]# cp -f /ops-data/nginx-1.17.4/objs/nginx .
cp: overwrite ‘./nginx’? y
[root@nginx-node0 sbin]# ./nginx -v
nginx version: nginx/1.17.4
[root@nginx-node0 sbin]# kill -s SIGUSR2 10729
[root@nginx-node0 sbin]# ps -ef | grep nginx
root      10729      1  0 23:37 ?        00:00:00 nginx: master process ./nginx
nobody    10730  10729  0 23:37 ?        00:00:00 nginx: worker process
root      10737  10729  0 23:39 ?        00:00:00 nginx: master process ./nginx
nobody    10738  10737  0 23:39 ?        00:00:00 nginx: worker process
root      10740   7269  0 23:39 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-node0 sbin]# kill -s SIGQUIT 10729
[root@nginx-node0 sbin]# ps -ef | grep nginx
root      10737      1  0 23:39 ?        00:00:00 nginx: master process ./nginx
nobody    10738  10737  0 23:39 ?        00:00:00 nginx: worker process
root      10742   7269  0 23:39 pts/0    00:00:00 grep --color=auto nginx

猜你喜欢

转载自www.cnblogs.com/drfung/p/11692479.html