Nginx的重要特性
支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
资源消耗少:在3万并发连接下,开启10个Nginx线程消耗的内存不到200MB
可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能,这相当
于专业的Haproxy软件或LVS的功能
具备Squid等专业缓存软件等的缓存功能。
支持异步网络I/O事件模型epoll(linux2.6+)
Nginx软件的主要企业功能应用
(1)作为Web服务软件
Nginx是一个支持高性能,高并发的Web服务软件,它具有很多优秀的特性,作为Web服务器,与Apache相比,Nginx能够支持更多的并发连接访问,但占用的资源更少,效率更高,在功能上也强大了很多,几乎不逊色于Apache。
(2)反向代理或负载均衡服务
在反向代理或负载均衡服务方面,Nginx可以作为Web服务,PHP等动态服务及Memcached缓存的代理服务器,它具有类似专业反向代理软件(如Haproxy)的功能,同时也是一个优秀的邮件代理服务软件,但是Nginx的代理功能还是相对简单了些,特别是不支持TCP的代理(Nginx1.9.0版本已经开始支持TCP代理了)
(3)前端业务数据缓存服务
在Web缓存服务方面,Nginx可通过自身的proxy_cache模块实现类Squid等专业缓存软件的功能。
为什么Nginx总体性能比Apache高?
Nginx使用最新的epoll(Linux2.6内核)和kqueue(freebsd)异步网络I/O模型,而Apache使用的是传统的select模型。目前Linux下能够承受高并发访问的Squid,Memcached软件采用的都是epoll模型。
处理大量连接的读写时,Apache所采用的select网络I/O模型比较低效。
静态业务:若是高并发场景,尽量采用Nginx或Lighttpd,二者首选Nginx。
动态业务:理论上采用Nginx和Apache均可,建议选择Nginx,为了避免相同业务的服务软件多样
化,增加额外维护成本。动态业务可以由Nginx兼做前端代理,再根据页面元素的类型或目录,转发到后端相应的服务器进行处理。
既有静态业务又有动态业务:采用Nginx
nginx的编译安装部署
useradd -s /sbin/nologin -M xxx #创建程序用户
tar xf nginx-1.10.2.tar.gz -C /usr/src/ #解压缩
cd /usr/src/nginx-1.10.2
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #预配置
make && make install #编译和安装
ln -s /usr/local/nginx/sbin/ /usr/local/sbin/* #给命令做软连接,以便PATH能找到
/usr/local/nginx/sbin/nginx #启动nginx
主配置文件
Nginx主配置文件nginx.conf是一个纯文本类型的文件(其他配置文件大多也是如此),它位于Nginx安装目录下的conf目录,整个配置文件是以区块的形式组织的。一般,每个区块以一个大括号“{}”来表示,区块可以分为几个层次,整个配置文件中Main区位于最上层,在Main区下面可以有Events区,HTTP区等层级,在HTTP区中又包含有一个或多个Server区,每个Server区中又可有一个或多个location区
egrep -v "#|^$" nginx.conf #去掉包含#号和空行的内容
worker_processes 1; #worker进程的数量
error_log logs/error.log; #错误日志(默认没开)
pid logs/nginx.pid; #进程号(默认没开)
events { #事件区块开始
worker_connections 1024; #每个worker进程支持的最大连接数,一般改204800
} #事件区块结束
http { #http区块开始
include mime.types; #Nginx支持的媒体类型库文件包含
default_type application/octet-stream; #默认的媒体类型
sendfile on; #开启高效传输模式
keepalive_timeout 65; #连接超时。
server { #网站配置区域(第一个server第一个虚拟主机站点)
listen 80; #提供服务的端口,默认80
server_name www.chensiqi.org; #提供服务的域名主机名
location / { #第一个Location区块开始
root html; #站点的根目录(相对于nginx安装路径)
index index.html index.htm; #默认的首页文件,多个用空格分开
}
error_page 500 502 503 504 /50x.html; #出现对应的http状态码时,使用50x.html回应客户
location = /50x.html { #Location区块开始,访问50x.html
root html; #指定对应的站点目录为html
}
}
server { #网站配置区域(第二个server第二个虚拟主机站点)
listen 80; #提供服务的端口,默认80
server_name bbs.chensiqi.org; #提供服务的域名主机名
location / { #服务区块
root html; #相对路径(nginx安装路径)
index index.html index.htm;
}
location = /50x.html { #发生错误访问的页面
root html;
}
}
}
整个nginx配置文件的核心框架如下:
worker_processes 1;
events {
worker_connections 204800;
}
http {
include mime.types;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
Nginx的功能模块说明
例
使用grep过滤命令来生成基础的Nginx主配置文件nginx.conf,然后根据生成的初始配置进行修改,使其成为所需的形式,具体步骤为:
[root@localhost conf]# pwd
/application/nginx/conf
[root@localhost conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
规范化Nginx配置文件
下面是优化后的Nginx配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf; #虚拟网站配置信息统一放在了当前的extra目录下
include extra/mail.conf;
include extra/status.conf;
}
[root@localhost nginx]# tree conf/extra/
conf/extra/
├── mail.conf
├── status.conf
└── www.conf
0 directories, 3 files
[root@localhost nginx]# cat conf/extra/www.conf
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root /var/www/html/wwwcom;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
}
Nginx状态信息功能实战
1、确认编译时是否设定了此模块
root@localhost nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.10.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx-1.10.2/ --with-http_stub_status_module --with-http_ssl_module
##说明
–with-http_stub_status_module模块就是状态信息模块
2、设定信息模块配置
[root@localhost nginx]# cat conf/extra/status.conf
##status
server{
listen 80;
server_name status.yunjisuan.com;
location / {
stub_status on; #开启状态信息功能
access_log off; #不记录访问日志
}
}
##说明
状态信息模块配置方式和搭建虚拟网站类似需要占用一个域名来访问
nginx -t 检查配置文件语法
nginx -s reload 平滑重启nginx
3、Nginx status显示结果详解
[root@localhost nginx]# curl status.yunjisuan.com
Active connections: 2 #表示Nginx正在处理的活动连接2个
server accepts handled requests
39 39 41
Reading: 0 Writing: 1 Waiting: 1
第一个server表示Nginx启动到现在2共处理了39个连接
第二个accepts表示Nginx启动到现在共成功创建了39次握手
请求丢失数=(握手数-连接数),可以看出,本次状态显示没有丢失请求。
第三个handled requests,表示总共处理了41次请求。
Reading为Nginx读取到客户端的Header信息数
Writing为Nginx返回给客户端的Header信息数
Waiting为Nginx已经处理完正在等候下一次请求指令的驻留连接。在开启keep-alive的情况下,这个值等于active - (reading+writing)
增加错误日志
worker_processes 1;
error_log logs/error.log; #非常简单,一般增加此行即可
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/mail.conf;
include extra/status.conf;
}
Nginx访问日志轮询切割
默认情况下Nginx会把所有的访问日志生成到一个指定的访问日志文件access.log里,但这样一来,时间长了就会导致日志个头很大,不利于日志的分析和处理,因此,有必要对Nginx日志,按天或按小时进行切割,使其分成不同的文件保存。
[root@localhost nginx]# cat /server/scripts/cut_nginx_log.sh
#!/bin/bash
#日志切割脚本可挂定时任务,每天00点整执行
Dateformat=`date +%Y%m%d`
Basedir="/usr/local/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
[root@localhost nginx]# cat >>/var/spool/cron/root << KOF
#cut nginx access log by
00 00 * * * /bin/bash /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
Nginx location
算是一种匹配规则。
顺序 | 匹配标识 | 说明 |
---|---|---|
1 | " location = / { " | 精确匹配 |
2 | " location ^~ /images/ { " | 先进行字符串的前缀匹配 |
3 | " loction ~* .(gif\jpg)$ { " | 正则匹配,*为不区分大小写 |
4 | " location /documents/ { " | 匹配常规字符串,模糊匹配 |
5 | " location / { " | 默认匹配 |
Nginx rewrite
Nginx rewrite的主要功能是实现URL地址重写
默认格式:rewrite regex replacement flag;
位置:server 、if、location
###说明
regex:匹配URI的正则表达式
replacement:要跳转的位置
flag标记说明:
permanent:301永久重定向
redirect:302临时跳转
last : 终止在本location块中处理接收到的URI,并将此处重写的URI作为新的URI使用其他location进行处理。(只是终止当前location的处理)
break : 将此处重写的URI作为一个新的URI在当前location中继续执行,并不会将新的URI转向其他location。
Nginx访问认证
这种使用账号密码才可以访问网站的功能主要应用在企业内部人员访问的地址上,例如:企业网站后台,MySQL客户端phpmyadmin,企业内部的CRM,WIKI网站平台
1、利用htpasswd创建密码文件
htpasswd -bc /usr/local/nginx/conf/htpasswd QWER 123123
Adding password for user QWER
cat /usr/local/nginx/conf/htpasswd
QWER:FC1/eEc/iK0Mo #账号密码是加密的(htpasswd是文件的名字)
2、在虚拟主机配置文件里加入两条配置信息
[root@localhost html]# cat /usr/local/nginx/conf/extra/blog.conf
server {
listen 80;
server_name blog.baidu.com;
location / {
root /var/www/html/blogcom;
index index.html index.htm;
auth_basic "QWER training"; #加入这条配置
auth_basic_user_file /usr/local/nginx/conf/htpasswd; #加入这条配置
}
}
#配置解释:
auth_basic :验证的基本信息选项(后边跟着的双引号里就是验证窗口的名字)
auth_basic_user_file :验证的用户文件(后边根账号密码文件的绝对路径)
完事
Tengine和Nginx是什么关系?
Tengine是淘宝开源Nginx的分支,官方站点为http://tengine.taobao.org/
访问Nginx时出现状态码“403 forbidden”的原因
(1)Nginx配置文件里没有配置默认首页参数,或者首页文件在站点目录下没有如下内容:
index index.php index.html index.htm;
(2)站点目录或内部的程序文件没有Nginx用户访问权限
(3)Nginx配置文件中设置了allow,deny等权限控制,导致客户端没有访问权限。
知识点回顾
Nginx的特性优点
主流Web动态静态性能对比
Apache select 和Nginx epoll 模型的区别(面试常考)
虚拟主机概念及类型分类详解
基于域名和端口虚拟主机的介绍及搭建
Nginx错误,访问日志,以及访问日志切割
Nginx访问状态信息
Nginx location
Nginx rewrite
Nginx Web访问认证