nginx的基本特性html
它所具有的其余www服务特性java
面试时可能须要解答以下nginxHTTP服务器的特点及优势nginx
apache软件的特色web
处理静态小文件(小于1Mb),nginx比Apache和Lighttpd更有优点,处理动态文件时Apache更有优点,可是差距不大。这是由于处理动态数据的能力取决于PHP(java)和后端数据库的服务能力,也就是说瓶颈不在web服务器上,通常状况下普通的PHP引擎支持的并发链接参考值为300~1000,java引擎和数据库的并发链接参考值为300~1500。面试
为何nginx整体性能比Apache高正则表达式
nginx使用最新的epoll和kqueue(freebsb)异步网络I/O模型,而Apache使用的是传统的select模型数据库
异步的安全性、稳定性没有同步高,中间容易被人窃取,用户收不到数据apache
select和epoll的释义:vim
select模型就像保姆照看一群孩子,会询问每一个孩子是否要尿尿,有就带领去(尿尿比做网络I/O时间,时间复杂度是O(n))windows
epoll就是孩子要尿尿,本身主动去规定好的地方,而后保姆带领去,这样保姆只须要关心那个地方有没有孩子 (时间复杂度O(1))
指标 |
select |
epoll |
性能 |
随着链接数的增长而急剧降低。处理成千上万的并发链接数时,性能不好。 |
随着链接数的增长,性能基本没有降低,处理成千上万的并发链接数时,性能好 |
链接数 |
链接数 有限制,处理最大链接数不超过1024.若是超过1024那么就要修改FD_SETSIZE宏,并从新编译。 |
链接数无限制 |
内在处理机制 |
性能轮询 |
回调callback |
开发复杂性 |
低 |
中 |
表1 Apache select和nginx epoll技术对比
静态业务:
如果高并发场景,尽可能采用nginx或Lighttpd,首选nginx
动态业务:
理论上采用nginx和Apache都可,为了不相同业务服务软件多样化,增长维护成本
既有动态又有静态:
选用nginx
安装nginx有3种方法:
更换国内的yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
安装阿里云的epol源
一、备份(若有配置其余epel源)
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
二、下载新repo 到/etc/yum.repos.d/
epel(RHEL 7)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
epel(RHEL 6)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
epel(RHEL 5)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-5.rep
配置安装nginx的yum源
cd /etc/yum.repos.d/
vim nginx.repo,填写以下内容:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/6/$basearch/ gpgcheck=0 enabled=1
1.安装nginx须要的pcre库
yum install pcre pcre-devel -y
安装pcre库是为了是nginx支持具有URI重写功能的rewrite模块,不安装那就没法使用rewrite模块的功能。基本上rewrite功能是企业必须的。
2. 安装openssl-devel
[root@web01 nginx]#yum -y install openssl openssl-devel
3. 开始安装nginx
[root@web01 nginx]#wget http://nginx.org/download/nginx-1.6.3.tar.gz [root@web01 nginx]#tar xf nginx-1.6.3.tar.gz [root@web01 nginx]#useradd nginx -M -s /sbin/nologin [root@web01 nginx]#cd nginx-1.6.3 [root@web01 nginx]#./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module [root@web01 nginx]#make [root@web01 nginx]#make install [root@web01 nginx]#ls /application/nginx-1.6.3/ conf html logs sbin [root@web01 nginx]#ln -s /application/nginx-1.6.3/ /application/nginx [root@web01 nginx]#/application/nginx/sbin/nginx
1. 检查语法
[root@web01 nginx]#/application/nginx/sbin/nginx -t
2. 启动nginx服务
[root@web01 nginx]#/application/nginx/sbin/nginx
3. 查看服务对应的端口是否成功开启
[root@web01 nginx]#netstat -tunlp|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7377/nginx [root@web01 nginx]# ps -ef|grep nginx root 7377 1 0 17:38 ? 00:00:00 nginx: master process www 7378 7377 0 17:38 ? 00:00:00 nginx: worker process /application/nginx/sbin/nginx root 7382 4710 0 17:39 pts/0 00:00:00 grep nginx [root@web01 nginx]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 7377 root 6u IPv4 22767 0t0 TCP *:http (LISTEN) nginx 7378 www 6u IPv4 22767 0t0 TCP *:http (LISTEN)
4. 结果
(1)用浏览器输入10.0.0.8看到下图就成功了
(2)wget 10.0.0.8
(3)curl -I http://nginx.org/
补充
/application/nginx/sbin/nginx -V #显示编译参数
/application/nginx/sbin/nginx -h #man帮助
/application/nginx/sbin/nginx -t #检查语法
编译安装目录结构
[root@localhost ~]# tree /application/nginx-1.6.3/ /usr/local/nginx ├── client_body_temp ├── conf # Nginx全部配置文件的目录 │ ├── fastcgi.conf # fastcgi相关参数的配置文件 │ ├── fastcgi.conf.default # fastcgi.conf的原始备份文件 │ ├── fastcgi_params # fastcgi的参数文件 │ ├── fastcgi_params.default # 全部结尾为default的文件都是备份文件 │ ├── koi-utf │ ├── koi-win │ ├── mime.types # 媒体类型 │ ├── mime.types.default │ ├── nginx.conf # Nginx主配置文件 │ ├── nginx.conf.default │ ├── scgi_params # scgi相关参数文件 │ ├── scgi_params.default │ ├── uwsgi_params # uwsgi相关参数文件 │ ├── uwsgi_params.default │ └── win-utf ├── fastcgi_temp # fastcgi临时数据目录 ├── html # Nginx默认站点目录 │ ├── 50x.html #错误页面优雅替代显示文件,例如当出现502错误时会调用此页面 │ └── index.html # 默认的首页文件 ├── logs # Nginx日志目录 │ ├── access.log # 访问日志文件 │ ├── error.log # 错误日志文件 │ └── nginx.pid # pid文件,Nginx进程启动后,会把全部进程的ID号写到此文件 ├── proxy_temp # 临时目录 ├── sbin # Nginx命令目录 │ └── nginx # Nginx的启动命令 ├── scgi_temp # 临时目录 └── uwsgi_temp # 临时目录
yum安装的nginx的目录结构:
[root@10.0.0.20 ~]#rpm -qa nginx nginx-1.12.2-1.el6.ngx.x86_64 [root@10.0.0.20 ~]#rpm -ql nginx-1.12.2-1.el6.ngx.x86_64
编译安装nginx软件时。可使用./configure--help查看相关参数帮助。
--prefix #设置安装路径
--user=USER #进程用户权限
--group=GROUP #进程用户组权限
--with-http_stub_status_module #激活状态信息
--with-http_ssl_module #激活ssl功能
Nginx http 功能模块 模块说明
ngx_http_core_module 包括一些核心的http参数配置对应的配置为http区块部分
ngx_http_access_module 访问控制模块,用来控制网站用户对Nginx的访问
ngx_http_gzip_module 压缩模块,对Nginx返回的数据压缩,属于性能优化模块
ngx_http_fastcgi_module FastCGI模块,和动态应用相关的模块,例如PHP
ngx_http_proxy_module proxy 代理模块
ngx_http_upstream_module 负载均衡模块,实现网站的负载均衡功能及节点的健康检查
ngx_http_rewrite_module URL地址重写模块
ngx_http_limit_conn_module 限制用户并发链接数及请求数模块
ngx_http_limit_req_module 根据定义的key限制Nginx请求过程的速率
ngx_http_log_module 访问日志模块,指定的格式记录Nginx客户访问日志等信息
ngx_http_auth_basic_module web 认证模块,设置web用户经过帐号、密码访问Nginx
ngx_http_ssl_module ssl模块,用于加密的http链接,如https
ngx_http_stub_status_module 记录Nginx基本访问状态信息等的模块
[root@web01 conf]# egrep -v '#|^$' nginx.conf worker_processes 1; #work进程的数量 events { #事件区块开始 worker_connections 1024; #每一个worker进程支持的最大链接数 } #事件区块结束 http { #http区块开始 include mime.types; #nginx支持的媒体类型库文件 default_type application/octet-stream; #默认的媒体类型 sendfile on; #开启高效的传输模式 keepalive_timeout 65; #链接超时 include path/*.conf; #若是server标签过多能够摘出去,到任何路径下 server { #第一个server区块开始,表示一个独立的虚拟主机站点 listen 80; #提供服务的端口,默认是80 server_name localhost; #提供服务的域名主机名 location / { #第一个location区块开始 root html; #站点的根目录,至关于nginx安装目录 index index.html index.htm; #默认的首页文件,若是没有首页文件报403,报403的除了添加首页文件的另外一个解决方法,在这放置autoindex on } #第一个location区块结束 error_page 500 502 503 504 /50x.html; #出现对应的http状态码时,使用50x.html回应客户 location = /50x.html { #location区块开始,访问50x.html root html; #指定站点目录html } } } #http区块结束
所谓的虚拟主机,在web访问里就是一个独立的网站站点,这个站点对应独立的域名(也多是IP或端口),具备独立的程序和目资源录,能够独立的对外提供服务供用户访问。
这个独立的站点在配置里由必定的格式的标签段标记的。在nginx软件里是使用一个server{}标签来标识一个虚拟主机,一个web服务里能够有多个虚拟主机,便可以支持多个虚拟主机站点。
企业外网场景,经过不一样的域名区分不一样的虚拟主机,企业应用最广的类型
1.配置基于域名的nginx.conf内容
[root@web01 conf]# egrep -v '#|^$' nginx.conf.default >nginx.conf [root@web01 conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.etiantian.org; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
2.建立域名对应的站点目录即文件
mkdir -p /application/nginx/html/www -p echo http://www.etiantian.org >/application/nginx/html/www/index.html
3.检查语法从新加载
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
4.最后测试配置的访问结果
echo “10.0.0.8 www.etiantian.org” >>/etc/hosts
注意:
在windows客户端进行访问,若是域名没有作正是DNS解析,可在笔记本的hosts文件添加记录
公司后台,测试场景,内部服务
这类虚拟主机主要是针对企业内部网站,一些不但愿直接对外提供用户访问的网站后台
注意:
通常是先判断端口号,而后判断域名,若是端口对应上了,域名没有找见默认去找这个端口下的第一台主机(也就是第一个server标签)
[root@web01 conf]# cat nginx.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } } server { listen 81; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } } server { listen 82; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } } }
检查
重启
访问
http://www.etiantian.org:80
http://bbs.etiantian.org:81
http://blog.etiantian.org:82
网卡上增长IP
法一:
ip addr add 10.0.0.3/24 dev eth0 label eth0:3 ip addr add 10.0.0.4/24 dev eth0 label eth0:4
法二:
ifconfig eth0:0 10.0.0.101/24 up
查看结果:
[root@web01 conf]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:98:47:1B inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe98:471b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5611 errors:0 dropped:0 overruns:0 frame:0 TX packets:3963 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2142583 (2.0 MiB) TX bytes:579494 (565.9 KiB) eth0:3 Link encap:Ethernet HWaddr 00:0C:29:98:47:1B inet addr:10.0.0.3 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:4 Link encap:Ethernet HWaddr 00:0C:29:98:47:1B inet addr:10.0.0.4 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[root@web01 conf]# cat nginx.conf.base_port
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 10.0.0.8:80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } } server { listen 10.0.0.3:81; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } } server { listen 10.0.0.4:82; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } } }
也就是新添加一个server标签打开stub_status on;,编译安装nginx的时候的参数--with-http_stub_status_module
[root@web01 extra]# cat extra/status.conf server { listen 80; server_name status.etiantian.org; location / { stub_status on; access_log off; } }
检查语法,重启服务,配置hosts解析
第一个server表示nginx启动到如今共处理了多少个链接;
第二个accepts表示启动到如今共成功建立了多少多少次握手
第三个handled requests表示处理了多少次请求
reading为nginx读取到客户端的header信息数
writing为nginx返回给客户端的header信息数
waiting为nginx已经处理完正在等候下一次请求指令的驻留链接
[root@web01 conf]# cat >>/xxx/xxx/nginx/conf/extra/02_blog.conf<<EOF server { listen 80; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } access_log logs/access.log [format buffer=size [flush=time]] main; }
EOF
注意
1. 这行配置中的main是http标签中配置的log_format后面的main对应的格式,能够多个格式,方便使用.
2. 配置行的[]括号内的选项是可选的,配置缓存,防止高并发的大量IO
3. format gzip[=level]
错误日志级别
default:error_log logs/error.log level;
关键配置 日志文件 错误日志级别[debug|info|warn|error|alert|notice|crit|emerg]
生产场景通常用warn|error|crit三个级别
标签端的配置
main,http,server,location
日志格式
http { 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 logs/access.log main; include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/*.conf; }
nginx日志变量 |
说明 |
$remote_addr |
记录访问网站客户端地址 |
$remote_user |
远程客户端用户名称 |
$time_local |
访问时间和时区 |
$request |
用户的http请求的起始行信息 |
$status |
http状态码,记录请求返回的状态 |
$body_bytes_sent |
服务器发送给客户端的响应body字节数 |
$http_referer |
此连接是从哪一个链接访问过来的 |
$http_user_agent |
记录客户端的访问信息 |
$http_x_forwarded_for |
当前有代理服务器时,设置了web节点机理客户端地址的配置 |
例:
$remote_addr 对应的10.0.0.20,客户端的IP
$remote_user 对应的是第二个中杠,没有远程用户用-填充
$time_local 对应的时间
$request 对应的是GET/HTTP/1.0
$status304 状态码
$body_bytes_sent 对应的是0
$http_referer 这里是-,直接打开的域名浏览,因此没有值
$http_user_agent 那一长串的东西
$http_x_forwarded_for 这里是10.0.0.1
[root@web01 logs]# mv access.log $(date +%F -d "-1day")_access.log
[root@web01 logs]# /application/nginx/sbin/nginx -s reload
从新启动就会从新生成access_www.log文件,而后写定时任务。
[root@web01 scripts]# crontab -l
################time sync by oldboy at 2010-2-1
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
################
00 00 * * * /bin/sh /server/scripts/cut_log.sh >/dev/null 2>&1
cat cut_log.sh cd /application/nginx/logs &&\ mv access.log $(date +%F -d "-1day")_access.log /application/nginx/sbin/nginx -s reload
Nginx经常使用日志收集及分析工具备rsyslog、awstats、flume、ELK(Elasticsearch logstash Kibana)、storm等。
做用:是根据用户请求的URI来执行不一样的应用
语法:location { = | ~ | ~* | ^~ } uri {
…
}
注意: = 精确匹配,~区分大小写,~*不区分大小写,!匹配取反,^~在常规字符串检查以后,不作正则表达式的检查
URI是关键部分,能够是普通字符串地址路径,或者是正则表达式
location |
{ = | ~ | ~* | ^~ } |
uri |
{…} |
指令 |
匹配标识 |
匹配网站地址 |
配置URI后要执行的配置端 |
location示例:
[root@web01 extra]# cat 02_blog.conf server { listen 80; server_name blog.etiantian.org; location / { return 401; } location = / { return 402; } location /documents/ { return 403; } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; } }
检查语法
重启生效
实验返回状态码:
curl -s -o /dev/null -I -w "%{http_code}\n" http://blog.etiantian.org/
curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/images/
参数解释:-s静默,-o不输出,-I请求头,-w "%{http_code}\n"状态码
顺序 |
不用URI即特殊字符匹配 |
匹配说明 |
1 |
“location = / { ” |
精确匹配 |
2 |
“location ^~ /images/ { ” |
匹配常规字符串,不作正则匹配检查 |
3 |
“location ~* \.(gif/jpg/jpeg)$ { ” |
正则匹配 |
4 |
“location /documents/ { ” |
路径匹配 |
5 |
“location / { ” |
全部location都不能匹配后的默认匹配 |
表2 URI及特殊字符组合匹配的顺序说明
和Apache等的web服务软件同样,nginx rewrite的主要功能也是实现URL地址重写,nginx的rewrite规则须要PCRE软件的支持,经过Perl兼容正则表达式语法进行规则匹配。
rewrite指令语法:
rewrite regex replacement [flag]
默认值:none 应用位置:server、location、if
例子:rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
server标签下:
直接rewrite ^/(.*) http://www.etiantian.org/$1permanent;
if匹配:
if ( $http_host ~* "^(.*)\.etiantian\.org$") { set $domain $1; rewrite ^(.*) http://www.etiantian.org/$domain/oldboy.html break; }
字符 |
描述 |
\ |
将后面的字符标记为一个特殊字符或一个后项引用,例:\\和\n匹配换行符 |
^ |
匹配输入字符串的起始位置 |
$ |
匹配输入字符串的结束位置 |
* |
匹配前面字符0次或屡次,例:ol*匹配“o”或者“olllll” |
+ |
匹配前面字符1次或屡次,例:ol*匹配“ol”或者“olllll” |
? |
匹配前面字符0次或1次 |
. |
匹配除‘\n’以外的任何单个字符 |
(pattern) |
匹配括号内得pattern,而且在后面获取对应的匹配,会后项经过$1~n引用 |
flag标记符号 |
说明 |
last |
本条规则匹配完成后,继续向下匹配新的location URI规则 |
break |
本条规则匹配完成即终止,再也不匹配后面的任何规则 |
redirect |
返回302临时重定向,浏览器地址栏会显示跳转后的URL地址 |
permanent |
返回301永久重定向,浏览器地址栏会显示跳转后的URL地址 |
rewrite ^(.*)/bbs/ h有关rewrite特殊flag标记last与break的说明:
在根location中或者server标签中编写rewrite规则,建议使用last标记,而在普通的location或if{}中编写rewrite规则,则建议使用break标记。
注意:
示例:
location / { auth_basic "oldboy training"; #这是个提示 auth_basic_user_file /application/nginx/conf/htpasswd; #认证读取的文件 }
建立帐号密码:此帐号就是登录时须要的
[root@web01 extra]# rpm -qf /usr/bin/htpasswd httpd-tools-2.2.15-53.el6.centos.x86_64 [root@web01 extra]# htpasswd -cb /application/nginx/conf/htpasswd oldboy 123456 Adding password for user oldboy [root@web01 extra]# cat /application/nginx/conf/htpasswd oldboy:hkWTQUKYkqLSo
具体操做:
htpasswd -bc /application/nginx/conf/htpasswd oldboy 123456
chmod 400 /application/nginx/conf/htpasswd
chown nginx /application/nginx/conf/htpasswd
检查语法
重启生效
更多经常使用开源运维工具见http://oldboy.blog.51cto.com/2561410/775056。