各类web服务器的占有率: http:///technologies/overview/web_server/all  
Nginx是一个高性能的web和反向代理服务器软件,与Apache相比,Nginx能够支持更多的并发连接(针对静态的,小文件服务器),且占用资源更少,效率很高。 作为负载均衡服务器,Nginx可以作为http server或DB等服务器的代理服务器,类似专业的Haproxy软件功能(Nginx代理功能相对简单,代理功能及效率不如Haproxy)。 邮件代理服务软件; 缓存服务器,相当于专业的缓存软件,如squid Nginx安装简单,配置文件简洁、配置灵活(支持perl语法)。 如 网易、新浪、赶集、人人网等都在使用Nginx。 Nginx的http服务器特性: 处理静态文件,索引文件及自动索引,打开文件描述符缓存; 使用缓存加速反向代理;简单负载均衡及容错; 远程FastCGI服务的缓存加速支持;简单的负载均衡及容错; 模块化的架构。过滤器包括gzip压缩、ranges支持、chunked相应、xslt,ssi以及圈像缩放,在ssi过滤器中,一个包含多个ssi的页面,如果经由FastCGI或反向代理处理,可被并行处理; 支持SSL,TLS SNI; 基于名字和IP的虚拟主机; Keep-alive和pipelined连接支持; 灵活的配置; 重新加载配置以及在线升级时,不需要中断正在处理的请求; 自定义访问日至格式,带缓存的日志写操作以及快速日志轮转; 3xx-5xx错误代码重定向; 重写(rewrite)模块; 给予客户端IP地址和http基本认证机制的访问控制; 支持PUT、DELETE、MKCOL、COPY、以及MOVE方法; 支持FLV流和MP4流; 速度限制; 来自同一地址的同时连接数或请求数限制;
以上信息来自: http:/// Nginx有点总结: 1.高并发:能支持1-2万甚至更多的并发连接(静态小文件环境下); 2.内存消耗少:在3w并发连接下,开启的10个Nginx进程消耗不到200M内存; 3.可以做HTTP反向代理:即负载均衡功能,相当于专业的Haproxy软件或lvs的功能; 4.内置对RS服务健康检查功能:如果Nginx proxy后端某台web机器宕机,不会影响前端的访问,这个功能还是比较弱,徐后续改进; 5.通过cache插件(cache_purge)可以实现类squid等专业的缓存软件实现的功能; 6.Nginx最重要的优点是:支持kqueue(FreeBSD4.1+),epoll(Linux 2.6+)等网络IO事件模型。由此来支持高并发。
Nginx的应用场合: 使用 Nginx 结合FastCGI运行PHP、JSP、Perl等程序; 使用 Nginx 作为反向代理、负载均衡、规则过滤; 使用 Nginx 运行静态 HTML 页、小图片等;
使用 Nginx 加 cache 插件实现对 web 服务器缓存功能;
提示:近几年,网上热炒 Nginx web server,经过实际测试,对于提供纯静态小文件HTML页面、图片等服务,Apache确实也比Nginx逊色点。但在结合PHP引擎提供php服务及其它更多方面,并不比Nginx差,而apahce的稳定性相对要好一些。 Nginx和其他web服务器的对比 主流web服务产品对比说明: 1)Apache 2)Nginx 基于异步IO模型,性能强,能够支持上万并发; 对小文件支持很好,性能很高(限静态小文件); 代码优美,扩展库必须编译进主程序; 消耗系统资源比较低;
3)lighttpd 
总结:当处理动态数据时,三者的差距不大,从测试结果来看,Apache更有优势点,这是因为处理动态数据的能力取决于 php 和后端数据库的提供服务能力,也就是说瓶颈不在于web服务器上。一般php支持的并发参考值300--1000,java引擎并发300--1000. Nginx使用最新的epoll(Linux2.6内核)和kqueue(freebsd)网络IO模型,而Apache则使用的是传统的select模型。目前Linux能够承受高并发访问的Squid、Memcached都采用的是epoll网络的I/O模型。 Nginx安装配置 1)主要配置文件介绍 配置文件:../nginx/nginx.conf 主目录:../nginx/html 2)主要命令: 查看版本信息:/usr/sbin/nginx -v 配置文件语法检查:/usr/sbin/nginx -t 启动:/etc/init.d/nginx start 停止: /etc/init.d/nginx stop 修改配置,平滑重启: /etc/init.d/nginx reload 注意:重启也可以使用发信号的方式: kill -HUP `cat /var/run/nginx.pid` #向master进程的PID发送信号。 Nginx支持的信号: TEM,INT:快速关闭 QUIT:正常关闭 HUP:平滑重启reload,重新加载配置文件 USR1:重新打开日志文件,在切割日志时有用 USR2:平滑升级 WINCH:从容关闭工作worker进程(只剩主进程master) Nginx 的工作模式:1个 master 进程 +N 个 worker 进程 编译安装 Nginx 1)安装 Nginx 所需的 pcre 库 pcre 全称(Perl Compatible Regular Expressions),中文 perl 兼容正则表达式,官方网址:http://www./,安装pcre 库是为了使Nginx支持 HTTP Rewrite 模块。安装过程如下: tar xzf pcre-8.36.tar.gz cd pcre-8.36 ./configure make; make install cd .. 附:pcre介绍 2)安装Nginx 软件下载:wget useradd nginx -s /sbin/nologin -M tar xzf nginx-1.4.7.tar.gz cd nginx-1.4.7 ./configure --user=nginx --group=nginx --prefix=/usr/local/ nginx-1.4.7 --with-http_ssl_module --with-http_stub_status_module 编译安装的一些参数的获取:./configure --help
make && make install 或者 make; make install ln -s /usr/local/nginx-1.4.7 /usr/local/nginx 做软连接的目的是隐藏自己软件的版本号,一方面是安全考虑,另一方面是使用的时候方便,而且升级时,在升级后,把软连接删除,重新做个软连接即可,其他地方不需要动就能使用新版本的软件。
3)启动并检查安装结果 /usr/local/nginx/sbin/nginx -t
如果遇到错误,解决方法见下面的描述 启动服务: /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 编译安装的Nginx启动,不用加start 如果是rpm包安装的Nginx,需要按如下方式启动: /usr/local/nginx/sbin/nginx start
查看端口: lsof -i :80 ss -lnutp |grep 80 ps -ef |grep nginx 错误解决: 
然后再执行命令:ldconfig 不要还怕错误,要想办解决错误,错误才能使你进步。 Welcome to Nginx ! 如果不能出现 welcome to Nginx,请按如下步骤检查: 部署一个简单的Nginx web站点 Nginx默认站点目录:Nginx安装目录下的html目录(即:/usr/local/nginx/html),这个信息可以在Nginx的配置文件中找到。 
F:\老男孩初级到高级最完整版本\老男孩运维初级\09-LAMP-APACHE-NGINX/L09-10老男孩Linux运维实战培训-Nginx服务生产实战应用指南03..avi Nginx的目录结构说明: [root@test nginx]# tree /usr/local/nginx /usr/local/nginx ├── client_body_temp ├── conf #这是 Nginx 的所有配置文件的目录,及其重要。 │ ├── fastcgi.conf #fastcgi的配置文件 │ ├── fastcgi.conf.default │ ├── fastcgi_params #fastcgi的参数文件 │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types │ ├── mime.types.default │ ├── nginx.conf #Nginx默认的主配置文件 │ ├── nginx.conf.default │ ├── scgi_params │ ├── scgi_params.default │ ├── uwsgi_params │ ├── uwsgi_params.default │ └── win-utf ├── fastcgi_temp #临时目录 ├── html #编译安装时,Nginx的默认站点目录,类似Apache的htdocs目录 │ ├── 50x.html #错误页面优雅替代显示文件,例如:出现502错误会调用此页面 │ └── index.html #默认的首页文件,在实际环境中,大家习惯用(注意:不是必须)index.html、index.jsp来作为网站的首页文件。首页文件名字是在nginx.conf中事先定义好的。具体参数为:index index.html index.htm;注意:与Apache参数的不同DirectoryIndex index.html ├── logs #日志目录,包括访问日志和错误日志 │ ├── access.log #这是Nginx的默认访问日志文件,使用tail -f access.log来实时观看网站的用户访问情况信息。 │ ├── error.log #这是Nginx的错误日志文件,如果Nginx出现启动故障等问题,一定要查看这个错误日志。 │ └── nginx.pid #Nginx的pid文件,Nginx进程启动后,会把所有进程的ID号写到此文件 ├── proxy_temp ├── sbin #这是Nginx命令的目录,如Nginx的启动命令nginx │ └── nginx # ├── scgi_temp #临时目录 └── uwsgi_temp #临时目录
9 directories, 21 files
Nginx主配置文件nginx.conf nginx.conf是Nginx的最重要的配置文件之一,必须搞定它,下面针对默认主配置文件nginx.conf的参数做详细的中文解释: [ root@test ~]# cat -n /usr/local/nginx/conf/nginx.conf 1 2 #user nobody; 3 worker_processes 1; 4 5 #error_log logs/error.log; 6 #error_log logs/error.log notice; 7 #error_log logs/error.log info; 8 9 #pid logs/nginx.pid; 10 11 12 events { 13 worker_connections 1024; 14 } 15 16 17 http { 18 include mime.types; 19 default_type application/octet-stream; 20 21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 22 # '$status $body_bytes_sent "$http_referer" ' 23 # '"$http_user_agent" "$http_x_forwarded_for"'; 24 25 #access_log logs/access.log main; 26 27 sendfile on; 28 #tcp_nopush on; 29 30 #keepalive_timeout 0; 31 keepalive_timeout 65; 32 33 #gzip on; 34 35 server { 36 listen 80; 37 server_name localhost; 38 39 #charset koi8-r; 40 41 #access_log logs/host.access.log main; 42 43 location / { 44 root html; 45 index index.html index.htm; 46 } 47 48 #error_page 404 /404.html; 49 50 # redirect server error pages to the static page /50x.html 51 # 52 error_page 500 502 503 504 /50x.html; 53 location = /50x.html { 54 root html; 55 } 56 57 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 58 # 59 #location ~ \.php$ { 60 # proxy_pass http://127.0.0.1; 61 #} 62 63 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 64 # 65 #location ~ \.php$ { 66 # root html; 67 # fastcgi_pass 127.0.0.1:9000; 68 # fastcgi_index index.php; 69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 70 # include fastcgi_params; 71 #} 72 73 # deny access to .htaccess files, if Apache's document root 74 # concurs with nginx's one 75 # 76 #location ~ /\.ht { 77 # deny all; 78 #} 79 } 80 81 82 # another virtual host using mix of IP-, name-, and port-based configuration 83 # 84 #server { 85 # listen 8000; 86 # listen somename:8080; 87 # server_name somename alias another.alias; 88 89 # location / { 90 # root html; 91 # index index.html index.htm; 92 # } 93 #} 94 95 96 # HTTPS server 97 # 98 #server { 99 # listen 443 ssl; 100 # server_name localhost; 101 102 # ssl_certificate cert.pem; 103 # ssl_certificate_key cert.key; 104 105 # ssl_session_cache shared:SSL:1m; 106 # ssl_session_timeout 5m; 107 108 # ssl_ciphers HIGH:!aNULL:!MD5; 109 # ssl_prefer_server_ciphers on; 110 111 # location / { 112 # root html; 113 # index index.html index.htm; 114 # } 115 #} 116 117 } 去掉注释和空格后: [ root@test conf]# egrep -v "#|^$" nginx.conf worker_processes 1; #工作进程 events { worker_connections 1024; #事件 } http { #http的标签 include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { #虚拟主机的配置 listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } server标签相当于Apache中的一个虚拟主机vhost; 所有的server标签都要放在http的标签的里面; Nginx虚拟主机配置实战: 1)基于域名的虚拟主机 生产环境中最常用到的。必须熟练掌握 在主配置文件的第一行增加Nginx运行用户:user nginx nginx 调整启动进程,根据cpu的数量调节(进程数可以是CPU的数量乘2):worker_processes 8 ; 在events标签里面第一行增加:use epoll 修改server标签 修改配置文件过程中,要注意大括号的对应关系,一定要成对出现。低级错误。 nginx.conf中的配置,都要以分号结束。
user nginx nginx; worker_processes 2; events { use epoll; worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www. ; location / { root html; index index.html index.htm; } } } 创建站点目录: mkdir /data/www/{www,bbs,blog} -p 授权:chown -R nginx.nginx /data/www 创建日志文件目录: mkdir /app/log -p 授权:chown -R nginx.nginx /app/log 修改nginx.conf中的默认站点目录: root /data/www/www;
echo www > /data/www/www/index.html 检查语法 /usr/local/nginx/sbin/nginx -t 平滑重启Nginx /usr/local/nginx/sbin/nginx -s reload
Windows本机解析配置: 开始 -- 运行-- %systemroot%\system32\drivers\etc\hosts 完整的虚拟主机配置如下: [ root@test conf]# cat -n nginx.conf 1 user nginx nginx; 2 worker_processes 4; 3 error_log logs/error.log crit; 4 pid logs/nginx.pid; 5 6 events { 7 use epoll; 8 worker_connections 1024; 9 } 10 http { 11 include mime.types; 12 default_type application/octet-stream; 13 log_format commonlog '$remote_addr - $remote_user [$time_local] "$request" ' 14 '$status $body_bytes_sent "$http_referer" ' 15 '"$http_user_agent" "$http_x_forwarded_for"'; 16 sendfile on; 17 keepalive_timeout 65; 18 server { 19 listen 80; 20 server_name www. ; 21 location / { 22 root /data/www/www; 23 index index.html index.htm; 24 access_log /app/log/www_access.log commonlog; 25 } 26 } 27 ############ 28 server { 29 listen 80; 30 server_name bbs.; 31 location / { 32 root /data/www/bbs; 33 index index.html index.htm; 34 access_log /app/log/bbs_access.log commonlog; 35 } 36 } 37 ############ 38 server { 39 listen 80; 40 server_name blog.; 41 location / { 42 root /data/www/blog; 43 index index.html index.htm; 44 access_log /app/log/blog_access.log commonlog; 45 } 46 } 47 } Nginx配置文件的目录优化 如果虚拟主机比较多的时候,把所有虚拟主机都放一个配置文件里面,就不太好查看和管理了。可以模拟Apache的虚拟目录的方式,包含虚拟目录的配置文件。 首先创建虚拟目录配置文件存放目录: mkdir /usr/local/nginx/extra 修改后的配置文件包括两个文件,分别如下: 主配置文件: [ root@test ~]# cat -n /usr/local/nginx/conf/nginx.conf 1 user nginx nginx; 2 worker_processes 4; 3 error_log logs/error.log crit; 4 pid logs/nginx.pid; 5 6 events { 7 use epoll; 8 worker_connections 1024; 9 } 10 http { 11 include mime.types; 12 default_type application/octet-stream; 13 log_format commonlog '$remote_addr - $remote_user [$time_local] "$request" ' 14 '$status $body_bytes_sent "$http_referer" ' 15 '"$http_user_agent" "$http_x_forwarded_for"'; 16 sendfile on; 17 keepalive_timeout 65; 18 include ../extra/nginx_vhosts.conf; 19 } 虚拟主机配置文件: [ root@test ~]# cat -n /usr/local/nginx/extra/nginx_vhosts.conf 1 server { 2 listen 80; 3 server_name www. ; 4 location / { 5 root /data/www/www; 6 index index.html index.htm; 7 access_log /app/log/www_access.log commonlog; 8 } 9 } 10 ############ 11 server { 12 listen 80; 13 server_name bbs.; 14 location / { 15 root /data/www/bbs; 16 index index.html index.htm; 17 access_log /app/log/bbs_access.log commonlog; 18 } 19 } 20 ############ 21 server { 22 listen 80; 23 server_name blog.; 24 location / { 25 root /data/www/blog; 26 index index.html index.htm; 27 access_log /app/log/blog_access.log commonlog; 28 } 29 } 甚至我们还可以将每个虚拟主机定义一个vhost配置文件,这样简单清晰,可以分别定义,方便维护和管理; 包含的时候,只需要修改主配置文件中的: include ../extra/nginx_vhosts.conf; 为:include ../extra/*.conf; 也可以分别写一行; 配置文件优化: 对nginx_vhosts.conf配置文件拆分: sed -n '1,9p' nginx_vhosts.conf > www..conf sed -n '10,19p' nginx_vhosts.conf > bbs..conf
sed -n '20,29p' nginx_vhosts.conf > blog..conf
编辑主配置文件,将include ../extra/nginx_vhosts.conf;替换为: include ../extra/www..conf;
include ../extra/bbs..conf;
include ../extra/blog..conf;
配置Nginx状态信息虚拟主机: cat >>/usr/local/nginx/conf/extra/nginx_vhosts.conf <<EOF #将下面这个server追加到Nginx的虚拟主机配置文件中。 ###### server { listen 80; server_name status.; location / { stub_status on; access_log off; } } EOF 2)基于端口的虚拟主机配置 只需修改配置文件: nginx_vhosts.conf 中的每个虚拟主机中的端口即可,即,将默认的80端口改成别的端口,如8060、8070、8080等; 然后重新加载配置文件。 3)基于IP地址的虚拟主机配置 此种比较少用,了解下即可 如果要测试此类虚拟主机,需要在测试机上增加几个IP地址(在负载均衡配置里,此IP也被称为VIP): ifconfig eth0:192.168.64.101 netmask 255.255.255.0 up ifconfig eth0:192.168.64.102 netmask 255.255.255.0 up
ifconfig eth0:192.168.64.103 netmask 255.255.255.0 up
修改配置文件: nginx_vhosts.conf 把server_name 改成这几个IP地址,将每个端口前面加上对应的IP地址; 实例: [root@test ~]# cat -n /usr/local/nginx/extra/nginx_vhosts.conf 1 server { 2 listen 192.168.64.101:8060; 3 server_name 192.168.64.101; 4 location / { 5 root /data/www/www; 6 index index.html index.htm; 7 access_log /app/log/www_access.log commonlog; 8 } 9 }
批量替换:将80替换为:IP加端口 :g/80/s//192.168.64.101:80/g 后面两个与之类似 修改完成后,检查配置文件语法,重新加载配置文件,进行测试; 检查语法 : /usr/local/nginx/sbin/nginx -t 平滑重启Nginx : /usr/local/nginx/sbin/nginx -s reload Nginx虚拟主机配置别名: [root@test ~]# cat -n /usr/local/nginx/extra/nginx_vhosts.conf 1 server { 2 listen 80; 3 server_name www. ; 4 location / { 5 root /data/www/www; 6 index index.html index.htm; 7 access_log /app/log/www_access.log commonlog; 8 } 9 } 说明:别名,就写在域名后面就行了,以空格分开即可; 应用场景: 1)如输入baidu.com会自动跳转到www.baidu.com,输入taobao.com会自动跳转到www.taobao.com,这里我们就是要实现输入和访问www.一样显示我们的网站。 2)在生产环境中,利用别名来监控集群环境下面的RS的URL是否正常。 如:ServerAlias 21www. 22www. 可以在监控服务里面配置hosts来监控RS 21www. 22www.等地址是否正常,进而判断每台机器的www.是否正常。 如不使用别名,则很难通过URL方式检测判断下面的机器是否正常(因为域名是同一个)。 当然,Keepalived和Haproxy等软件也有对于RS使用URL的健康检查措施,这方面的案例见相关文档。 配置多个Nginx实例 Nginx安装后的启动命令在“/usr/local/nginx/sbin/nginx”,我们可以通过 -h 参数查看相关参数的用法: [ root@test ~]# /usr/local/nginx/sbin/nginx -h nginx version: nginx/1.8.0 Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options: -?,-h : this help -v : show version and exit #显示Nginx版本号后退出 -V : show version and configure options then exit #显示Nginx版本号和配置选项(配置选项即:编译安装Nginx的时候所用的参数)后退出 -t : test configuration and exit #测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否存在语法错误。 -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload #发送信号给一个master进程,这里的reload参数很重要,是优雅重启Nginx的参数,类似Apache的graceful参数。 -p prefix : set prefix path (default: /usr/local/nginx-1.8.0/) -c filename : set configuration file (default: conf/nginx.conf) #使用制定的Nginx配置文件,而不是conf目录下的nginx.conf。借此可以启动多个Nginx实例。 -g directives : set global directives out of configuration file [ root@test ~]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.8.0 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx-1.8.0 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module cp -af: 杀掉nginx:pkill nginx 启动多实例: 应用场景: 服务器资源有限,且单个服务器的性能又很空余,又希望各http相对独立,此时就可以采用nginx多实例的方式;
本文出自 “冷水泡茶” 博客,请务必保留此出处http://miaocbin.blog.51cto.com/689091/1692772
|