Nginx 详解
- Nginx 相关概念
- Nginx 安装及配置
- Nginx 官方文档说明
- main 配置段常见的配置指令
- 事件驱动相关的配置
- http协议及http模块相关配置
Nginx 相关概念
1、Nginx 概念
Nginx(engine X) — NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.
2、Nginx 功能
http协议:web服务器(类似于httpd)、http reverse proxy(类似于httpd)、imap/pop3 reverse proxy
3、Nginx 程序架构
3.1 master/worker
-
一个master进程:负载加载和分析配置文件、管理worker进程、平滑升级
-
一个或多个worker进程:处理并响应用户请求
-
缓存相关的进程:
cache loader # 载入缓存对象 cache manager # 管理缓存对象
3.2 特性
异步、事件驱动和非阻塞。
并发请求处理 # 通过epoll/select
文件IO # 高级IO sendfile,异步,mmap
3.3 Nginx 模块
高度模块化,但其模块早期不支持DSO机制;近期版本支持动态装载和卸载。
模块分类:
1、核心模块:core module
2、标准模块:
HTTP modules:
Standard HTTP modules
Optional HTTP modules
Mail modules
Stream modules:
传输层代理
3、3rd party modules:
3.4 Nginx 功用
- 静态的web资源服务器;(图片服务器,或js/css/html/txt等静态资源服务器)
- 结合FastCGI/uwSGI/SCGI等协议反代动态资源请求
- http/https协议的反向代理
- imap4/pop3协议的反向代理
- tcp/udp协议的请求转发
Nginx 安装及配置
1、Nginx 安装
-
自定义YUM官方仓库安装Nginx、常用命令及启动、进程查
https://blog.csdn.net/weixin_44983653/article/details/100808806
-
自建epel yum仓库并安装Nginx
https://blog.csdn.net/weixin_44983653/article/details/100840513
-
使用yum安装epel yum源,并安装nginx(与nginx官方源安装有微小区别)
https://blog.csdn.net/weixin_44983653/article/details/100840818
2、Nginx 编译安装
# yum groupinstall "Development Tools" "Server Platform Development"
# yum install pcre-devel openssl-devel zlib-devel
# useradd -r nginx
# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
# make && make install
3、Nginx 程序环境
3.1 配置文件的组成部分
-
配置文件的组成部分:
主配置文件:nginx.conf include conf.d/*.conf fastcgi, uwsgi,scgi等协议相关的配置文件 mime.types:支持的mime类型
-
主程序文件:/usr/sbin/nginx
-
Unit File:nginx.service
3.2 Nginx 主配置文件说明
- 主配置文件的配置指令:
directive value [value2 ...];
- 注意事项:
(1) 指令必须以分号结尾
(2) 支持使用配置变量
内建变量:由Nginx模块引入,可直接引用
自定义变量:由用户使用set命令定义
set variable_name value;
引用变量:$variable_name
3.3 Nginx 主配置文件结构
main block # 主配置段,也即全局配置段;
event {
# 事件驱动相关的配置,单进程响应N个请求
...
}
http {
# http/https 协议相关的配置段
...
}
mail {
...
}
stream {
...
}
3.4 Nginx http 协议相关的配置结构
http {
...
... # 各server的公共配置
server {
...
} # 每个server用于定义一个虚拟主机
server {
...
listen # 监听地址和端口
server_name # 主机名
root # 站点根目录(document_root)
alias # 路径别名
location [OPERATOR] URL {
# 针对于一些URL的条件
...
if CONDITION {
...
}
}
}
}
Nginx 官方文档说明
- 官方配置文档地址:
http://nginx.org/en/docs/
main 配置段常见的配置指令
-
官方配置指令说明:
http://nginx.org/en/docs/ngx_core_module.html
-
分类:
正常运行必备的配置 优化性能相关的配置 用于调试及定位问题相关的配置 事件驱动相关的配置
1、正常运行必备的配置
-
user
Defines user and group credentials used by worker processes. If group is omitted, a group whose name equals that of user is used.Syntax: user user [group]; Default: user nobody nobody; Context: main
-
pid /PATH/TO/PID_FILE
指定存储nginx主进程进程号码的文件路径。 -
include file | mask
指明包含进来的其它配置文件片断。 -
load_module file;
指明要装载的动态模块。
2、性能优化的相关配置
-
worker_processes number | auto;
worker进程的数量;通常应该等于小于当前主机的cpu的物理核心数 auto:当前主机物理CPU核心数
-
worker_cpu_affinity cpumask …;
worker_cpu_affinity auto [cpumask]; # worker与CPU进行绑定 worker_cpu_affinity auto; # 如果此服务器主要是做nginx服务器的话,可是使用此配置 # 如果还有其它重要程序运行,不建议使用
CPU MASK: 00000000 # 8个CPU的表示 00000001 # 0号CPU 00000010 # 1号CPU ... ...
服务器有8个CPU,nginx服务要启用4个worker进程,可以使用auto进行自动分配,也可以使用手动绑定 比如绑定后四个CPU,就是4、5、6、7号CPU(00010000、00100000、01000000、10000000)
-
worker_priority number;
指定worker进程的nice值,设定worker进程优先级;[-20,20] [root@Tang-Neo ~]# ps axo comm,pid,psr,ni | grep nginx # 默认nice值是0 nginx 7209 0 0 nginx 8812 3 0 nginx 8813 2 0 nginx 8814 1 0
-
worker_rlimit_nofile number;
所有worker进程所能够打开的文件数量上限 一个进程至少需要打一个套接字文件,1024个进程就需要至少打开1024个套接字文件 3个worker进程至少需要打开3*1024=3072个文件
3、调试、定位问题
1、daemon on|off;
# 是否以守护进程方式运行Nignx;(CentOS 7不需要)
2、master_process on|off;
# 是否以master/worker模型运行nginx;默认为on
3、error_log file [level];
4、示例
[root@Tang-Neo nginx]# vim nginx.conf
worker_processes 1;
[root@Tang-Neo nginx]# pwd
/etc/nginx
[root@Tang-Neo nginx]# ps -aux | grep nginx # 只有一个 worker 子进程
root 7209 0.0 0.0 46340 980 ? Ss 07:53 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 7210 0.0 0.2 46752 2176 ? S 07:53 0:00 nginx: worker process
root 7242 0.0 0.0 112708 976 pts/2 S+ 08:06 0:00 grep --color=auto nginx
[root@Tang-Neo nginx]# vim nginx.conf
worker_processes auto;
[root@Tang-Neo nginx]# nginx -t # 检查语法格式
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@Tang-Neo nginx]# nginx -s reload # 重载 nginx 程序,不需重启
[root@Tang-Neo nginx]# ps -aux | grep nginx # 根据物理CPU进行worker子进程分配,4个CPU就会产生4个worker子进程
root 7209 0.0 0.1 46468 1960 ? Ss 07:53 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 7248 0.0 0.2 46880 2064 ? S 08:07 0:00 nginx: worker process
nginx 7249 0.0 0.2 46880 2064 ? S 08:07 0:00 nginx: worker process
nginx 7250 0.0 0.2 46880 2064 ? S 08:07 0:00 nginx: worker process
nginx 7251 0.0 0.2 46880 2064 ? S 08:07 0:00 nginx: worker process
root 7253 0.0 0.0 112708 976 pts/2 S+ 08:08 0:00 grep --color=auto nginx
[root@Tang-Neo ~]# lscpu | grep "CPU(s)" # 本机四个CPU
CPU(s): 4
On-line CPU(s) list: 0-3
NUMA node0 CPU(s): 0-3
[root@Tang-Neo nginx]# vim nginx.conf
events {
# 单个 worker 进程响应N个请求,一共响应 4(worker的进程数量)*1024
worker_connections 1024;
}
[root@Tang-Neo ~]# ps axo comm,pid,psr | grep nginx # 显示程序的进程和运行在哪个CPU上
nginx 7209 3
nginx 7389 2
nginx 7390 0
nginx 7391 3
nginx 7392 1
[root@Tang-Neo ~]# watch -n.5 'ps axo comm,pid,psr | grep nginx' # 每0.5s运行一次命令
[root@Tang-Neo ~]# ps axo comm,pid,psr | grep nginx # 如果绑定(worker_cpu_affinity cpumask ...;)以后,就不会变化了
nginx 7209 3
nginx 7389 3
nginx 7390 1
nginx 7391 1
nginx 7392 0
[root@Tang-Neo ~]# vim /etc/nginx/nginx.conf # 编辑主配置文件,绑定CPU
user nginx;
worker_processes auto;
worker_cpu_affinity 1000 0100 0010 0001;
[root@Tang-Neo ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@Tang-Neo ~]# nginx -s reload
[root@Tang-Neo ~]# ps axo comm,pid,psr | grep nginx
nginx 7209 2
nginx 8795 3
nginx 8796 2
nginx 8797 1
nginx 8798 0
[root@Tang-Neo ~]# ab -n 10000 -c 100 http://192.168.1.9/index.html # 进行压力测试
[root@Tang-Neo ~]# ps axo comm,pid,psr | grep nginx # 进行压力测试后,运行的CPU也不会进行改变
nginx 7209 2
nginx 8795 3
nginx 8796 2
nginx 8797 1
nginx 8798 0
[root@Tang-Neo ~]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes 3;
worker_cpu_affinity 1000 0100 0010;
[root@Tang-Neo ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@Tang-Neo ~]# nginx -s reload
[root@Tang-Neo ~]# ps axo comm,pid,psr | grep nginx
nginx 7209 0
nginx 8812 3
nginx