目录
一、nginx介绍
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件代理服务器,并在一个BSD-like协议下发行。由俄罗斯的程序设计师lgor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
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具有很高的稳定性;支持热部署;代码质量非常高,代码很规范,手法成熟,模块扩展也很容易;采用了一些os提供的最新特性如对sendfile(Linux2.2+),accept-filter(FreeBSD4.1+),TCP_DEFER_ACCEPT(Linux 2.4+)的支持,从而大大提高了性能。
三、Nginx模块单元介绍
3.1、ngx_http_access_module模块:实现基于ip的访问控制功能
3.2、ngx_http_auth_basic_module模块:实现基于用户的访问控制,使用basic机制进行用户认证
3.3、ngx_http_stub_status_module模块:用于输出nginx的基本状态信息
3.4、ngx_http_log_module模块:用指定的格式写入请求日志
3.5、ngx_http_gzip_module模块:用gzip算法来压缩数据可以节约带宽,默认nginx不压缩数据,但是压缩会消耗CPU资源,且压缩文本图像类效果较好,能达到30%左右,但压缩音频视频没有多大意义,因为本身音视频就是被压缩过的,很可能对音视频压缩反而会增大其体积
3.6、ngx_http_ssl_module模块:启用https时使用的模块
3.7、ngx_http_rewrite_module模块:重定向模块,可以将客户端的请求基于regex所描述的模式进行检查,而后完成替换。当旧的业务和新的业务不一样,网站更名了,就可以使用此模块。将访问旧的请求重定向成新的
3.8、ngx_http_referer_module模块:可以跟踪链接从哪里跳转过来的,该字段可以用来防止盗链
3.9、ngx_http_headers_module模块:向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值
四、不同版本的Nginx区别
1、Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
2、Stable version:最新稳定版,生产环境上建议使用的版本
3、Legacy versions:遗留的老版本的稳定版
五、Nginx的优化安装
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar zxf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module ###统计状态模块
[root@localhost nginx-1.12.2]# make && make install
[root@localhost ~]# useradd -M -s /sbin/nologin nginx ###创建一个不可登录的程序用户
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin ###优化执行路径
[root@localhost ~]# nginx ###启动服务
[root@localhost ~]# netstat -anpt | grep nginx ###查看nginx服务是否开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16429/nginx: master
[root@localhost ~]# killall -s QUIT nginx ###选项-s QUIT等于-3 停止服务
[root@localhost ~]# netstat -anpt | grep nginx
[root@localhost ~]# nginx
[root@localhost ~]# killall -s HUP nginx ###选项-s HUP等于-1 重新加载
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16488/nginx: master
[root@localhost ~]# vi /etc/init.d/nginx ###制作管理脚本
#!/bin/bash
#chkconfig: 35 20 80
#description: nginx server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
killall -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
killall -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|reload|status}"
exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
六、nginx的访问状态及验证
6.1、Nginx的访问状态统计
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
user nginx nginx ###修改#user nobody为user nginx nginx
error_log logs/error.log info ###去除#号
events {
use epoll; ###新增此行,默认使用select/poll
worker_connections 1024; ###表示一个工作进程允许1024个连接
}
location ~ /status { ###配置统计功能
stub_status on;
access_log off;
} ###在server模块里的error_page上面增加
[root@localhost ~]# 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 ~]# nginx -V ###查看版本号及开启的模块
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost ~]# systemctl start nginx ###开启nginx服务
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0 ###关闭防火墙和核心防护
6.2、nginx的验证功能
[root@localhost ~]# yum -y install httpd-tools
[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db lisi ###创建访问用户
New password:
Re-type new password:
Adding password for user lisi
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
allow 20.0.0.13/24; ###允许本机访问
deny all; ###拒绝所有
auth_basic "secret"; ###验证方式为密码验证
auth_basic_user_file /usr/local/nginx/passwd.db; ###密码所在文件
}
[root@localhost ~]# nginx -t ###验证配置文件是否有错
nginx: [warn] low address bits of 20.0.0.13/24 are meaningless in /usr/local/nginx/conf/nginx.conf:48
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 ~]# systemctl restart nginx ###重启服务
七、配置Nginx虚拟主机
7.1、基于域名
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.aa.com;
charset utf-8
location / {
root /var/www/aa;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.ab.com;
charset utf-8;
location / {
root /var/www/ab;
index index.html index.htm;
}
}
[root@localhost ~]# 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 ~]# systemctl restart nginx ###重启nginx服务
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vi /etc/hosts ###增加映射
20.0.0.13 www.aa.com www.ab.com
6.1.2、准备测试页
[root@localhost ~]# mkdir -p /var/www/aa
[root@localhost ~]# mkdir -p /var/www/ab
[root@localhost aa]# vi index.html
<html><body><h1>This is test1!</h1></body></html>
[root@localhost ab]# vi index.html
<html><body><h1>This is test2!</h1></body></html>
7.2、基于IP
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.13:80; ###修改IP地址
server_name www.aa.com;
charset utf-8;
#access_log logs/aa.access.log main;
location / {
root /var/www/aa;
index index.html index.htm;
}
}
server {
listen 20.0.0.135:80; ###增加的一张新网卡
server_name www.aa.com;
charset utf-8;
#access_log logs/aa.access.log main;
location / {
root /var/www/aa;
index index.html index.htm;
}
}
[root@www ~]# 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@www ~]# systemctl restart nginx
[root@www ~]# vi /etc/hosts
20.0.0.13 www.aa.com
20.0.0.135 www.aa.com
7.3、基于端口
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.13:80; ###更改端口号
server_name www.aa.com;
charset utf-8;
#access_log logs/aa.access.log main;
location / {
root /var/www/aa;
index index.html index.htm;
}
}
server {
listen 20.0.0.13:8080; ###更改端口号
server_name www.aa.com;
charset utf-8;
#access_log logs/aa.access.log main;
location / {
root /var/www/aa;
index index.html index.htm;
}
}
[root@www ~]# systemctl restart nginx