Nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。
Nginx 可以在大多数 UnixLinux OS 上编译运行,并有 Windows 移植版。 Nginx 的1.4.0稳定版已经于2013年4月24日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高。
Nginx相较于Apache、lighttpd具有占有内存少,稳定性高等优势,并且依靠并发能力强,丰富的模块库以及友好灵活的配置而闻名。 在Linux操作系统下,nginx使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或 FreeBSD操作系统上采用类似于epoll的高效事件模型kqueue。
Nginx的优势
1、作为Web服务器,Nginx处理静态文件、索引文件,自动索引的效率非常高
2、作为代理服务器,Nginx可以实现无缓存的反向代理加速,提高网站运行速度
3、作为负载均衡服务器,Nginx既可以在内部直接支持Rails和PHP,也可以支持HTTP代理服务器对外进行服务,同时还支持简单的容错和利用算法进行负载均衡
4、在性能方面,Nginx是专门为性能优化而开发的,实现上非常注重效率。它采用内核Poll模型,可以支持更多的并发连接,最大可以支持对5万个并发连接数的响应,而且只占用很低的内存资源
5、在稳定性方面,Nginx采取了分阶段资源分配技术,使得CPU与内存的占用率非常低。Nginx官方表示,Nginx保持1万个没有活动的连接,而这些连接只占用2.5MB内存,因此,类似DOS这样的攻击对Nginx来说基本上是没有任何作用的
6、在高可用性方面,Nginx支持热部署,启动速度特别迅速,因此可以在不间断服务的情况下,对软件版本或者配置进行升级,即使运行数月也无需重新启动,几乎可以做到7x24小时不间断地运行。
Nginx的安装、配置与维护
安装
nginx的获取
RPM包获取:http://nginx.org/packages/
源码包获取:http://nginx.org/download/
nginx的rpm包安装
# 下载nginx的rpm包
[root@localhost ~]# wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
--2019-04-16 14:34:56-- http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 771856 (754K) [application/x-redhat-package-manager]
Saving to: ‘nginx-1.14.2-1.el7_4.ngx.x86_64.rpm’
100%[======================================>] 771,856 34.1KB/s in 21s
2019-04-16 14:35:18 (36.4 KB/s) - ‘nginx-1.14.2-1.el7_4.ngx.x86_64.rpm’ saved [771856/771856]
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
# 安装nginx
[root@localhost ~]# yum localinstall -y nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
nginx源码安装
# 添加nginx用户和用户组
[root@localhost ~]# groupadd -r -g 108 nginx
[root@localhost ~]# useradd -r -u 108 -g 108 nginx
[root@localhost ~]# id nginx
uid=108(nginx) gid=108(nginx) groups=108(nginx)
# 解压
[root@localhost ~]# tar xvf nginx-1.8.0.tar.gz -C /usr/local/src/
# 安装依赖包 gcc gcc-c++ make recp pcre-devel openssl openssl-devel
[root@localhost ~]# yum install -y gcc gcc-c++ make recp pcre-devel openssl openssl-devel
# 安装nginx
[root@localhost src]# cd /usr/local/src/nginx-1.8.0/
[root@localhost nginx-1.8.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-openssl=/usr/local/openssl
### 安装参数
参数详解:
--prefix= 指向安装目录
--sbin-path 指向(执行)程序文件(nginx)
--conf-path= 指向配置文件(nginx.conf)
--error-log-path= 指向错误日志目录
--http-log-path= 指向访问日志
--pid-path= 指向pid文件(nginx.pid)
--lock-path= 指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)
--user= 指定程序运行时的非特权用户
--group= 指定程序运行时的非特权用户组
--with-http_ssl_module 启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)
--with-http_flv_module 启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_sub_module 启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
--with-http_gzip_static_module 启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
--http-client-body-temp-path= 设定http客户端请求临时文件路径
--http-proxy-temp-path= 设定http代理临时文件路径
--http-fastcgi-temp-path= 设定http fastcgi临时文件路径
--http-uwsgi-temp-path= 设定http uwsgi临时文件路径
--http-scgi-temp-path= 设定http scgi临时文件路径
--with-pcre 启用pcre库
[root@localhost nginx-1.8.0]# make && make install
# 配置全局变量方便使用和启动
[root@localhost nginx]# vi /etc/profile
export PATH=/usr/local/nginx/sbin:$PATH
[root@localhost nginx]# source /etc/profile
[root@localhost nginx]# vi /usr/lib/systemd/system/nginx.service
[Unit] //对服务的说明
Description=nginx - high performance web server //描述服务
After=network.target remote-fs.target nss-lookup.target //描述服务类别
[Service] //服务的一些具体运行参数的设置
Type=forking //后台运行的形式
PIDFile=/usr/local/nginx/logs/nginx.pid //PID文件的路径
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf //启动准备
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf //启动命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload //重启命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop //停止命令
ExecQuit=/usr/local/nginx/sbin/nginx -s quit //快速停止
PrivateTmp=true //给服务分配临时空间
[Install]
WantedBy=multi-user.target //服务用户的模式
# 创建一个文件夹用于保存不同网站的配置文件
[root@localhost nginx]# mkdir /usr/local/nginx/conf.d
如果在重启或启动nginx服务时无法启动,报错为80端口被占用
sudo fuser -k 80/tcp #关闭占用80端口的程序
nginx配置文件详解
(1)nginx 的安装目录文件
[root@localhost conf.d]# tree /etc/nginx/
/etc/nginx/
├── conf.d
│ ├── default.conf 主配置文件
│ └── default.conf.bak
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules -> …/…/usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf
(2)主配置文件下详解
Nginx主配置文件nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的,每一个区块以一个大括号“{}”来表示,区块可以分为几个层次,整个配置文件中,Main区位于上层,在Main区下面可以有Events区、HTTP区等层次,在HTTP区中又包含有一个或者多个server区,每个server区中又可有一个或者多个location区。