1. nginx简介!!!
Nginx 是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。
第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
nginx的特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
2. Nginx的特性与优点!!!
2.1 nginx的特性!!!
nginx是一个很牛的高性能Web和反向代理服务器,它具有很多非常优越的特性
- nginx是一个很牛的高性能Web和反向代理服务器,它具有很多非常优越的特性
- 使用epoll and kqueue作为开发模型
- nginx作为负载均衡服务器:nginx既可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
- nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好很多
2.2 nginx的优点!!!
- 高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2-3万并发连接数
- 内存消耗少:在3万并发连接下,开启的10个nginx进程才消耗150M内存
- 配置文件非常简单:风格跟程序一样通俗易懂
- 成本低廉:nginx为开源软件,可以免费使用。
- 支持Rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组
- 内置的健康检查功能:如果Nginx Proxy后端的某台Web服务器宕机了,不会影响前端访问
- 节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头
- 稳定性高:用于反向代理,宕机的概率微乎其微
- 模块化设计:模块可以动态编译
- 外围支持好:文档全,二次开发和模块较多
- 支持热部署:可以不停机重载配置文件
- 支持事件驱动、AIO(AsyncIO,异步IO)、mmap(Memory Map,内存映射)等性能优化
3. nginx的功能及应用类别!
3.1 Nginx的基本功能!
- 静态资源的web服务器,能缓存打开的文件描述符
- http、smtp、pop3协议的反向代理服务器
- 缓存加速、负载均衡
- 支持FastCGI(fpm,LNMP),uWSGI(Python)等 模块化(非DSO机制),过滤器zip、SSI及图像的大小调整
- 支持SS
3.2 nginx的扩展功能!
- 基于名称和IP的虚拟主机
- 支持keepalive 支持平滑升级
- 定制访问日志、支持使用日志缓冲区提高日志存储性能
- 支持URL重写,支持路径别名,支持基于IP及用户的访问控制,支持速率限制,支持并发数限制
3.3 nginx的应用类别!
- 使用nginx结合FastCGI运行PHP、JSP、Perl等程序
- 使用nginx作反向代理、负载均衡、规则过滤
- 使用nginx运行静态HTML网页、图片
- nginx与其他新技术的结合应用
4. nginx模块与工作原理!
nginx由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个locationblock(location是nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
4.1 nginx模块!
nginx的模块从结构上分为核心模块、基础模块和第三方模块
- HTTP模块、EVENT模块和MAIL模块等属于核心模块
- HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块属于基本模块
- HTTP Upstream模块、Request Hash模块、Notice模块和HTTP Access Key模块属于第三方模块
nginx模块从功能上分为三类,分别是
- Handlers(处理器模块)。此类模块直接处理请求,并进行输出内容和修改headers信息等操作。handlers处理器模块一般只能有一个
- Filters(过滤器模块)。此类模块主要对其他处理器模块输出的内容进行修改操作,最后由nginx输出
- Proxies(代理器模块)。就是nginx的HTTP Upstream之类的模块,这些模块主要与后端一些服务比如fastcgi等操作交互,实现服务代理和负载均衡等功能
4.2 nginx工作原理!
5. nginx部署!!!
5.1 nginx的安装!!!
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
CentOS Stream 8 - AppStream 593 B/s | 4.4 kB 00:07
CentOS Stream 8 - AppStream 2.9 MB/s | 10 MB 00:03
CentOS Stream 8 - BaseOS 8.8 kB/s | 3.9 kB 00:00
CentOS Stream 8 - BaseOS 2.9 MB/s | 6.8 MB 00:02
CentOS Stream 8 - Extras 1.8 kB/s | 3.0 kB 00:01
CentOS Stream 8 - Extras 10 kB/s | 13 kB 00:01
软件包 openssl-1:1.1.1g-11.el8.x86_64 已安装。
依赖关系解决。
================================================================================================================================
软件包 架构 版本 仓库 大小
================================================================================================================================
安装:
gcc x86_64 8.5.0-2.el8 appstream 23 M
gcc-c++ x86_64 8.5.0-2.el8 appstream 12 M
gd-devel x86_64 2.2.5-7.el8 appstream 50 k
make x86_64 1:4.2.1-10.el8 baseos 498 k
openssl-devel x86_64 1:1.1.1k-1.el8 baseos 2.3 M
pcre-devel x86_64 8.42-6.el8 baseos 551 k
[root@localhost ~]# yum -y groups mark install 'Development Tools'
上次元数据过期检查:0:02:11 前,执行于 2021年06月23日 星期三 11时35分19秒。
依赖关系解决。
================================================================================================================================
软件包 架构 版本 仓库 大小
================================================================================================================================
安装组:
Development Tools
事务概要
================================================================================================================================
完毕!
[root@localhost ~]#
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
--2021-06-23 11:39:16-- http://nginx.org/download/nginx-1.20.1.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1061461 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.20.1.tar.gz”
nginx-1.20.1.tar.gz 100%[=======================================================>] 1.01M 433KB/s 用时 2.4s
2021-06-23 11:39:19 (433 KB/s) - 已保存 “nginx-1.20.1.tar.gz” [1061461/1061461])
5.2 编译安装!!
[root@localhost ~]# tar xf nginx-1.20.1.tar.gz
[root@localhost ~]# cd nginx-1.20.1/
[root@localhost nginx-1.20.1]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
checking for OS
+ Linux 4.18.0-257.el8.x86_64 x86_64
[root@localhost nginx-1.20.1]# make -j 4
[root@localhost nginx-1.20.1]# make install
5.3 配置环境变量并且启动!!!!
[root@localhost nginx-1.20.1]# echo "export PATH=/usr/local/nginx/sbin:$PATH" >/etc/profile.d/nginx.sh
[root@localhost nginx-1.20.1]# . /etc/profile.d/nginx.sh
[root@localhost nginx-1.20.1]# nginx
[root@localhost nginx-1.20.1]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
5.4 nginx.conf的相关配置!!
daemon {on | off}; //是否以守护进程方式运行nginx,调式时应设置为off
master_process {on | off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别; //配置错误日志
不以守护进程的方式运行nginx!!
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
deamon off;
#error_log logs/error.log;
[root@localhost ~]# nginx
5.5 不以master/worker模式运行nginx!!!!
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
master_process off;
#error_log logs/error.log;
#error_log logs/error.log notice;
[root@localhost ~]# nginx
[root@localhost ~]# ps -ef | grep nginx
root 218965 1 0 22:29 ? 00:00:00 nginx
root 220339 129684 0 22:29 pts/7 00:00:00 grep --color=auto nginx
5.6 设置日志位置和级别!!!
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
[root@localhost ~]# nginx
[root@localhost ~]# tail -f /usr/local/nginx/logs/error.log
2021/06/23 22:33:24 [notice] 246454#0: using the "epoll" event method
2021/06/23 22:33:24 [notice] 246454#0: nginx/1.20.1
2021/06/23 22:33:24 [notice] 246454#0: built by gcc 8.5.0 20210514 (Red Hat 8.5.0-2) (GCC)
2021/06/23 22:33:24 [notice] 246454#0: OS: Linux 4.18.0-257.el8.x86_64
2021/06/23 22:33:24 [notice] 246454#0: getrlimit(RLIMIT_NOFILE): 1024:262144
2021/06/23 22:33:24 [notice] 246455#0: start worker processes
2021/06/23 22:33:24 [notice] 246455#0: start worker process 246456
5.7 设配参数!!!
user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组。
pid /path/to/pid_file; //指定nginx守护进程的pid文件
worker_rlimit_nofile number; //设置所有worker进程最大可以打开的文件数,默认为1024.
worker_rlimit_core size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
5.8 指定nginx用户运行nginx进程!!!
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
#error_log logs/error.log;
[root@localhost ~]# nginx
[root@localhost ~]# ps -elf | grep nginx
0 T root 249209 129684 0 80 0 - 1889 - 22:33 pts/7 00:00:00 tail -f /usr/local/nginx/logs/error.log
1 S root 279204 1 0 80 0 - 20407 - 22:37 ? 00:00:00 nginx: master process nginx
5 S nginx 279214 279204 0 80 0 - 28575 do_epo 22:37 ? 00:00:00 nginx: worker process
0 S root 280348 129684 0 80 0 - 3087 - 22:37 pts/7 00:00:00 grep --color=auto nginx
5.9 指定nginx守护进程的pid文件!!!!
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
pid logs/nginx.pid;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
[root@localhost ~]# nginx
[root@localhost ~]# ls /usr/local/nginx/logs/
error.log nginx.pid