Nginx 是什么?
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器。
Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的.
第一个公开版本0.1.0发布于2004年10月4日。
四个发行版本
- 开源版:https://nginx.org/
- 商业版:https://nginx.com/ f5
- Openresty: https://openresty.org/cn/
- Tengine: https://tengine.taobao.org/
为什么选择Nginx?
互联网公司大都选择 Nginx
- Nginx 技术成熟,具备的功能是企业最常使用而且最需要的
- 适合当前主流架构趋势, 微服务、云架构、中间层
- 统一技术栈, 降低维护成本, 降低技术更新成本。
Nginx 特点
- 开源,可以从官网直接获取源代码
- 高并发:高性能,Nginx性能非常残暴,支持海量并发,顶住10万以上连接是没有问题的
- 低内存消耗:在高性能的同时,保持很低的内存消耗;
- 模块化:Nginx具有丰富的模块可以按需使用,并且有开发能力的技术人员还可以二次开发
- 热启动:例如当修改配置文件后,不需要停止与启动就可以让配置生效
Nginx 应用场景
Nginx 应用场景
Nginx 的组成
Nginx 应用场景
可以将Nginx 理解为一辆小汽车,nginx.conf 可比作驾驶员,控制这Nginx的的行为,但是当汽车在驾驶的过程的过程中,出现了问题,这个时候就需要一个黑匣子到底是汽车本身出现了问题,还是驾驶员驾驶不当出现了问题。Nginx 二进制可执行文件是由本身的框架和第三方模块,编译的可执行文件,这个文件可以理解为汽车本身。它有完整的系统,所有的功能都有它提供。access.log 可以理解为这个汽车经过任何一个地方形成的一个GPS轨迹,error.log 可以理解为汽车的黑匣子,当遇到不可预期的问题时,可以查看该日志文件记录信息进行问题定位。如果要对nginx 进行运维分析,可以对access.log进行分析。如果遇到一些未知的错误,这时就要对error.log进行分析了。
Nginx 重要配置文件说明
1. 查看配置文件
1
2
3
4
5
6
7
8
9
10
11
12
|
$ rpm -ql nginx
...................................................
/etc/logrotate.d/nginx #nginx日志切割的配置文件
/etc/nginx/nginx.conf #nginx主配置文件
/etc/nginx/conf.d #子配置文件
/etc/nginx/conf.d/default.conf #默认展示的页面一样
/etc/nginx/mime.types #媒体类型 (http协议中的文件类型)
/etc/sysconfig/nginx #systemctl 管理 nginx的使用的文件
/usr/lib/systemd/system/nginx.service #systemctl 管理nginx(开 关 重启 reload)
/usr/sbin/nginx #nginx命令
/usr/share/nginx/html #站点目录 网站的根目录
/var/log/nginx #nginx日志 access.log 访问日志
|
2. 查看已经编译的模块
1
2
3
4
5
6
|
[root@www html]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --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'
|
配置文件注解
- 第一部分:配置文件主区域配置
1
2
3
4
|
user nginx; #定义运行nginx进程的用户
worker_processes auto; #Nginx运行的work进程数量(建议与CPU数量一致或 auto)
error_log /var/log/nginx/error.log warn; #nginx错误日志
pid /var/run/nginx.pid; #nginx运行pid
|
- 第二部分:配置文件事件区域
1
2
3
|
events {
worker_connections 1024; #每个 worker 进程支持的最大连接数
}
|
- 第三部分:配置http区域
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
http {
include /etc/nginx/mime.types; #Nginx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; #访问日志保存路径
sendfile on; #开启高效传输模式
#tcp_nopush on; #必须配合tcp_nopush使用,当数据包累计到一定大小后就发送
keepalive_timeout 65; #连接超时时间,单位是秒
#gzip on; #开启文件压缩
include /etc/nginx/conf.d/*.conf; #包含子配置文件
}
|
- 第四部分:子配置文件内容
1
2
3
4
5
6
7
8
|
server {
listen 80; #指定监听端口
server_name localhost; #指定监听的域名
location / {
root /usr/share/nginx/html; #定义站点的目录
index index.html index.htm; #定义首页文件
}
}
|
http server location 扩展了解项
- http{}层下允许有多个 Server{}层,一个 Server{}层下又允许有多个 Location
- http{} 标签主要用来解决用户的请求与响应。
- server{} 标签主要用来响应具体的某一个网站。
- location{} 标签主要用于匹配网站具体 URL 路径
Nginx 配置语法
- 配置文件由指令和指令块构成
- 每条指令以;分号结尾,指令与参数间以空格符号分隔
- 指令块以{} 大括号将多条指令组织在一起
- include 语句允许组合多个配置文件以提升可维护性
- 使用#符号添加注释,提高可读性
- 使用$符号使用变量
- 部分指令的参数支持正则表达式
Nginx 语法示例
Nginx 语法示例
配置参数
时间的单位
- ms: milliseconds
- s: seconds
- m: minutes
- h: hours
- d: days
- w: weeks
- M: months,30 days
- y: years,.365 days
空间的单位
- bytes
- kilobytes
- megabytes
- gigabytes
http 配置的指令块
- http
- server
- upstream
- location
http 配置的指令块
Nginx 命令行
- 格式: nginx- s reload
- 帮助: -h
- 使用指定的配置文件: -c
- 指定配置命令: -g
- 指定运行目录: -p
- 发送信号:-s
- 立刻停止服务: stop
- 优雅的停止服务:quit
- 重载配置文件:reload
- 重新开始记录日志文件:reopen
- 测试配置文件是否有语法错误:-t -T
- 打印nginx 的版本信息、编译信息等:-v -V
参考