利用Nginx替代apache实现高性能的Web环境_第1页
利用Nginx替代apache实现高性能的Web环境_第2页
利用Nginx替代apache实现高性能的Web环境_第3页
利用Nginx替代apache实现高性能的Web环境_第4页
利用Nginx替代apache实现高性能的Web环境_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、利用Nginx替彳apache实现高性能的Web环境利用Nginx替彳apache实现高性能的Web环境Nginx介绍:Nginx发音为enginex,是由俄罗斯人IgorSysoev建立的项目,基于BSD许可。据说他当初是F5的成员之一,英文主页:。俄罗斯的一些大网站已经使用它超过两年多了,一直表现不凡,相信想了解nginx的朋友都读过阿叶大哥的利用nginx实现负载均衡的文章相关链接见(六)。测试环境:红动中国(redocn)提供运营服务器环境.关于红动服务环境:红动中国在早期利用apache环境,再加上一些优化的工作,一直是相对很稳定,但是最近由于网站发展,访问量越来越大,在线人数一多经

2、常出现,负载过高,性能急剧下降,经过双木站长的同意,考虑是否能利用nginx来代替apache,经过长时间的观察目前nginx工作很稳定,系统也不会再说现高负载的状况,占用内存也很低,访问速率从用户体验来看明显有提升.关于红动中国:红动中国(redocn)论坛经过近1年的快速发展,目前日均页面访问量超过100万,位居全球设计论坛(中文)第1位,是国内最具影响力的设计论坛之一。目前论坛拥有近20万会员,包括众多设计界领军人物在内的行业中坚力量、相关艺术院校师生以及部分设计爱好者等。迁移目标:实现网站论坛静态化,防盗链,下载并发数和速率限制,实现原站apache所具有的所有功能,将原apache环

3、境下的站点全部迁移到Nginx一.PHP(Fastcgi)编译安装rootattphp-5.2.4#catin.sh./configure-prefix=/usr/local/php-fcgi-enable-fastcgi-enable-discard-path-enable-force-cgi-redirectjwith-config-file-path=/usr/local/php-fcgi/etc-enable-zend-multibytewith-mysql=/usr/local/mysqlwith-libxml-dir=/usr/local/libxml2with-gd=/usr/l

4、ocal/gd2mith-jpeg-dirmith-png-dirmith-bz2with-freetype-dirmith-iconv-dirmith-zlib-dirmith-openssH/usr/local/opensslwith-mcrypt=/usr/local/libmcrypt-enable-sysvsem-enable-inline-optimization-enable-soap-enable-gd-native-ttA-enable-ftp-enable-mbstringenable-exif-disable-debug-disable-ipv6makemakeinsta

5、llcpphp.ini-dist/usr/local/php-fcgi/etc/php.ini复制代码注:关于如何安装gd库,mysql的编译安装,本文将不介绍,本文重点在于介绍nginx的安装与配置,如想了解其它相关的问题可以到LinuxPk去找相关的贴子( HYPERLINK )二.Nginx编译安装1.创建nginx运行用户和虚拟主机目录groupaddwww-g48useradd-u48-gwwwwwwmkdir-p/data/www/wwwrootchown-Rwww:www/data/www/wwwroot复制代码2.安装lighttpd中附带的spawn-fcgi,用来启动php

6、-cgi先编译安装lighttpd产生spawn-fcgi二进制文件.cd/usr/local/src/lighttpd-1.4.18 HYPERLINK / /cpsrc/spawn-fcgi/usr/local/php-fcgi/bin/复制代码启动php-cgi进程,监听的8085端口,进程数为250(如果服务器内存小于可以只开启25个进程),用户为www:/usr/local/php-fcgi/bin/spawn-fcgi-a-p8085-C250-uwww-f/usr/local/php-fcgi/bin/php-cgi3.nginx的安装与配置安装Nginx所需的pcre库: HY

7、PERLINK /exim/pcre/pcre-7.3.tar.gz /exim/pcre/pcre-7.3.tar.gztarzxvfpcre-7.2.tar.gzcdpcre-7.2/./configuremake&makeinstallcd./ HYPERLINK http:/sysoev.ru/nginx/nginx-0.5.32.tar.gz http:/sysoev.ru/nginx/nginx-0.5.32.tar.gztarzxvfnginx-0.5.32.tar.gzcdnginx-0.5.32./configureuser=www-group=www-prefix=/usr

8、/local/nginx/with-http_stub_status_moduleth-openssl=/usr/local/opensslmake&makeinstall复制代码此模块非核心模块,需要在编译的时候手动添加编译参数-with-http_stub_status_moduleI,:fIlli配置nginx三.Nginx主配置文件及PHP支持.1.nginx.conf主配置文件的配置#cd/usr/local/nginx/conf/#cpnginx.confnginx.conf.cao#cat/dev/nullnginx.conf3GB ,wi#vinginx.conf/主配置文件u

9、serwwwwww;worker_processes10;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;pid/var/run/nginx.pid;#Specifiesthevalueformaximumfiledescriptorsthatcanbeopenedbythisprocess.worker_rlimit_nofile51200;eventsuseepoll;#maxclient=worker_processes*worker_connections/cpu_

10、numberworker_connections51200;httpincludeconf/mime.types;default_typeapplication/octet-stream;log_formatmain$remote_add-r$remote_user$time_local$request$status$body_bytes_sent$http_referer$http_user_agent$http_x_forwarded_for;#access_log/data/www/logs/access.logmain;#sendfileon;tcp_nopushon;tcp_node

11、layoff;keepalive_timeout60;client_header_timeout3m;client_body_timeout3m;send_timeout3m;connection_pool_size256;client_header_buffer_size1k;large_client_header_buffers42k;request_pool_size4k;output_buffers432k;postpone_output1460;client_max_body_size10m;client_body_buffer_size256k;client_body_temp_p

12、ath/dev/shm/client_body_temp;proxy_temp_path/usr/local/nginx/proxy_temp;fastcgi_temp_path/usr/local/nginx/fastcgi_temp;#gzipgzipon;gzip_http_version1.0;gzip_comp_level2;gzip_proxiedany;gzip_typestext/plaintext/htmltext/cssapplication/x-javascripttext/xmlapplication/xmlapplication/xmlrsstext/javascri

13、pt;gzip_min_length1100;gzip_buffers48k;#Thefollowingincludesarespecifiedforvirtualhosts/以下是加载虚拟主机配置.# HYPERLINK includeconf/vhosts/www_redocn_com.conf;#includeconf/vhosts/bbs_redocn_com.conf;#includeconf/vhosts/blog_redocn_com.conf;#includeconf/vhosts/down_redocn_com.conf;复制代码2.配置支持Fastcgi模式的PHProot

14、redocnconf#catenable_php5.conffastcgi_pass:8085;fastcgi_indexindex.php;fastcgi_paramGATEWAY_INTERFACECGI/1.1;fastcgi_paramSERVER_SOFTWAREnginx;#newacupload#fastcgi_pass_request_bodyoff;#client_body_in_file_onlyclean;#fastcgi_paramREQUEST_BODY_FILE$request_body_file;#fastcgi_paramQUERY_STRING$query_s

15、tring;fastcgi_paramREQUEST_METHOD$request_method;fastcgi_paramCONTENT_TYPE$content_type;fastcgi_paramCONTENT_LENGTH$content_length;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;fastcgi_paramSCRIPT_NAME$fastcgi_script_name;fastcgi_paramREQUEST_URI$request_uri;fastcgi_paramDOCUMENT_UR

16、I$document_uri;fastcgi_paramDOCUMENT_ROOT$document_root;fastcgi_paramSERVER_PROTOCOL$server_protocol;fastcgi_paramREMOTE_ADDR$remote_addr;fastcgi_paramREMOTE_PORT$remote_port;fastcgi_paramSERVER_ADDR$server_addr;fastcgi_paramSERVER_PORT$server_port;fastcgi_paramSERVER_NAME$server_name;#PHPonly,requi

17、redifPHPwasbuiltwith-enable-force-cgi-redirectfastcgi_paramREDIRECT_STATUS200;复制代码四,多虚拟主机应用配置案例.#mkdir/usr/local/nginx/conf/vhosts/建立虚拟主机配置存放目录1./首站配置rootredocnvhosts#viwww_redocn_com.confserverlisten80;server_;indexindex.htmlindex.htmindex.php;root/data/www/wwwroot;error_page404 HYPERLINK ;rewriteA

18、/bbs/(.*) HYPERLINK / /$1;location.*.php?$includeconf/enable_php5.conf;复制代码注:关于rewite需求,红动中国希望当用户访问 HYPERLINK /bbs /bbs的时候自动转至 HYPERLINK 在原apache中利用redirect实现Redirect/bbs HYPERLINK 本文中在nginx下利用rewrite实现:rewriteA/bbs/(.*) HYPERLINK / /$1;2.rootredocnvhostsvibbs_redocn_com.confserverlisten80;server_;i

19、ndexindex.htmlindex.htmindex.php;root/home/www/htdocs/bbs;access_log/var/log/nginx/access_.logcombined;location/#bbsrewriterewriteA/archiver/(fid|tid)-w-.html)$/archiver/index.php?$1last;rewriteA/forum-(0-9)-(0-9).html$/forumdisplay.php?fid=$1&page=$2last;rewriteA/thread-(0-9)-(0-9)-(0-9).html$/view

20、thread.php?tid=$1&extra=page=$3&page=$2last;rewriteA/space-(username|uid)-(.).html$/space.php?$1=$2last;rewriteA/tag-(.).html$/tag.php?name=$1last;break;#errorerror_page404/index.php;#redirectservererrorpagestothestaticpage/50 x.htmlerror_page500502503504/50 x.html;location=/50 x.htmlroothtml;#Preve

21、ntinghotlinkingofimagesandotherfiletypeslocation*A.(gif|jpg|png|swf|flv|rar|zip)$valid_referersnoneblockedserver_names*.*.*.*.*.*.*.cn*.;if($invalid_referer)rewrite/ HYPERLINK /images/redocn.gif /images/redocn.gif;#return403;#supportphplocation.*.php?$includeconf/enable_php5.conf;复制代码注:红动中国采用高性能的Dis

22、cuz!论坛,原apache的rewrite规则几乎不要做什么修改即可全部移植到nginx下.静态化配置见面上面的:#bbsrewrite部分.一般论坛都希望实现防盗链功能,在apache很轻松实现?在nginx下是否容易实现呢?答案是肯定的.#Preventinghotlinkingofimagesandotherfiletypesvalid_referersnoneblockedserver_names*.你允许连接的网址if($invalid_referer)rewrite/ HYPERLINK /images/redocn.gif /images/redocn.gif;/让另II人盗链

23、时显示你指定的图片.#return403;复制代码3.rootredocnvhosts#viblog_redocn_com.confserverlisten80;server_;indexindex.htmlindex.htmindex.php;root/data/www/wwwroot/blog;error_page404 HYPERLINK ;#supsiterewriterewriteA(0-9)/spacelist(.*)$index.php?$1/action_spacelist$2;rewriteA(0-9)/viewspace_(.)$index.php?$1/action_vi

24、ewspace_itemid_$2;rewriteA(0-9)/viewbbs_(.)$index.php?$1/action_viewbbs_tid_$2;rewriteA(0-9)/(.*)$index.php?$1/$2;rewriteA(0-9)$index.php?$1;rewriteAaction_(.)$index.php?action_$1;rewriteAcategory_(.)$index.php?action_category_catid_$1;rewriteAitemlist_(.)$index.php?action_itemlist_catid_$1;rewriteA

25、viewnews_(.)$index.php?action_viewnews_itemid_$1;rewriteAviewthread_(.)$index.php?action_viewthread_tid_$1;rewriteAindex(.a-zA-Z0-9*)$index.php;rewriteAhtml/(0-9)/viewnews_itemid_(0-9).html$index.php?action_viewnews_itemid_$2;rewriteA/(0-9)/spacelist(.)$/index.php?uid/$1/action/spacelist/type$2;rewr

26、iteA/(0-9)/viewspace(.)$/index.php?uid/$1/action/viewspace/itemid$2;rewriteA/(0-9)/viewbbs(.)$/index.php?uid/$1/action/viewbbs/tid$2;rewriteA/(0-9)/(.*)$/index.php?uid/$1/$2;rewriteA/(0-9)$/index.php?uid/$1;rewriteA/action(.)$/index.php?action$1;rewriteA/category(.)$/index.php?action/category/catid$

27、1;rewriteA/viewnews(.)$/index.php?action/viewnews/itemid$1;rewriteA/viewthread(.)$/index.php?action/viewthread/tid$1;rewriteA/mygroup(.)$/index.php?action/mygroup/gid$1;location.*.php?$includeconf/enable_php5.conf;复制代码注:blog采用功能强大的Supesite作为Blog站点:/1.Blog如何在Nginx里实现静态化,具体设置见,上面的#supesiterewrite.root

28、redocnvhosts#vidown_redocn_com.conflimit_zoneone$binary_remote_addr10m;serverlisten80;server_;indexindex.htmlindex.htmindex.php;root/data/www/wwwroot/down;error_page404/index.php;#redirectservererrorpagestothestaticpage/50 x.htmlerror_page500502503504/50 x.html;location=/50 x.htmlroothtml;#Zonelimitlocation/limit_connone1;limit_rate20k;#servestaticfileslocationA/(images|javascript|js|css|flash|media|stat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论