一、Nginx的介绍
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
1.1nginx 相对 apache 的优点:
轻量级,同样起web 服务,比apache 占用更少的内存及资源
抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能
高度模块化的设计,编写模块相对简单
社区活跃,各种高性能模块出品迅速啊
Nginx本身就是一个反向代理服务器
Nginx支持7层负载均衡
nginx 适合做静态,简单,效率高
1.2Nginx的缺点是
Nginx仅能支持http、https和Email协议,这样就在适用范围上面小些,这个是它的缺点。
对后端服务器的健康检查,只支持通过端口来检测,不支持通过url来检测。不支持Session的直接保持,但能通过ip_hash来解决。
二、Nginx的应用
(1)适用于高并发
(2)可以做负载均衡服务器和HTTP服务器
(3)HTTP服务器(动静分离)
(4)正向代理
三、部署基本的nginx的服务器及其基本配置
3.2编译、安装
官网下载软件包
http://nginx.org/en/download.html
[root@localhost Desktop]# ls
nginx-1.18.0.tar.gz
[root@localhost Desktop]# tar zxf nginx-1.18.0.tar.gz 解压
[root@localhost Desktop]# ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost Desktop]# cd nginx-1.18.0/
[root@localhost nginx-1.18.0]# ls
auto CHANGES(一些bug) CHANGES.ru(作者俄罗斯人) conf配置文件
configure编译用二进制文件 contrib语法检测字体 html类似于Apache的默认发布页面
LICENSE证书 man文档 README说明 src源码目录
虚拟机的vim可以自动检测配置文件语法是否有错误:
mkdir ~/.vim建立一个家目录下的隐藏目录
cp -r contrib/vim/* ~/.vim自动检测配置文件语法是否有错误
[root@localhost nginx-1.18.0]# ./configure --help 查看帮助
试着访问一下,看到的是默认页面
四、nginx的版本更新和回退(热部署)
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx -V 查看当前版本信息
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
首先去官网上下载一个另外一个版本的nginx
当且替换更新的版本有问题回退到之前的版本
五、nginx的日志切割
5.1nginx为什么要进行日志切割?
nginx作为一个反向代理器,每天接收的请求非常庞大,日志文件也会比较多,一旦日志超过内存大小就会无法打开日志,导致服务器卡死,所有要进行切割,对于大的日志文件查看起来也非常方便。
5.2切割方法如下
日志文件太小,手动增大日志文件
日志的备份以凌晨十二点为节点,进行备份前一天的日志内容
六、nginx的gzip压缩
vim /usr/local/nginx/conf/nginx.conf 编辑nginx的配置文件
刷新一下文件变小,利于客户下载
七、nginx的启动
之前的启动nginx都是脚本方法
/usr/local/nginx/sbin/nginx
系统官方默认的服务启动脚本存放在:/usr/lib/systemd/system/httpd.service
,自己安装的服务避免放在那里,官方推荐我们放在/etc/systemd/system下
参照http服务的文件:
cp /usr/lib/systemd/system/httpd.service /etc/systemd/system/nginx.service
编辑配置文件/etc/systemd/system/nginx.service
[root@localhost system]# /usr/local/nginx/sbin/nginx -s stop 脚本方式关闭
[root@localhost system]# ps ax
[root@localhost system]# systemctl start nginx.service 开启
[root@localhost system]# ps ax
八、Nginx主配置文件中参数的详细解释
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost logs]# ../sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost logs]# ../sbin/nginx -s reload 报错
nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
[root@localhost logs]# ls
2020-05-05_access.log access.log error.log
[root@localhost logs]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 解决办法
[root@localhost logs]# ../sbin/nginx -s reload 成功
[root@localhost logs]# ps -ef | grep nginx
root 81671 1 0 05:07 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx 81683 81671 0 05:07 ? 00:00:00 nginx: worker process
root 81729 75651 0 05:08 pts/1 00:00:00 grep --color=auto nginx