概述:Nginx是一款由俄罗斯开发的开源的高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3/SMTP代理服务,其性能优点着为显著,官网上称:单台nginx服务器能够处理50000并发;javascript
特色:高性能、稳定、消耗硬件资源小、可以处理大并发,主要用于静态的解析,动静页面的分离;css
功能:html
1.做为Web服务器,nginx处理静态文件、索引文件以及自动索引效率很是高。前端
2.做为代理服务器,Nginx能够实现无缓存的反向代理加速,提升网站运行速度。java
3.做为负载均衡服务器,Nginx既能够在内部直接支持Rails和PHP,也能够支持HTTP代理服务器,对外进行服务。同时支持简单的容错和利用算法进行负载均衡。node
优点:linux
1.在性能方面,Nginx在实现上很是注重效率。它采用内核Poll模型,能够支持更多的并发链接,最大能够支持对50 000个并发链接数的响应,并且占用很低的内存资源。nginx
2.在稳定性方面,Nginx采起了分阶段资源分配技术,使得对CPU与内存的占用率很是低。Nginx官方表示Nginx保持10 000个没有活动的链接,这些链接只占2.5M内存,所以,相似DOS这样的攻击对Nginx来讲基本上是没有任何做用的。web
3.在高可用性方面,Nginx支持热部署,启动速度特别迅速,所以能够在不间断服务的状况下,对软件版本或者配置进行升级,即便运行数月也无需从新启动,几乎能够作到7*24小时的不间断运行。正则表达式
核心模块:HTTP模块、EVENT事件模块、MAIL模块。
基础模块:HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块、HTTP Rewrite模块
第三方模块:HTTP Upstream Request Hash模块、Notice模块、HTTP Access Key模块。
Handlers:处理器模块,此类模块直接处理请求,并进行输出内容和修改headers信息等操做。Handlers处理器模块通常只能有一个。
Filters:过滤器模块,此类模块主要对其余处理器模块输出的内容进行修改操做,最后由Nginx输出。
Proxies:代理类模块,此类模块是Nginx的HTTP Upstream之类的模块,这些模块主要与后端一些服务好比FastCGI等进行交互,实现服务代理和负载均衡等功能。
单工做进程模式:除主进程外,还有一个工做进程,工做进程是单线程的,默认为此模式;
多工做进程模式:每一个工做进程包含多个线程;
master进程:
1.接收外界传递给Nginx的信号,进而管理服务的状态等;
2.管理worker进程,向各worker进程发送信号,监控worker进程的运行状态,当worker进程异常状况下退出后,会自动从新启动新的worker进程;
3.master进程充当整个进程组与用户的交互接口,同时对进程进行监护。它不须要处理网络事件,不负责业务的执行,只会经过管理worker进程来实现重启服务、平滑升级、更换日志文件、配置文件实时生效等功能。
worker进程:
1.处理基本的网络事件,多个worker进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。
2.一个请求,只可能在一个worker进程中处理,一个worker进程,不可能处理其它进程的请求。
3.worker进程的个数是能够设置的,通常咱们会设置与机器cpu核心数量一致。
扩展:
http://blog.csdn.net/hguisu/article/details/8930668 ##nginx实现原理
1.每一个链接对应一个描述。select模型受限于 FD_SETSIZE(即进程最大打开的描述符数),linux2.6.35为1024,实际上linux每一个进程所能打开描数字的个数仅受限于内存大小,然而在设计select的系统调用时,倒是参考FD_SETSIZE的值。可经过从新编译内核更改此值,但不能根治此问题,对于百万级的用户链接请求即使增长相应进程数,仍显得杯水车薪;
2.select每次请求都会扫描一个文件描述符的集合,这个集合的大小是做为select第一个参数传入的值。可是每一个进程所能打开文件描述符如果增长了,扫描的效率也将减少;
3.内核到用户空间,采用内存复制方式传递信息,这样就增长了没必要要的复制延迟;
1.请求无文件描述字大小限制,仅与内存大小相关;
2.epoll返回时已经明确的知道哪一个socket fd发生了什么事件,不用像select那样再一个个比对;
3.内核到用户空间,采用共享内存方式传递消息,使用mmap加速内核与用户空间的消息传递;
Apache:Apache 2.2.9以前只支持select模型,2.2.9以后支持epoll模型;
Nginx:支持epoll模型;
案例环境:
系统类型 | IP地址 | 主机名 | 所需软件 | 硬件 |
Centos 6.5 64bit | 192.168.100.150 | www.linuxfan.cn | nginx-1.12.2.tar.gz | 内存:2G CPU:8核 |
案例步骤:
安装nginx程序;
优化nginx服务并启动服务;
客户端访问测试;
开启nginx的状态监听模块;
客户端访问nginx的状态监听界面;
企业级优化Nginx服务;
访问测试优化后nginx服务;
安装webbench压力测试工具,进行测试nginx性能;
自主学习:Nginx服务器内核优化;
[root@www ~]# rpm -e httpd --nodeps [root@www ~]# yum -y install pcre-devel zlib-devel [root@www ~]# useradd -M -s /sbin/nologin nginx [root@www ~]# tar zxvf nginx-1.12.2.tar.gz -C /usr/src/ [root@www ~]# cd /usr/src/nginx-1.12.2/ [root@www nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module 注解: --prefix=/usr/local/nginx ##指定安装位置 --user=nginx --group=nginx ##指定运行服务的用户和组 --with-http_stub_status_module ##开启状态监听模块 --conf-path= ##指向配置文件存放位置 --error-log-path= ##指向错误日志存放位置 --pid-path= ##指向pid文件存放位置 --with-rtsig_module ##启用rtsig模块支持(实时信号) --with-select_module ##启用select模块支持(一种轮询模式,不推荐在高载环境下使用)禁用:--without-select_module --with-http_ssl_module ##启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl) --with-http_xslt_module ##启用ngx_http_xslt_module支持(过滤转换XML请求) --with-http_image_filter_module ##启用ngx_http_image_filter_module支持(传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用,要用到gd库) --with-http_gzip_static_module ##启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流) --with-http_degradation_module ##启用ngx_http_degradation_module支持(容许在内存不足的状况下返回204或444码) --without-http_access_module ##禁用ngx_http_access_module支持(该模块提供了一个简单的基于主机的访问控制,容许或拒绝基于ip地址) --without-http_auth_basic_module ##禁用ngx_http_auth_basic_module(该模块是可使用用户名和密码基于http基本认证方法,来保护你的站点或其部份内容) ---without-http_rewrite_module ##禁用ngx_http_rewrite_module支持(该模块容许使用正则表达式改变URL) --without-http_fastcgi_module ##禁用ngx_http_fastcgi_module支持(该模块容许Nginx 与FastCGI 进程交互,并经过传递参数来控制FastCGI 进程工做。) [root@www nginx-1.12.2]# make &&make install [root@www nginx-1.12.2]# ls /usr/local/nginx/ client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@www ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##优化命令执行路径 [root@www ~]# vi /etc/init.d/nginx #!/bin/bash # chkconfig: - 99 20 # description: Nginx Server Control Script NP="/usr/local/nginx/sbin/nginx" NPF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $NP; if [ $? -eq 0 ] then echo "nginx is starting!! " fi ;; stop) kill -s QUIT $(cat $NPF) if [ $? -eq 0 ] then echo "nginx is stopping!! " fi ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $NPF) if [ $? -eq 0 ] then echo "nginx config file is reload! " fi ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0 [root@www ~]# chmod +x /etc/init.d/nginx [root@www ~]# chkconfig --add nginx [root@www ~]# chkconfig nginx on [root@www ~]# /etc/init.d/nginx start nginx is starting!! [root@www ~]# netstat -utpln |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3713/nginx
客户端访问测试
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf ##编辑配置文件在server中添加以下行: 47 location /status { 48 stub_status on; 49 access_log off; 50 } [root@www ~]# /etc/init.d/nginx restart nginx is stopping!! nginx is starting!!
客户端访问nginx的状态监听界面
http://192.168.100.150/status
活动的链接数
已处理的链接数 成功的tcp握手次数 已处理的请求数
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 204800; events { use epoll; worker_connections 204800; } http { include mime.types; default_type application/octet-stream; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 2k; large_client_header_buffers 4 4k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 4k; fastcgi_buffers 8 4k; fastcgi_busy_buffers_size 8k; fastcgi_temp_file_write_size 8k; fastcgi_cache TEST; fastcgi_cache_valid 200 302 1h; fastcgi_cache_valid 301 1d; fastcgi_cache_valid any 1m; fastcgi_cache_min_uses 1; fastcgi_cache_use_stale error timeout invalid_header http_500; open_file_cache max=204800 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; server { listen 80; server_name www.linuxfan.cn; location / { root /usr/local/nginx/html/; index index.html index.htm; } location /status { stub_status on; access_log off; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { expires 30d; } access_log /usr/local/nginx/logs/access.log access; } }
注解: worker_processes 8; ##设置worker进程数量 worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; ##设置每一个worker进程对应一个cpu的核心 error_log /usr/local/nginx/logs/nginx_error.log crit; ##指定错误日志 pid /usr/local/nginx/logs/nginx.pid; ##指定运行时产生的pid文件 worker_rlimit_nofile 204800; ##指定nginx进程最多可以打开多少个文件描述符,一般与系统中的ulimit -n保持一致; events { ##事件区域配置 use epoll; ##指定处理模型为epoll worker_connections 204800; ##每一个进程最多可以处理多少个链接 } http { ##http服务配置区域 include mime.types; ##指定文件扩展名和文件类型映射表 default_type application/octet-stream; ##指定文件类型 charset utf-8; ##指定字符集 server_names_hash_bucket_size 128; ##服务器名字的hash表大小 client_header_buffer_size 2k; ##客户端请求头部buffer大小 large_client_header_buffers 4 4k; ##指定客户端请求中较大的消息头的缓存数量和大小 client_max_body_size 8m; ##指定客户端请求的单个文件的最大字节数 sendfile on; ##开启高效传输模式 tcp_nopush on; ##防止网络阻塞 keepalive_timeout 60; ##客户端链接超时时间 #FastCGI相关参数是为了改善网站的性能:减小资源占用,提升访问速度。 fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 ##配置fastcgi缓存路径和目录结构等级 keys_zone=TEST:10m inactive=5m; ##关键字区域存储时间和非活动删除时间 fastcgi_connect_timeout 300; ##链接到后端FastCGI的超时时间 fastcgi_send_timeout 300; ##向FastCGI传送请求的超时时间 fastcgi_read_timeout 300; ##接收FastCGI应答的超时时间 fastcgi_buffer_size 4k; ##指定读取FastCGI应答第一部分须要多大的缓冲区 fastcgi_buffers 8 4k; ##指定本地须要用多少和多大的缓冲区来缓冲FastCGI的应答请求 fastcgi_busy_buffers_size 8k; ##一般为fastcgi_buffer_size大小的两倍 fastcgi_temp_file_write_size 8k; ##写入缓存文件时使用多大的数据块,大小同上 fastcgi_cache TEST; ##开启Fastcgi的缓存而且为其指定一个名称 fastcgi_cache_valid 200 302 1h; ##指定不一样的状态码,其缓存的时间 fastcgi_cache_valid 301 1d; fastcgi_cache_valid any 1m; fastcgi_cache_min_uses 1; ##URL通过被访问多少次将被缓存 fastcgi_cache_use_stale error timeout invalid_header http_500; ##指定什么状况下不进行缓存 open_file_cache max=204800 inactive=20s; ##指定缓存文件最大数量,通过多长时间文件没有被请求后则删除缓存, open_file_cache_min_uses 1; ##指令中的inactive参数时间内文件的最少使用次数,若是超过这个数字,文件更改信息一直是在缓存中打开的; open_file_cache_valid 30s; ##指定多长时间检查一次缓存的有效信息,检查该缓存的源文件是否发生变化修改等; tcp_nodelay on; ## nagle算法,有须要发送的就当即发送,链接转换为长链接时使用; gzip on; ##开启gzip压缩 gzip_min_length 1k; ##指定最小压缩文件的大小 gzip_buffers 4 16k; ##指定压缩缓冲区的个数和大小 gzip_http_version 1.0; ##指定压缩版本 gzip_comp_level 2; ##指定压缩等级1-9,9等级最高 gzip_types text/plain application/x-javascript text/css application/xml; ##指定压缩文件类型 gzip_vary on; ##前端缓存服务器缓存通过压缩的页面 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; ##配置日志格式,具体变量表示请结合百度,日志格式为access server { listen 80; server_name www.linuxfan.cn; location / { root /usr/local/nginx/html/; index index.html index.htm; } location /status { stub_status on; access_log off; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { expires 30d; ##指定以上格式的文件将进行缓存 } access_log /usr/local/nginx/logs/access.log access; } }
[root@www~]# /etc/init.d/nginx start nginx: [warn] no "fastcgi_cache_key" for "fastcgi_cache" in /usr/local/nginx/conf/nginx.conf:75 nginx: [warn] no "fastcgi_cache_key" for "fastcgi_cache" in /usr/local/nginx/conf/nginx.conf:75 nginx: [warn] no "fastcgi_cache_key" for "fastcgi_cache" in /usr/local/nginx/conf/nginx.conf:75 nginx is starting!! [root@www ~]# netstat -utpln |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3627/nginx [root@www ~]# ps aux |grep nginx |grep -v grep root 3627 0.0 0.0 30708 376 ? Ss 18:59 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 3628 0.1 11.6 113920 57600 ? S 18:59 0:00 nginx: worker process nginx 3629 0.0 11.1 113920 54932 ? S 18:59 0:00 nginx: worker process nginx 3630 0.0 9.7 113920 48336 ? S 18:59 0:00 nginx: worker process nginx 3631 0.0 9.6 113920 47768 ? S 18:59 0:00 nginx: worker process nginx 3632 0.0 9.8 113920 48516 ? S 18:59 0:00 nginx: worker process nginx 3633 0.0 10.5 113920 52084 ? S 18:59 0:00 nginx: worker process nginx 3634 0.0 11.5 113920 57156 ? S 18:59 0:00 nginx: worker process nginx 3635 0.0 9.4 113920 46892 ? S 18:59 0:00 nginx: worker process nginx 3636 0.0 0.0 30844 392 ? S 18:59 0:00 nginx: cache manager process
[root@www ~]# top
访问测试优化后nginx服务
[root@www ~]# yum -y install gcc ctags [root@www ~]# wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz [root@www ~]# tar zxvf webbench-1.5.tar.gz -C /usr/src/ [root@www ~]# cd /usr/src/webbench-1.5/ [root@www webbench-1.5]# mkdir /usr/local/man [root@www webbench-1.5]# make && make install [root@www webbench-1.5]# cd [root@www~]# webbench -c 10000 -t 5 http://www.linuxfan.cn:80/index.html ##并发数为10000,时间为5秒 Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Benchmarking: GET http://www.linuxfan.cn:80/index.html 10000 clients, running 5 sec. Speed=147744 pages/min, 1397500 bytes/sec. Requests: 12312 susceed, 0 failed. [root@www ~]# netstat -nat | awk '/^tcp/ {++S[$NF]} END {for(key in S) print key,"\t",S[key]}' ##查看数据库状态 TIME_WAIT 15961 ##表示收到了对方的FIN报文,并发送出了ACK报文 FIN_WAIT1 166 ##已发送FIN报文,等待对方的ACK SYN_SENT 189 ##这个状态与SYN_RCVD遥相呼应,用于创建链接 FIN_WAIT2 1 ##半关闭状态 ESTABLISHED 1343 ##已经创建链接 SYN_RECV 256 ##已经接收到对方的SYN报文,等待ACK报文 LISTEN 9 ##监听状态
浏览器访问监控界面刷新测试
[root@www ~]# vi /etc/sysctl.conf
[root@www ~]# sysctl -p