nginx
nginx的特性和优点
nginx的特性
nginx是一个很牛的高性能Web和反向代理服务器,它具有很多非常优越的特性:
1.在高连接并发的情况下,nginx是Apache服务器不错的替代品,能够支持高达50000个并发连接数的响应
2.使用epoll and kqueue作为开发模型
3.nginx作为负载均衡服务器:nginx既可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
4.nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好很多
nginx的优点
1.高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2-3万并发连接数
2.内存消耗少:在3万并发连接下,开启的10个nginx进程才消耗150M内存(15M*10=150M)
3.配置文件非常简单:风格跟程序一样通俗易懂
4.成本低廉:nginx为开源软件,可以免费使用。而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则需要十多万至几十万人民币
5.支持Rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组
6.内置的健康检查功能:如果Nginx Proxy后端的某台Web服务器宕机了,不会影响前端访问
7.节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头
8.稳定性高:用于反向代理,宕机的概率微乎其微
9.模块化设计:模块可以动态编译
10.外围支持好:文档全,二次开发和模块较多
11.支持热部署:可以不停机重载配置文件
12.支持事件驱动、AIO(AsyncIO,异步IO)、mmap(Memory Map,内存映射)等性能优化
nginx的功能及应用类别
nginx的基本功能
静态资源的web服务器,能缓存打开的文件描述符
http、smtp、pop3协议的反向代理服务器
缓存加速、负载均衡
支持FastCGI(fpm,LNMP),uWSGI(Python)等
模块化(非DSO机制),过滤器zip、SSI及图像的大小调整
支持SSL
nginx的模块与工作原理
nginx由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
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等操作交互,实现服务代理和负载均衡等功能
nginx模块分为:核心模块、事件模块、标准Http模块、可选Http模块、邮件模块、第三方模块和补丁等
nginx基本模块:所谓基本模块,指的是nginx默认的功能模块,它们提供的指令,允许你使用定义nginx基本功能的变量,在编译时不能被禁用,包括:
核心模块:基本功能和指令,如进程管理和安全。常见的核心模块指令,大部分是放置在配置文件的顶部
事件模块:在Nginx内配置网络使用的能力。常见的events(事件)模块指令,大部分是放置在配置文件的顶部
配置模块:提供包含机制具体的指令,请参考nginx的官方文档 nginx的工作原理
nginx的模块直接被编译进nginx,因此属于静态编译方式。
启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。
nginx的进程架构: 启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生
worker线程,一个worker线程用来处理n个request。

下图展示了基本的WEB服务请求步骤

nginx的安装与配置
创建系统用户nginx
[root@czh ~]# useradd -r -M -s /sbin/nologin nginx
安装相关的依赖包
[root@czh ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ vim wget make
创建日志存放目录
[root@czh ~]# mkdir -p /var/log/nginx
[root@czh ~]# chown -R nginx.nginx /var/log/nginx
下载nginx
wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@czh ~]# ls
anaconda-ks.cfg nginx-1.20.1.tar.gz
编译安装
[root@czh 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
[root@czh nginx-1.20.1]# make install
设置环境变量
[root@czh nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@czh nginx-1.20.1]# . /etc/profile.d/nginx.sh
//服务控制方式,使用nginx命令
-t //检查配置文件语法
-v //输出nginx的版本
-c //指定配置文件的路径
-s //发送服务控制信号,可选值有{stop|quit|reopen|reload}
启动nginx
[root@czh ~]# nginx -v
nginx version: nginx/1.20.1
[root@czh ~]# nginx -s stop
[root@czh ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@czh ~]# nginx
[root@czh ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
查看
[root@czh html]# echo 'hello word' >index.html
[root@czh html]# pwd
/usr/local/nginx/html

nginx的配置文件详解
主配置文件:/usr/local/nginx/conf/nginx.conf
默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
可以在启动nginx时通过-c选项来指定要读取的配置文件
nginx常见的配置文件及其作用
| 配置文件 | 作用 |
|---|---|
| nginx.conf | nginx的基本配置文件 |
| mime.types | MIME类型关联的扩展文件 |
| fastcgi.conf | 与fastcgi相关的配置 |
| proxy.conf | 与proxy相关的配置 |
| sites.conf | 配置nginx提供的网站,包括虚拟主机 |
nginx.conf配置详解
nginx.conf的内容分为以下几段:
main配置段:全局配置段。其中main配置段中可能包含event配置段
event {}:定义event模型工作特性
http {}:定义http协议相关的配置
配置指令:要以分号结尾,语法格式如下:
derective value1 [value2 ...];
支持使用变量:
内置变量:模块会提供内建变量定义
自定义变量:set var_name value
用于调试、定位问题的配置参数
daemon {on | off}; //是否以守护进程方式运行nginx,调式时应设置为off
master_process {on | off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别; //配置错误日志
error_log里的位置和级别能有以下可选项:
| 位置 | 级别 |
|---|---|
| file stderr syslog:server=address[,parameter=value] memory:size | debug:若要使用debug级别,需要在编译nginx时使用–with-debug选 info notice warn error crit alert emerg |
不以守护进程方式运行nginx
[root@czh ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
daemon off;
[root@czh ~]# nginx -s stop
[root@czh ~]# nginx
它会将后台运行的nginx转换到前台运行,但是它的服务还是会启动的
重新再开一台可以看到80起来了
[root@czh ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
不以master/worker模型运行nginx
[root@czh ~]# ps -elf | grep nginx
1 S root 513570 1 0 80 0 - 20407 - 11:59 ? 00:00:00 nginx: master process nginx
5 S nginx 513571 513570 0 80 0 - 28575 do_epo 11:59 ? 00:00:00 nginx: worker process
0 R root 514254 409930 0 80 0 - 3087 - 11:59 pts/2 00:00:00 grep --color=auto nginx
[root@czh ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
master_process off;
[root@czh ~]# nginx -s stop
[root@czh ~]# nginx
[root@czh ~]# ps -elf |grep nginx
1 S root 516651 1 0 80 0 - 20520 do_epo 12:00 ? 00:00:00 nginx
0 R root 517265 387609 0 80 0 - 3087 - 12:00 pts/0 00:00:00 grep --color=auto nginx
正常运行必备的配置参数
user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组
pid /path/to/pid_file; //指定nginx守护进程的pid文件
worker_rlimit_nofile number; //设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_core size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
设置日志位置和级别
[root@czh ~]# 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@host ~]# nginx -s reload
[root@czh ~]# tail -f /usr/local/nginx/logs/error.log
2021/06/23 12:03:10 [notice] 516651#0: using the "epoll" event method
2021/06/23 12:04:53 [emerg] 531214#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2021/06/23 12:04:53 [notice] 531214#0: try again to bind() after 500ms
2021/06/23 12:04:53 [emerg] 531214#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2021/06/23 12:04:53 [notice] 531214#0: try again to bind() after 500ms
2021/06/23 12:04:53 [emerg] 531214#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
指定用户运行worker进程
[root@czh ~]# 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@czh ~]# nginx -s reload
[root@czh ~]# ps -elf | grep nginx
5 S root 532326 1 0 80 0 - 20442 - 12:05 ? 00:00:00 nginx: master process nginx
5 S nobody 538491 532326 0 80 0 - 28604 do_epo 12:07 ? 00:00:00 nginx: worker process
0 R root 538933 387609 0 80 0 - 3087 - 12:07 pts/0 00:00:00 grep --color=auto nginx
[root@czh ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
[root@czh ~]# nginx -s reload
[root@czh ~]# ps -elf | grep nginx
5 S root 532326 1 0 80 0 - 20418 - 12:05 ? 00:00:00 nginx: master process nginx
5 S nginx 541376 532326 0 80 0 - 28579 do_epo 12:07 ? 00:00:00 nginx: worker process
0 R root 541972 387609 0 80 0 - 3087 - 12:08 pts/0 00:00:00 grep --color=auto nginx
开启nginx守护进程的pid文件
[root@czh ~]# vim /usr/local/nginx/conf/nginx.conf
#user nginx nginx;
worker_processes 1;
#[root@czh ~]# vim /usr/local/nginx/conf/nginx.conf
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
[root@czh ~]# nginx -s reload
[root@czh ~]# ls /usr/local/nginx/logs/
error.log nginx.pid
本文介绍了nginx的特性,如高并发、内存消耗低,以及作为反向代理和负载均衡服务器的优势。讨论了nginx的模块化设计和工作原理,强调了其稳定性与热部署能力。同时,概述了nginx的安装与配置过程,包括配置文件的详细解释。
2051

被折叠的 条评论
为什么被折叠?



