nginx 基础篇

Nginx 是俄罗斯人 lgor Sysoev(伊戈尔·塞索耶夫开发的一款高性能的 HTTP 利反向代理服务器 Nginxl 以高效的 epoll、kqueue、eventport 作为网络 IO 模型在高并发场景下 Nginx 能够轻松支持 5w 并发连安数的啊应,计三消耗的版务器内存 CPU 等系统资原消耗印很低运行非常稳定。3W 个 tcp 连接下,消耗的内存才

不到 150M

当前想让 nginx 支持 5 万并发,甚至百万级的并发,都是可以的,但需要做很多的优化工作,如

nginx 优化方向

想至少支持 5 万并发的基本调优

1. 服务器内存、CPU 硬件支持。如 8 核 16 线程、326 内存的服务器

2. 磁盘使用 SSD、或者购买至少 1500e 转的 SAS 企业级硬盘,做成 RAID0 3. 安装光纤网口

4. 使用 1inux 系统,如 centos7, 优化内核参数,对 TCp 连接的设置 

5. 优化 nginx, conf 中并发相关的参数,如等 
worker_processes 16;
worker_connections 50000;

6. 未完待续,ngnx 百万级并发优化篇的知识,,面试造火箭必备

nginx 介绍

nginx概念
1.nginx是一个高性能的HTTP服务器、反向代理服务器。
2.主要特点
- 开源源代码
- 高性能,并发性能、处理tcp连接性能极高
- 可靠,服务稳定,得到了全世界的验证。
nginx优点
1.每一家公司都会用到nginx(广泛应用)
2.技术成熟,是企业最常见且必须的工具
3.适用于任意架构,单机环境,集群环境,微服务架构、云原生架构等
nginx的重要特性
1.官网直接获取源码,免费用,讲道理这种高性能的软件,是很贵的。
2.高性能,官网提供测试数据,性能残暴,1秒内能支持5万个tcp连接
3.消耗资源很低,有数据证明在生产环境下3万左右的并发tcp连接,开启10个nginx进程消耗不到150M。
4.有能力可以自己对nginx进行二次开发,如淘宝的tengine。
5.模块化管理,nginx有大量的模块(插件),运维根据公司的业务需求,按需编译安装设置即可。
模块插件是为了提供额外的功能,如
- url重写,根据域名、url、客户端的不同,转发http请求到不同的机器
- https证书的支持
- 支持静态资源压缩
- 支持热部署、无须重启,更新配置文件
- 支持二次开发新插件
企业级应用
1.提供静态页面展示,网页服务
2.提供多个网站、多个域名的网页服务
3.提供反向代理服务(结合动态应用程序)
4.提供简单资源下载服务(密码认证)
5.用户行为分析(日志功能)

nginx架构

nginx是多进程架构,当启动nginx会使用root创建master进程,由master进程创建多个worker进程。
master主进程原理
1.启动时检查nginx.conf是否正确,语法错误;
2.根据配置文件的参数创建、且监控worker进程的数量和状态;
3.监听socket,接收client发起的请求,然后worker竞争抢夺链接,获胜的可以处理且响应请求。
4.接收运维超哥发送的管理nginx进程的信号,并且将信号通知到worker进程。
5.如果运维超哥发送了reload命令,则读取新配置文件,创建新的worker进程,结束旧的worker进程。
worker工作进程原理
1.实际处理client网络请求的是worker
2.master根据nginx.conf决定worker的数量
3.有client用户请求到达时,worker之间进程竞争,获胜者和client建立连接且处理用户请求;
4.接收用户请求后,若需要代理转发给后端,则后端处理完毕后接收处理结果,再响应给用户
5.接收并处理master发来的进程信号,如启动、重启、重载、停止。
nginx进程间通信图

image-20220816165428792

nginx处理http请求

img

nginx重要模块
[root@yuchao-tx-server ~]#nginx -V 该命令可见nginx已安装的模块,也是默认比较重要的模块

--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-compat
--with-debug
--with-file-aio
--with-google_perftools_module
--with-http_addition_module
--with-http_auth_request_module
--with-http_dav_module
--with-http_degradation_module
--with-http_flv_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_image_filter_module=dynamic
--with-http_mp4_module
--with-http_perl_module=dynamic
--with-http_random_index_module
--with-http_realip_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_ssl_module
--with-http_stub_status_module
--with-http_sub_module
--with-http_v2_module
--with-http_xslt_module=dynamic
--with-mail=dynamic
--with-mail_ssl_module
--with-pcre
--with-pcre-jit
--with-stream=dynamic
--with-stream_ssl_module
--with-stream_ssl_preread_module
--with-threads
--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
模块解释
官网
https://nginx.org/en/docs/
Nginx模块名称    模块作用
ngx_http_access_module    四层基于IP的访问控制,可以通过匹配客户端源IP地址进行限制
ngx_http_auth_basic_module    状态页,使用basic机制进行用户认证,在编译安装nginx的时候需要添加编译参数--withhttp_stub_status_module,否则配置完成之后监测会是提示语法错误
ngx_http_stub_status_module    状态统计模块
ngx_http_gzip_module    文件的压缩功能
ngx_http_gzip_static_module    静态压缩模块
ngx_http_ssl_module    nginx 的https 功能
ngx_http_rewrite_module    重定向模块,解析和处理rewrite请求
ngx_http_referer_module    防盗链功能,基于访问安全考虑
ngx_http_proxy_module    将客户端的请求以http协议转发至指定服务器进行处理
ngx_stream_proxy_module    tcp负载,将客户端的请求以tcp协议转发至指定服务器处理
ngx_http_fastcgi_module    将客户端对php的请求以fastcgi协议转发至指定服务器助理
ngx_http_uwsgi_module    将客户端对Python的请求以uwsgi协议转发至指定服务器处理
ngx_http_headers_module    可以实现对头部报文添加指定的key与值
ngx_http_upstream_module    负载均衡模块,提供服务器分组转发、权重分配、状态监测、调度算法等高级功能
ngx_stream_upstream_module    后端服务器分组转发、权重分配、状态监测、调度算法等高级功能
ngx_http_fastcgi_module    实现通过fastcgi协议将指定的客户端请求转发至php-fpm处理
ngx_http_flv_module    为flv伪流媒体服务端提供支持
核心模块如下

img

重定向模块

img

公司使用php作为后端,nginx反向代理php的模块

img

反向代理模块

img

负载均衡模块

img