SlideShare une entreprise Scribd logo
1  sur  53
 
高性能 Web 服务器 Nginx 及相关新技术的应用实践 ,[object Object],[object Object],[object Object]
什么是 Nginx ? ,[object Object],[object Object],[object Object]
Nginx 的优点① ,[object Object],[object Object],[object Object],[object Object]
Nginx 的优点② ,[object Object],[object Object],[object Object],[object Object]
单台 Nginx 支撑了高达 2.8 万的活动并发连接数 2009-09-03 14:30 ,金山游戏《剑侠情缘网络版 3 》临时维护 1 小时,大量玩家上官网,论坛、评论、客服等动态应用 Nginx 服务器集群,每台服务器的 Nginx 活动连接数达到 2.8 万,这是本人遇到的 Nginx 生产环境最高并发值。
Nginx 的主要应用类别 ,[object Object],[object Object],[object Object],[object Object]
Nginx 在金山逍遥网中的应用案例 ,[object Object]
金山逍遥网 Nginx 七层负载均衡的应用
Nginx 承担每个机房 Web 负载均衡服务
简单的 Nginx 负载均衡配置① ……  upstream bbs_server_pool { server  192.168.1.15:80 weight=1 max_fails=2 fail_timeout=30s; server  192.168.1.16:80 weight=1 max_fails=2 fail_timeout=30s; server  192.168.1.17:80 weight=1 max_fails=2 fail_timeout=30s; server  192.168.1.18:80 weight=1 max_fails=2 fail_timeout=30s; }  …… 在 nginx.conf 配置文件中,用 upstream 指令定义一组反向代理 / 负载均衡后端服务器池。
简单的 Nginx 负载均衡配置② ……  server{ listen 80; server_name  bbs.yourdomain.com *.bbs.yourdomain.com; location / { proxy_pass http://bbs_server_pool; proxy_set_header Host  $host; proxy_set_header X-Forwarded-For  $remote_addr; } access_log off; } ……
简单的 Nginx 负载均衡配置③ ,[object Object],[object Object],[object Object]
Nginx 负载均衡的双机热备
通常情况下的负载均衡 HA 高可用 优点:实现了双机热备、故障自动转移。 缺点:备机服务器处于闲置状态,浪费了硬件资源。
逍遥网 Nginx 负载均衡双机互备 正常情况下,两台 Nginx 负载均衡服务器全部处于活动状态,对外提供服务。
服务器①绑定 IP 别名 ,[object Object],[object Object],[object Object],[object Object]
服务器②绑定 IP 别名 ,[object Object],[object Object],[object Object],[object Object]
 
新的 Nginx 双机互备 ( 发生故障时 ) 自动接管公网虚拟 IP ,实现故障转移
服务器①去除 IP 别名 ,[object Object],[object Object]
服务器②接管原服务器①的虚拟 IP ,[object Object],[object Object],[object Object],[object Object]
Nginx 负载均衡 URL 分发
硬件、软件 七层负载均衡对比: NetScaler 与 Nginx
硬件、软件 七层负载均衡对比: NetScaler 与 Nginx
根据不同的 URL 转发到不同服务器 server{ listen  80; server_name  abc.domain.com; location ~ ^/admincp.php { proxy_pass http://192.168.1.11; proxy_set_header Host  $host; proxy_set_header X-Forwarded-For  $remote_addr; } location / { proxy_pass http://php_server_pool; proxy_set_header Host  $host; proxy_set_header X-Forwarded-For  $remote_addr; } }
Web 相关文件的实时自动同步
少量文件的多服务器自动同步 ,[object Object],[object Object],[object Object]
大量文件的多服务器自动同步 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Nginx 的 Web 缓存服务
Nginx 的缓存功能 ,[object Object],[object Object],[object Object],[object Object]
反向代理中的 Nginx.conf 缓存配置① …… # 设置 Web 缓存区名称为 cache_one ,缓存空间大小为 2000MB , 1 天清理一次缓存,单个文件超过 5m 不缓存。 proxy_cache_path  /data0/proxy_cache_path  levels=1:2  keys_zone=cache_one:2000m inactive=1d max_size=5m; # 注: proxy_temp_path 和 proxy_cache_path 指定的路径必须在同一分区 proxy_temp_path /data0/proxy_temp_path; upstream my_server_pool { server  192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s; server  192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s; } ……
反向代理中的 Nginx.conf 缓存配置② server { listen  80; server_name  my.domain.com; location / { proxy_set_header Host  $host; proxy_set_header X-Forwarded-For  $remote_addr; proxy_pass http://my_server_pool; } location ~ .*(gif|jpg|jpeg|png|bmp|swf|js|css)$ { # 对图片、 JS 、 CSS 进行缓存,使用 Web 缓存区 cache_one proxy_cache cache_one;
反向代理中的 Nginx.conf 缓存配置③ # 对不同 HTTP 状态码缓存设置不同的缓存时间 proxy_cache_valid  200 10m; proxy_cache_valid  304 3m; proxy_cache_valid  301 302 1h; proxy_cache_valid  any 1m; # 设置 Web 缓存的 Key 值, Nginx 根据 Key 值 md5 哈希存储缓存,这里根据“域名、 URI 、客户端请求 Header 头中的 If-Modified-Since 信息”组合成 Key 。 proxy_cache_key $host$request_uri$http_if_modified_since; # 反向代理,访问后端内容源服务器 proxy_set_header Host  $host; proxy_set_header X-Forwarded-For  $remote_addr; proxy_pass http://my_server_pool; } access_log  off; }
Nginx 缓存功能的优点 ,[object Object],[object Object]
Nginx 的 Rewrite 重写规则
Nginx Rewrite 规则相关指令 Nginx Rewrite 规则相关指令有 if 、 rewrite 、 set 、 return 、 break 等,其中 rewrite 是最关键的指令。一个简单的 Nginx Rewrite 规则语法如下: rewrite  ^/b/(.*)html  /play.php?video=$1 break; 如果加上 if 语句,示例如下: if (!-f $request_filename) { rewrite ^/img/(.*)$ /site/$host/images/$1 last; }
Nginx 与 Apache 的 Rewrite 规则实例对比① 简单的 Nginx 和 Apache  重写规则区别不大,基本上能够完全兼容。例如: Apache Rewrite  规则: RewriteRule  ^/(mianshi|xianjing)/$ /zl/index.php?name=$1 [L] RewriteRule  ^/ceshi/$ /zl/ceshi.php [L] RewriteRule  ^/(mianshi)_([a-zA-Z]+)/$ /zl/index.php?name=$1_$2 [L] RewriteRule  ^/pingce([0-9]*)/$ /zl/pingce.php?id=$1 [L] Nginx Rewrite  规则: rewrite  ^/(mianshi|xianjing)/$ /zl/index.php?name=$1 last; rewrite  ^/ceshi/$ /zl/ceshi.php last; rewrite  ^/(mianshi)_([a-zA-Z]+)/$ /zl/index.php?name=$1_$2 last; rewrite  ^/pingce([0-9]*)/$ /zl/pingce.php?id=$1 last; 由以上示例可以看出, Apache 的 Rewrite 规则改为 Nginx 的 Rewrite 规则,其实很简单: Apache 的 RewriteRule 指令换成 Nginx 的 rewrite 指令, Apache 的 [L] 标记换成 Nginx 的 last 标记,中间的内容不变。
Nginx 与 Apache 的 Rewrite 规则实例对比② 如果 Apache 的 Rewrite 规则改为 Nginx 的 Rewrite 规则后,使用 nginx -t 命令检查发现 nginx.conf 配置文件有语法错误,那么可以尝试给条件加上引号。例如一下的 Nginx Rewrite 规则会报语法错误: rewrite  ^/([0-9]{5}).html$ /x.jsp?id=$1  last; 加上引号就正确了: rewrite  "^/([0-9]{5}).html$" /x.jsp?id=$1  last;
Nginx 与 Apache 的 Rewrite 规则实例对比③ Apache 与 Nginx 的 Rewrite 规则在 URL 跳转时有细微的区别: Apache Rewrite  规则: RewriteRule  ^/html/tagindex/([a-zA-Z]+)/.*$ /$1/ [R=301,L] Nginx Rewrite  规则: rewrite  ^/html/tagindex/([a-zA-Z]+)/.*$ http://$host/$1/  permanent; 以上示例中,我们注意到, Nginx Rewrite  规则的置换串中增加了“ http://$host” ,这是在 Nginx 中要求的。
Nginx 与 Apache 的 Rewrite 规则实例对比④ 另外, Apache 与 Nginx 的 Rewrite 规则在变量名称方面也有区别,例如: Apache Rewrite  规则: RewriteRule  ^/user/login/$ /user/login.php?login=1&forward=http://%{HTTP_HOST}  [L] Nginx Rewrite  规则: rewrite  ^/user/login/$ /user/login.php?login=1&forward=http://$host  last;
Nginx 与 Apache 的 Rewrite 规则实例对比⑤ Apache 与 Nginx Rewrite  规则的一些功能相同或类似的指令、标记对应关系: Apache 的 RewriteCond 指令对应 Nginx 的 if 指令; Apache 的 RewriteRule 指令对应 Nginx 的 rewrite 指令; Apache 的 [R] 标记对应 Nginx 的 redirect 标记; Apache 的 [P] 标记对应 Nginx 的 last 标记; Apache 的 [R,L] 标记对应 Nginx 的 redirect 标记; Apache 的 [P,L] 标记对应 Nginx 的 last 标记; Apache 的 [PT,L] 标记对应 Nginx 的 last 标记;
Nginx 与 Apache 的多条件 Rewrite 示例① 允许指定的域名访问本站,其他域名一律跳转到 http://www.aaa.com Apache Rewrite  规则: RewriteCond %{HTTP_HOST}  ^(.*?)domaincom$ RewriteCond %{HTTP_HOST}  !^qitadomaincom$ RewriteCond %{DOCUMENT_ROOT}/market/%1/index.htm -f RewriteRule ^/wu/$ /market/%1/index.htm [L] Nginx 的 if 指令不支持嵌套,也不支持 AND 、 OR 等多条件匹配,相比于 Apache 的 RewriteCond ,显得麻烦一些,但是,我们可以通过 下一页 的 Nginx 配置写法来实现这个示例:
Nginx 与 Apache 的多条件 Rewrite 示例② Nginx Rewrite  规则: if ($host ~* ^(.*?)domaincom$) { set $var_wupin_city $1; set $var_wupin '1'; } if ($host ~* ^qitadomaincom$) { set $var_wupin '0'; } if (!-f $document_root/market/$var_wupin_city/index.htm) { set $var_wupin '0'; } if ($var_wupin ~ '1') { rewrite ^/wu/$ /market/$var_wupin_city/index.htm last; }
Nginx 与金山逍遥 TCSQL 的配合
一般数据库的缓存类型 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
什么是 TCSQL 实时列表缓存数据库? ,[object Object],[object Object],[object Object]
 
TCSQL 的查询速度 ,[object Object],[object Object],[object Object]
TCSQL 的查询速度 ,[object Object],[object Object]
找出瓶颈 ,[object Object],[object Object],[object Object],[object Object],[object Object]
抛弃制约性能的 PHP 中间层 ,[object Object],[object Object],[object Object],[object Object]
谢谢!

Contenu connexe

Tendances

gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務升煌 黃
 
聊聊我接触的集群管理
聊聊我接触的集群管理聊聊我接触的集群管理
聊聊我接触的集群管理rfyiamcool
 
使用Nginx轻松实现开源负载均衡——对外版
使用Nginx轻松实现开源负载均衡——对外版使用Nginx轻松实现开源负载均衡——对外版
使用Nginx轻松实现开源负载均衡——对外版pigso
 
使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡guest2d0fe3
 
使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡Cary Yang
 
Linux network monitoring hands-on pratice
Linux network monitoring hands-on praticeLinux network monitoring hands-on pratice
Linux network monitoring hands-on praticeKenny (netman)
 
Kafka的设计与实现
Kafka的设计与实现Kafka的设计与实现
Kafka的设计与实现wang xing
 
Http Headers 與 Cache 機制(2016)
Http Headers 與 Cache 機制(2016)Http Headers 與 Cache 機制(2016)
Http Headers 與 Cache 機制(2016)振揚 陳
 
构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接 构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接 Renaun Erickson
 
Http cache 的優化
Http cache 的優化Http cache 的優化
Http cache 的優化振揚 陳
 
Improvements Made in KoP 2.9.0 - Pulsar Summit Asia 2021
Improvements Made in KoP 2.9.0  - Pulsar Summit Asia 2021Improvements Made in KoP 2.9.0  - Pulsar Summit Asia 2021
Improvements Made in KoP 2.9.0 - Pulsar Summit Asia 2021StreamNative
 
Traffic server overview
Traffic server overviewTraffic server overview
Traffic server overviewqianshi
 
课题三:Nginx基础知识
课题三:Nginx基础知识课题三:Nginx基础知识
课题三:Nginx基础知识Liu Allen
 
The Evolution History of RoP(RocketMQ-on-Pulsar) - Pulsar Summit Asia 2021
The Evolution History of RoP(RocketMQ-on-Pulsar) - Pulsar Summit Asia 2021The Evolution History of RoP(RocketMQ-on-Pulsar) - Pulsar Summit Asia 2021
The Evolution History of RoP(RocketMQ-on-Pulsar) - Pulsar Summit Asia 2021StreamNative
 
Php Webservers
Php WebserversPhp Webservers
Php Webserverssamon127
 
Apache配置文件说明
Apache配置文件说明Apache配置文件说明
Apache配置文件说明wensheng wei
 

Tendances (17)

gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務
 
聊聊我接触的集群管理
聊聊我接触的集群管理聊聊我接触的集群管理
聊聊我接触的集群管理
 
使用Nginx轻松实现开源负载均衡——对外版
使用Nginx轻松实现开源负载均衡——对外版使用Nginx轻松实现开源负载均衡——对外版
使用Nginx轻松实现开源负载均衡——对外版
 
使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡
 
使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡
 
Linux network monitoring hands-on pratice
Linux network monitoring hands-on praticeLinux network monitoring hands-on pratice
Linux network monitoring hands-on pratice
 
Kafka的设计与实现
Kafka的设计与实现Kafka的设计与实现
Kafka的设计与实现
 
Http Headers 與 Cache 機制(2016)
Http Headers 與 Cache 機制(2016)Http Headers 與 Cache 機制(2016)
Http Headers 與 Cache 機制(2016)
 
构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接 构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接
 
Http cache 的優化
Http cache 的優化Http cache 的優化
Http cache 的優化
 
Improvements Made in KoP 2.9.0 - Pulsar Summit Asia 2021
Improvements Made in KoP 2.9.0  - Pulsar Summit Asia 2021Improvements Made in KoP 2.9.0  - Pulsar Summit Asia 2021
Improvements Made in KoP 2.9.0 - Pulsar Summit Asia 2021
 
Traffic server overview
Traffic server overviewTraffic server overview
Traffic server overview
 
课题三:Nginx基础知识
课题三:Nginx基础知识课题三:Nginx基础知识
课题三:Nginx基础知识
 
The Evolution History of RoP(RocketMQ-on-Pulsar) - Pulsar Summit Asia 2021
The Evolution History of RoP(RocketMQ-on-Pulsar) - Pulsar Summit Asia 2021The Evolution History of RoP(RocketMQ-on-Pulsar) - Pulsar Summit Asia 2021
The Evolution History of RoP(RocketMQ-on-Pulsar) - Pulsar Summit Asia 2021
 
Php Webservers
Php WebserversPhp Webservers
Php Webservers
 
-Nginx book
 -Nginx book -Nginx book
-Nginx book
 
Apache配置文件说明
Apache配置文件说明Apache配置文件说明
Apache配置文件说明
 

En vedette

Valgrind debugger Tutorial
Valgrind debugger TutorialValgrind debugger Tutorial
Valgrind debugger TutorialAnurag Tomar
 
Script up your application with Lua! -- RyanE -- OpenWest 2014
Script up your application with Lua! -- RyanE -- OpenWest 2014Script up your application with Lua! -- RyanE -- OpenWest 2014
Script up your application with Lua! -- RyanE -- OpenWest 2014ryanerickson
 
Nginx+lua在阿里巴巴的使用
Nginx+lua在阿里巴巴的使用Nginx+lua在阿里巴巴的使用
Nginx+lua在阿里巴巴的使用OpenRestyCon
 
淺入淺出 GDB
淺入淺出 GDB淺入淺出 GDB
淺入淺出 GDBJim Chang
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handoutSuraj Kumar
 
Nginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP TricksNginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP TricksAdam Wiggins
 
Montreal On Rails 5 : Rails deployment using : Nginx, Mongrel, Mongrel_cluste...
Montreal On Rails 5 : Rails deployment using : Nginx, Mongrel, Mongrel_cluste...Montreal On Rails 5 : Rails deployment using : Nginx, Mongrel, Mongrel_cluste...
Montreal On Rails 5 : Rails deployment using : Nginx, Mongrel, Mongrel_cluste...addame
 
Nginx internals
Nginx internalsNginx internals
Nginx internalsliqiang xu
 
Nginx深度開發與客制化
Nginx深度開發與客制化Nginx深度開發與客制化
Nginx深度開發與客制化Joshua Zhu
 
The basics and design of lua table
The basics and design of lua tableThe basics and design of lua table
The basics and design of lua tableShuai Yuan
 
Lcu14 Lightning Talk- NGINX
Lcu14 Lightning Talk- NGINXLcu14 Lightning Talk- NGINX
Lcu14 Lightning Talk- NGINXLinaro
 
Delivering High-Availability Web Services with NGINX Plus on AWS
Delivering High-Availability Web Services with NGINX Plus on AWSDelivering High-Availability Web Services with NGINX Plus on AWS
Delivering High-Availability Web Services with NGINX Plus on AWSNGINX, Inc.
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90minsLarry Cai
 
Tuning TCP and NGINX on EC2
Tuning TCP and NGINX on EC2Tuning TCP and NGINX on EC2
Tuning TCP and NGINX on EC2Chartbeat
 
NGINX Installation and Tuning
NGINX Installation and TuningNGINX Installation and Tuning
NGINX Installation and TuningNGINX, Inc.
 
Using ngx_lua / lua-nginx-module in pixiv
Using ngx_lua / lua-nginx-module in pixivUsing ngx_lua / lua-nginx-module in pixiv
Using ngx_lua / lua-nginx-module in pixivShunsuke Michii
 
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明National Cheng Kung University
 
How to secure your web applications with NGINX
How to secure your web applications with NGINXHow to secure your web applications with NGINX
How to secure your web applications with NGINXWallarm
 

En vedette (20)

Valgrind debugger Tutorial
Valgrind debugger TutorialValgrind debugger Tutorial
Valgrind debugger Tutorial
 
Script up your application with Lua! -- RyanE -- OpenWest 2014
Script up your application with Lua! -- RyanE -- OpenWest 2014Script up your application with Lua! -- RyanE -- OpenWest 2014
Script up your application with Lua! -- RyanE -- OpenWest 2014
 
Nginx+lua在阿里巴巴的使用
Nginx+lua在阿里巴巴的使用Nginx+lua在阿里巴巴的使用
Nginx+lua在阿里巴巴的使用
 
淺入淺出 GDB
淺入淺出 GDB淺入淺出 GDB
淺入淺出 GDB
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handout
 
Nginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP TricksNginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP Tricks
 
Montreal On Rails 5 : Rails deployment using : Nginx, Mongrel, Mongrel_cluste...
Montreal On Rails 5 : Rails deployment using : Nginx, Mongrel, Mongrel_cluste...Montreal On Rails 5 : Rails deployment using : Nginx, Mongrel, Mongrel_cluste...
Montreal On Rails 5 : Rails deployment using : Nginx, Mongrel, Mongrel_cluste...
 
Nginx internals
Nginx internalsNginx internals
Nginx internals
 
Nginx深度開發與客制化
Nginx深度開發與客制化Nginx深度開發與客制化
Nginx深度開發與客制化
 
Learn C Programming Language by Using GDB
Learn C Programming Language by Using GDBLearn C Programming Language by Using GDB
Learn C Programming Language by Using GDB
 
Nginx-lua
Nginx-luaNginx-lua
Nginx-lua
 
The basics and design of lua table
The basics and design of lua tableThe basics and design of lua table
The basics and design of lua table
 
Lcu14 Lightning Talk- NGINX
Lcu14 Lightning Talk- NGINXLcu14 Lightning Talk- NGINX
Lcu14 Lightning Talk- NGINX
 
Delivering High-Availability Web Services with NGINX Plus on AWS
Delivering High-Availability Web Services with NGINX Plus on AWSDelivering High-Availability Web Services with NGINX Plus on AWS
Delivering High-Availability Web Services with NGINX Plus on AWS
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90mins
 
Tuning TCP and NGINX on EC2
Tuning TCP and NGINX on EC2Tuning TCP and NGINX on EC2
Tuning TCP and NGINX on EC2
 
NGINX Installation and Tuning
NGINX Installation and TuningNGINX Installation and Tuning
NGINX Installation and Tuning
 
Using ngx_lua / lua-nginx-module in pixiv
Using ngx_lua / lua-nginx-module in pixivUsing ngx_lua / lua-nginx-module in pixiv
Using ngx_lua / lua-nginx-module in pixiv
 
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
 
How to secure your web applications with NGINX
How to secure your web applications with NGINXHow to secure your web applications with NGINX
How to secure your web applications with NGINX
 

Similaire à 高性能Web服务器Nginx及相关新技术的应用实践

高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用redhat9
 
Nginx+常见应用技术指南
Nginx+常见应用技术指南Nginx+常见应用技术指南
Nginx+常见应用技术指南andy54321
 
Varnish简介
Varnish简介Varnish简介
Varnish简介fangdeng
 
缓存技术浅谈
缓存技术浅谈缓存技术浅谈
缓存技术浅谈Robbin Fan
 
X64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 newX64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 newYiwei Ma
 
如何使用 Xhprof 分析網站效能 (真實案例2)
如何使用 Xhprof 分析網站效能 (真實案例2)如何使用 Xhprof 分析網站效能 (真實案例2)
如何使用 Xhprof 分析網站效能 (真實案例2)Cyril Wang
 
独爽不如众乐
独爽不如众乐独爽不如众乐
独爽不如众乐Zheng Biao
 
分享平台构建之旅
分享平台构建之旅分享平台构建之旅
分享平台构建之旅tblanlan
 
Lamp高性能设计
Lamp高性能设计Lamp高性能设计
Lamp高性能设计锐 张
 
腾讯大讲堂08 可扩展web架构探讨
腾讯大讲堂08 可扩展web架构探讨腾讯大讲堂08 可扩展web架构探讨
腾讯大讲堂08 可扩展web架构探讨George Ang
 
Hacking Nginx at Taobao
Hacking Nginx at TaobaoHacking Nginx at Taobao
Hacking Nginx at TaobaoJoshua Zhu
 
构建基于Lamp的网站架构
构建基于Lamp的网站架构构建基于Lamp的网站架构
构建基于Lamp的网站架构Cosey Lee
 
開發環境建置
開發環境建置開發環境建置
開發環境建置Shengyou Fan
 
康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)Yiwei Ma
 
Cent os 安装 subversion
Cent os 安装 subversionCent os 安装 subversion
Cent os 安装 subversionYUCHENG HU
 
Bypat博客出品-服务器运维集群方法总结
Bypat博客出品-服务器运维集群方法总结Bypat博客出品-服务器运维集群方法总结
Bypat博客出品-服务器运维集群方法总结redhat9
 
PHP & AppServ
PHP & AppServPHP & AppServ
PHP & AppServHt Wang
 
Hbase使用hadoop分析
Hbase使用hadoop分析Hbase使用hadoop分析
Hbase使用hadoop分析baggioss
 

Similaire à 高性能Web服务器Nginx及相关新技术的应用实践 (20)

高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用
 
Nginx+常见应用技术指南
Nginx+常见应用技术指南Nginx+常见应用技术指南
Nginx+常见应用技术指南
 
Varnish简介
Varnish简介Varnish简介
Varnish简介
 
缓存技术浅谈
缓存技术浅谈缓存技术浅谈
缓存技术浅谈
 
X64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 newX64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 new
 
Paveo Tweak WordPress
Paveo Tweak WordPressPaveo Tweak WordPress
Paveo Tweak WordPress
 
如何使用 Xhprof 分析網站效能 (真實案例2)
如何使用 Xhprof 分析網站效能 (真實案例2)如何使用 Xhprof 分析網站效能 (真實案例2)
如何使用 Xhprof 分析網站效能 (真實案例2)
 
独爽不如众乐
独爽不如众乐独爽不如众乐
独爽不如众乐
 
分享平台构建之旅
分享平台构建之旅分享平台构建之旅
分享平台构建之旅
 
Lamp高性能设计
Lamp高性能设计Lamp高性能设计
Lamp高性能设计
 
腾讯大讲堂08 可扩展web架构探讨
腾讯大讲堂08 可扩展web架构探讨腾讯大讲堂08 可扩展web架构探讨
腾讯大讲堂08 可扩展web架构探讨
 
Hacking Nginx at Taobao
Hacking Nginx at TaobaoHacking Nginx at Taobao
Hacking Nginx at Taobao
 
构建基于Lamp的网站架构
构建基于Lamp的网站架构构建基于Lamp的网站架构
构建基于Lamp的网站架构
 
開發環境建置
開發環境建置開發環境建置
開發環境建置
 
康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)
 
Cent os 安装 subversion
Cent os 安装 subversionCent os 安装 subversion
Cent os 安装 subversion
 
Bypat博客出品-服务器运维集群方法总结
Bypat博客出品-服务器运维集群方法总结Bypat博客出品-服务器运维集群方法总结
Bypat博客出品-服务器运维集群方法总结
 
PHP & AppServ
PHP & AppServPHP & AppServ
PHP & AppServ
 
Hbase使用hadoop分析
Hbase使用hadoop分析Hbase使用hadoop分析
Hbase使用hadoop分析
 
摘星
摘星摘星
摘星
 

高性能Web服务器Nginx及相关新技术的应用实践

  • 1.  
  • 2.
  • 3.
  • 4.
  • 5.
  • 6. 单台 Nginx 支撑了高达 2.8 万的活动并发连接数 2009-09-03 14:30 ,金山游戏《剑侠情缘网络版 3 》临时维护 1 小时,大量玩家上官网,论坛、评论、客服等动态应用 Nginx 服务器集群,每台服务器的 Nginx 活动连接数达到 2.8 万,这是本人遇到的 Nginx 生产环境最高并发值。
  • 7.
  • 8.
  • 10. Nginx 承担每个机房 Web 负载均衡服务
  • 11. 简单的 Nginx 负载均衡配置① …… upstream bbs_server_pool { server 192.168.1.15:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.16:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.17:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.18:80 weight=1 max_fails=2 fail_timeout=30s; } …… 在 nginx.conf 配置文件中,用 upstream 指令定义一组反向代理 / 负载均衡后端服务器池。
  • 12. 简单的 Nginx 负载均衡配置② …… server{ listen 80; server_name bbs.yourdomain.com *.bbs.yourdomain.com; location / { proxy_pass http://bbs_server_pool; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log off; } ……
  • 13.
  • 15. 通常情况下的负载均衡 HA 高可用 优点:实现了双机热备、故障自动转移。 缺点:备机服务器处于闲置状态,浪费了硬件资源。
  • 16. 逍遥网 Nginx 负载均衡双机互备 正常情况下,两台 Nginx 负载均衡服务器全部处于活动状态,对外提供服务。
  • 17.
  • 18.
  • 19.  
  • 20. 新的 Nginx 双机互备 ( 发生故障时 ) 自动接管公网虚拟 IP ,实现故障转移
  • 21.
  • 22.
  • 26. 根据不同的 URL 转发到不同服务器 server{ listen 80; server_name abc.domain.com; location ~ ^/admincp.php { proxy_pass http://192.168.1.11; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } location / { proxy_pass http://php_server_pool; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } }
  • 28.
  • 29.
  • 30. Nginx 的 Web 缓存服务
  • 31.
  • 32. 反向代理中的 Nginx.conf 缓存配置① …… # 设置 Web 缓存区名称为 cache_one ,缓存空间大小为 2000MB , 1 天清理一次缓存,单个文件超过 5m 不缓存。 proxy_cache_path /data0/proxy_cache_path levels=1:2 keys_zone=cache_one:2000m inactive=1d max_size=5m; # 注: proxy_temp_path 和 proxy_cache_path 指定的路径必须在同一分区 proxy_temp_path /data0/proxy_temp_path; upstream my_server_pool { server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s; } ……
  • 33. 反向代理中的 Nginx.conf 缓存配置② server { listen 80; server_name my.domain.com; location / { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://my_server_pool; } location ~ .*(gif|jpg|jpeg|png|bmp|swf|js|css)$ { # 对图片、 JS 、 CSS 进行缓存,使用 Web 缓存区 cache_one proxy_cache cache_one;
  • 34. 反向代理中的 Nginx.conf 缓存配置③ # 对不同 HTTP 状态码缓存设置不同的缓存时间 proxy_cache_valid 200 10m; proxy_cache_valid 304 3m; proxy_cache_valid 301 302 1h; proxy_cache_valid any 1m; # 设置 Web 缓存的 Key 值, Nginx 根据 Key 值 md5 哈希存储缓存,这里根据“域名、 URI 、客户端请求 Header 头中的 If-Modified-Since 信息”组合成 Key 。 proxy_cache_key $host$request_uri$http_if_modified_since; # 反向代理,访问后端内容源服务器 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://my_server_pool; } access_log off; }
  • 35.
  • 36. Nginx 的 Rewrite 重写规则
  • 37. Nginx Rewrite 规则相关指令 Nginx Rewrite 规则相关指令有 if 、 rewrite 、 set 、 return 、 break 等,其中 rewrite 是最关键的指令。一个简单的 Nginx Rewrite 规则语法如下: rewrite ^/b/(.*)html /play.php?video=$1 break; 如果加上 if 语句,示例如下: if (!-f $request_filename) { rewrite ^/img/(.*)$ /site/$host/images/$1 last; }
  • 38. Nginx 与 Apache 的 Rewrite 规则实例对比① 简单的 Nginx 和 Apache 重写规则区别不大,基本上能够完全兼容。例如: Apache Rewrite 规则: RewriteRule ^/(mianshi|xianjing)/$ /zl/index.php?name=$1 [L] RewriteRule ^/ceshi/$ /zl/ceshi.php [L] RewriteRule ^/(mianshi)_([a-zA-Z]+)/$ /zl/index.php?name=$1_$2 [L] RewriteRule ^/pingce([0-9]*)/$ /zl/pingce.php?id=$1 [L] Nginx Rewrite 规则: rewrite ^/(mianshi|xianjing)/$ /zl/index.php?name=$1 last; rewrite ^/ceshi/$ /zl/ceshi.php last; rewrite ^/(mianshi)_([a-zA-Z]+)/$ /zl/index.php?name=$1_$2 last; rewrite ^/pingce([0-9]*)/$ /zl/pingce.php?id=$1 last; 由以上示例可以看出, Apache 的 Rewrite 规则改为 Nginx 的 Rewrite 规则,其实很简单: Apache 的 RewriteRule 指令换成 Nginx 的 rewrite 指令, Apache 的 [L] 标记换成 Nginx 的 last 标记,中间的内容不变。
  • 39. Nginx 与 Apache 的 Rewrite 规则实例对比② 如果 Apache 的 Rewrite 规则改为 Nginx 的 Rewrite 规则后,使用 nginx -t 命令检查发现 nginx.conf 配置文件有语法错误,那么可以尝试给条件加上引号。例如一下的 Nginx Rewrite 规则会报语法错误: rewrite ^/([0-9]{5}).html$ /x.jsp?id=$1 last; 加上引号就正确了: rewrite "^/([0-9]{5}).html$" /x.jsp?id=$1 last;
  • 40. Nginx 与 Apache 的 Rewrite 规则实例对比③ Apache 与 Nginx 的 Rewrite 规则在 URL 跳转时有细微的区别: Apache Rewrite 规则: RewriteRule ^/html/tagindex/([a-zA-Z]+)/.*$ /$1/ [R=301,L] Nginx Rewrite 规则: rewrite ^/html/tagindex/([a-zA-Z]+)/.*$ http://$host/$1/ permanent; 以上示例中,我们注意到, Nginx Rewrite 规则的置换串中增加了“ http://$host” ,这是在 Nginx 中要求的。
  • 41. Nginx 与 Apache 的 Rewrite 规则实例对比④ 另外, Apache 与 Nginx 的 Rewrite 规则在变量名称方面也有区别,例如: Apache Rewrite 规则: RewriteRule ^/user/login/$ /user/login.php?login=1&forward=http://%{HTTP_HOST} [L] Nginx Rewrite 规则: rewrite ^/user/login/$ /user/login.php?login=1&forward=http://$host last;
  • 42. Nginx 与 Apache 的 Rewrite 规则实例对比⑤ Apache 与 Nginx Rewrite 规则的一些功能相同或类似的指令、标记对应关系: Apache 的 RewriteCond 指令对应 Nginx 的 if 指令; Apache 的 RewriteRule 指令对应 Nginx 的 rewrite 指令; Apache 的 [R] 标记对应 Nginx 的 redirect 标记; Apache 的 [P] 标记对应 Nginx 的 last 标记; Apache 的 [R,L] 标记对应 Nginx 的 redirect 标记; Apache 的 [P,L] 标记对应 Nginx 的 last 标记; Apache 的 [PT,L] 标记对应 Nginx 的 last 标记;
  • 43. Nginx 与 Apache 的多条件 Rewrite 示例① 允许指定的域名访问本站,其他域名一律跳转到 http://www.aaa.com Apache Rewrite 规则: RewriteCond %{HTTP_HOST} ^(.*?)domaincom$ RewriteCond %{HTTP_HOST} !^qitadomaincom$ RewriteCond %{DOCUMENT_ROOT}/market/%1/index.htm -f RewriteRule ^/wu/$ /market/%1/index.htm [L] Nginx 的 if 指令不支持嵌套,也不支持 AND 、 OR 等多条件匹配,相比于 Apache 的 RewriteCond ,显得麻烦一些,但是,我们可以通过 下一页 的 Nginx 配置写法来实现这个示例:
  • 44. Nginx 与 Apache 的多条件 Rewrite 示例② Nginx Rewrite 规则: if ($host ~* ^(.*?)domaincom$) { set $var_wupin_city $1; set $var_wupin '1'; } if ($host ~* ^qitadomaincom$) { set $var_wupin '0'; } if (!-f $document_root/market/$var_wupin_city/index.htm) { set $var_wupin '0'; } if ($var_wupin ~ '1') { rewrite ^/wu/$ /market/$var_wupin_city/index.htm last; }
  • 46.
  • 47.
  • 48.  
  • 49.
  • 50.
  • 51.
  • 52.