热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

利用Nginx替代apache实现高性能的Web环境

作者:NetSeek欢迎转载,转载请注明出处:bbs.linuxpk.com原文链接:bbs.linuxpk.comthread-11845-1-1.htmlNginx介绍:Nginx发音为[enginex],是由俄罗斯人IgorSysoev建立的项目,基于BSD许可。据说他当初是F5的成员之一,英文主页:http
作者:NetSeek
欢迎转载,转载请注明出处: http://bbs.linuxpk.com
原文链接:http://bbs.linuxpk.com/thread-11845-1-1.html

Nginx介绍:
Nginx发音为[engine x],是由俄罗斯人Igor Sysoev建立的项目,基于BSD许可。
据说他当初是F5的成员之一,英文主页:http://nginx.net。俄罗斯的一些大网站已经使用它超过两年多了, 一直表现不凡,相信想了解nginx的朋友都读过阿叶大哥的利用nginx实现负载均衡的文章相关链接见(六)。

测试环境:红动中国(redocn)提供运营服务器环境.

关于红动服务环境:
红动中国在早期利用apache环境,再加上一些优化的工作,一直是相对很稳定,但是最近由于网站发展,访问量越来越大,在线人数一多经常出现,负载过高,性能急剧下降,经过双木站长的同意,考虑是否能利用nginx来代替apache,经过长时间的观察目前nginx工作很稳定,系统也不会再说现高负载的状况,占用内存也很低,访问速率从用户体验来看明显有提升.

关于红动中国:
红动中国(redocn)论坛经过近1年的快速发展,目前日均页面访问量超过100万,位居全球设计论坛(中文)第1位,是国内最具影响力的设计论坛之一。目前论坛拥有近20万会员,包括众多设计界领军人物在内的行业中坚力量、相关艺术院校师生以及部分设计爱好者等。

迁移目标:实现网站论坛静态化,防盗链,下载并发数和速率限制,实现原站apache所具有的所有功能,将原apache环境下的站点全部迁移到Nginx

一.php(Fastcgi)编译安装
[root@att php-5.2.4]# cat in.sh./configure \

  –PRefix=/usr/local/php-fcgi \

  –enable-fastcgi \

  –enable-discard-path \

  –enable-force-cgi-redirect \

  –with-config-file-path=/usr/local/php-fcgi/etc \

  –enable-zend-multibyte \

  –with-MySQL=/usr/local/mysql \

  –with-libxml-dir=/usr/local/libxml2 \

  –with-gd=/usr/local/gd2 \

  –with-jpeg-dir \

  –with-png-dir \

  –with-bz2 \

  –with-freetype-dir \

  –with-iconv-dir \

  –with-zlib-dir \

  –with-openssl=/usr/local/openssl \

  –with-mcrypt=/usr/local/libmcrypt \

  –enable-sysvsem \

  –enable-inline-optimization \

  –enable-soap \

  –enable-gd-native-ttf \

  –enable-ftp \

  –enable-mbstring \

  –enable-exif \

  –disable-debug \

  –disable-ipv6

make

make install

cp php.ini-dist /usr/local/php-fcgi/etc/php.ini
注:关于如何安装gd库,mysql的编译安装,本文将不介绍,本文重点在于介绍nginx的安装与配置,如想了解其它相关的问题可以到
LinuxPk去找相关的贴子(http://bbs.linuxpk.com)

二.Nginx编译安装
1.创建nginx运行用户和虚拟主机目录groupadd www -g 48

useradd -u 48 -g www www

mkdir -p /data/www/wwwroot

chown -R www:www /data/www/wwwroot
2.安装lighttpd中附带的spawn-fcgi,用来启动php-cgi
先编译安装lighttpd产生spawn-fcgi二进制文件.cd /usr/local/src/lighttpd-1.4.18
http://www.cncms.com/
cp src/spawn-fcgi /usr/local/php-fcgi/bin/
启动php-cgi进程,监听127.0.0.1的8085端口,进程数为250(如果服务器内存小于3GB,可以只开启25个进程),用户为www:
/usr/local/php-fcgi/bin/spawn-fcgi -a 127.0.0.1 -p 8085 -C 250 -u www -f /usr/local/php-fcgi/bin/php-cgi

3.nginx的安装与配置
安装Nginx所需的pcre库:
http://ftp.dk.debian.org/exim/pcre/pcre-7.3.tar.gz tar zxvf pcre-7.2.tar.gz

cd pcre-7.2/

./configure

make && make install

cd ../

http://sysoev.ru/nginx/nginx-0.5.32.tar.gz

tar zxvf nginx-0.5.32.tar.gz

cd nginx-0.5.32

./configure –user=www –group=www –prefix=/usr/local/nginx/ –with-http_stub_status_module –with-

openssl=/usr/local/openssl

make && make install
此模块非核心模块,需要在编译的时候手动添加编译参数 –with-http_stub_status_module
配置nginx

三.Nginx主配置文件及PHP支持.

1.nginx.conf 主配置文件的配置
#cd /usr/local/nginx/conf/
#cp nginx.conf nginx.conf.cao
#cat /dev/null > nginx.conf
#vi nginx.conf    //主配置文件user www www;

worker_processes 10;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

pid    /var/run/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 51200;

events

{

  use epoll;

  #maxclient = worker_processes * worker_connections / cpu_number

  worker_connections 51200;

}

http

{

  include    conf/mime.types;

  default_type application/octet-stream;

  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 /data/www/logs/access.log main;

  #sendfile on;

  tcp_nopush   on;

  tcp_nodelay  off;

  keepalive_timeout 60;

 

  client_header_timeout 3m;

  client_body_timeout  3m;

  send_timeout      3m;

  connection_pool_size    256;

  client_header_buffer_size  1k;

  large_client_header_buffers  4 2k;

  request_pool_size    4k;

  output_buffers  4 32k;

  postpone_output 1460;

  client_max_body_size    10m;

  client_body_buffer_size  256k;

  client_body_temp_path /dev/shm/client_body_temp;

  proxy_temp_path      /usr/local/nginx/proxy_temp;

  fastcgi_temp_path     /usr/local/nginx/fastcgi_temp;

 

  #gzip

  gzip on;

  gzip_http_version 1.0;

  gzip_comp_level 2;

  gzip_proxied any;

  gzip_types text/plain text/html text/CSS application/x-Javascript text/xml application/xml application/xml+rss text/Javascript;

  gzip_min_length 1100;

  gzip_buffers 4 8k;

  # The following includes are specified for virtual hosts  //以下是加载虚拟主机配置.

  #www.redocn.com

  include     conf/vhosts/www_redocn_com.conf;

  #bbs.redocn.com

  include     conf/vhosts/bbs_redocn_com.conf;

  #blog.redocn.com

  include     conf/vhosts/blog_redocn_com.conf;

  #down.redocn.com

  include     conf/vhosts/down_redocn_com.conf;

}
2.配置支持Fastcgi模式的PHP
[root@redocn conf]# cat enable_php5.conffastcgi_pass 127.0.0.1:8085;

fastcgi_index index.php;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE  nginx;

#new ac upload

#fastcgi_pass_request_body off;

#client_body_in_file_only clean;

#fastcgi_param REQUEST_BODY_FILE $request_body_file;

#

fastcgi_param QUERY_STRING    $query_string;

fastcgi_param REQUEST_METHOD   $request_method;

fastcgi_param CONTENT_TYPE    $content_type;

fastcgi_param CONTENT_LENGTH   $content_length;

fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME    $fastcgi_script_name;

fastcgi_param REQUEST_URI    $request_uri;

fastcgi_param DOCUMENT_URI    $document_uri;

fastcgi_param DOCUMENT_ROOT   $document_root;

fastcgi_param SERVER_PROTOCOL  $server_protocol;

fastcgi_param REMOTE_ADDR    $remote_addr;

fastcgi_param REMOTE_PORT    $remote_port;

fastcgi_param SERVER_ADDR    $server_addr;

fastcgi_param SERVER_PORT    $server_port;

fastcgi_param SERVER_NAME    $server_name;

# PHP only, required if PHP was built with –enable-force-cgi-redirect

fastcgi_param REDIRECT_STATUS  200;
四,多虚拟主机应用配置案例.

#mkdir /usr/local/nginx/conf/vhosts     //建立虚拟主机配置存放目录.
1.www.redocn.com              //首站配置
[root@redocn vhosts]#vi www_redocn_com.confserver

  {

      listen    80;

      server_name www.redocn.com;

      index index.html index.htm index.php;

      root /data/www/wwwroot;

      error_page 404 http://bbs.redocn.com;

      rewrite ^/bbs/(.*) http://bbs.redocn.com/$1;

      location ~ .*\.php?$

      {

          include conf/enable_php5.conf;

      }

  }
注: 关于rewite需求,红动中国希望当用户访问http://www.redocn.com/bbs的时候自动转至http://bbs.redocn.com
在原apache中利用redirect实现
Redirect /bbs http://bbs.redocn.com

本文中在nginx下利用rewrite实现:
rewrite ^/bbs/(.*) http://bbs.redocn.com/$1;
2.[root@redocn vhosts] vi bbs_redocn_com.confserver

  {

      listen    80;

      server_name bbs.redocn.com yan.redocn.com majia.redocn.com wt.redocn.com;

      index index.html index.htm index.php;

      root /home/www/htdocs/bbs;

      access_log /var/log/nginx/access_bbs.redocn.com.log combined;

      location / {

      #bbs rewrite

           rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$  /archiver/index.php?$1 last;

           rewrite ^/forum-([0-9]+)-([0-9]+)\.html$  /forumdisplay.php?fid=$1&page=$2 last;

           rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;

           rewrite ^/space-(username|uid)-(.+)\.html$  /space.php?$1=$2 last;

           rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;

           break;

      #error

      error_page 404 /index.php;

      #redirect server error pages to the static page /50x.html

      error_page  500 502 503 504 /50x.html;

      location = /50x.html {

      root  html;

       }

           }

      #Preventing hot linking of images and other file types

      location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {

          valid_referers none blocked server_names

             *.redocn.com redocn.com *.taobao.com taobao.com

             bbs.blueidea.com bbs.asiaci.com bbs.arting365.com forum.chinavisual.com softbbs.pconline.com.cn

             bbs.chinaddu.com bbs.photops.com *.baidu.com *.google.com *.google.cn *.soso.com *.yahoo.com.cn

             *.yahoo.cn;

      if ($invalid_referer) {

      rewrite  ^/  http://www.redocn.com/images/redocn.gif;

      #return  403;

                 }

  }

 

      #support php

      location ~ .*\.php?$

      {

          include conf/enable_php5.conf;

      }

 

  }
注:
1.红动中国采用高性能的Discuz!论坛,原apache的rewrite规则几乎不要做什么修改即可全部移植到nginx下.
静态化配置见面上面的:#bbs rewrite部分.
2.一般论坛都希望实现防盗链功能,在apache很轻松实现?在nginx下是否容易实现呢?答案是肯定的.     #Preventing hot linking of images and other file types

         valid_referers none blocked server_names *.redocn.com redocn.com …你允许连接的网址;

      if ($invalid_referer) {

      rewrite  ^/  http://www.redocn.com/images/redocn.gif; //让别人盗链时显示你指定的图片.

      #return  403;

                 }
3.blog.redocn.com
[root@redocn vhosts]#vi blog_redocn_com.confserver

  {

      listen    80;

      server_name blog.redocn.com;

      index index.html index.htm index.php;

      root /data/www/wwwroot/blog;

      error_page 404 http://bbs.redocn.com;

      #supsite rewrite

      rewrite ^([0-9]+)/spacelist(.*)$ index.php?$1/action_spacelist$2;

      rewrite ^([0-9]+)/viewspace_(.+)$ index.php?$1/action_viewspace_itemid_$2;

      rewrite ^([0-9]+)/viewbbs_(.+)$ index.php?$1/action_viewbbs_tid_$2;

      rewrite ^([0-9]+)/(.*)$ index.php?$1/$2;

      rewrite ^([0-9]+)$ index.php?$1;

      rewrite ^action_(.+)$ index.php?action_$1;

      rewrite ^category_(.+)$ index.php?action_category_catid_$1;

      rewrite ^itemlist_(.+)$ index.php?action_itemlist_catid_$1;

      rewrite ^viewnews_(.+)$ index.php?action_viewnews_itemid_$1;

      rewrite ^viewthread_(.+)$ index.php?action_viewthread_tid_$1;

      rewrite ^index([\.a-zA-Z0-9]*)$ index.php;

      rewrite ^html/([0-9]+)/viewnews_itemid_([0-9]+)\.html$ index.php?action_viewnews_itemid_$2;

      rewrite ^/([0-9]+)/spacelist(.+)$ /index.php?uid/$1/action/spacelist/type$2;

      rewrite ^/([0-9]+)/viewspace(.+)$ /index.php?uid/$1/action/viewspace/itemid$2;

      rewrite ^/([0-9]+)/viewbbs(.+)$ /index.php?uid/$1/action/viewbbs/tid$2;

      rewrite ^/([0-9]+)/(.*)$ /index.php?uid/$1/$2;

      rewrite ^/([0-9]+)$ /index.php?uid/$1;

      rewrite ^/action(.+)$ /index.php?action$1;

      rewrite ^/category(.+)$ /index.php?action/category/catid$1;

      rewrite ^/viewnews(.+)$ /index.php?action/viewnews/itemid$1;

      rewrite ^/viewthread(.+)$ /index.php?action/viewthread/tid$1;

      rewrite ^/mygroup(.+)$ /index.php?action/mygroup/gid$1;

      location ~ .*\.php?$

      {

          include conf/enable_php5.conf;

      }

  }
注:blog采用功能强大的Supesite作为Blog站点: http://www.supesite.com/
1.Blog如何在Nginx里实现静态化,具体设置见,上面的#supesite rewrite

4.down.redocn.com
[root@redocn vhosts]# vi down_redocn_com.conflimit_zone  one $binary_remote_addr 10m;

server

  {

      listen    80;

      server_name down.redocn.com;

      index index.html index.htm index.php;

      root  /data/www/wwwroot/down;

      error_page 404 /index.php;

      # redirect server error pages to the static page /50x.html

      error_page  500 502 503 504 /50x.html;

      location = /50x.html {

      root  html;

       }

      #Zone limit

      location / {

        limit_conn  one 1;

        limit_rate 20k;

      }

    

      # serve static files

      location ~ ^/(images|Javascript|js|css|Flash|media|static)/ {

      root  /data/www/wwwroot/down;

      expires 30d;

      }

  }
注:
由于现在的BT下载软件越来越多了,我们如何限制下载的并发数和速率呢?apache需要三方模块,nginx就不用了
在nginx利用两个指令即可实现:limit_zone(limit_conn) 来限制并发数,limit_rate来限制下载的速率,请看上面的配置实例.

5.启动nginx服务/usr/local/php-fcgi/bin/spawn-fcgi -a 127.0.0.1 -p 8085 -C 250 -u www -f/usr/local/php-fcgi/bin/php-cgi

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
你可以把上面两条命令制做成系统启动服务脚本,相关的脚本在网上也很多,本文就不再贴出来了,给出一个实例链接:
http://topfunky.net/svn/shovel/nginx/init.d/nginx

五.问题及经验总结:
1.安装Discuz论坛后,无法上传大于M以上的附件?
在主配置文件里加入:client_max_body_size    10m; 详细指令说明请参见(六)提供的Wiki链接.

2.Discuz附件无法下载附件?
最近遇到一个奇怪的问题在nginx下discuz论坛无法下载附件,后来打开error_log才知道,仍后一看/usr/local分区满了,
清了一大堆无用文件后,马上就正常了.

以上是本人迁移和测试过程中遇到的两个小问题,在此附上说明,只是希望配置nginx的朋友不要犯我一样的问题.
欢迎联系NetSeek(狂热linux爱好者^_^ msn:cnseek@msn.com QQ:67888954 Gtalk:cnseek@Gmail.com).

六.相关链接:
1.Discuz!.net高性能的PHP论坛程序 http://www.discuz.net Supesite: http://www.supesite.com/
2.Nginx参考文档:http://wiki.codemongers.com/
3.利用Nginx实现负载均衡(阿叶大哥的文章):http://www.imysql.com/comment/reply/210
4.linuxPk[Linux宝库]:http://bbs.linuxpk.com
5.红动中国 http://bbs.redocn.com


推荐阅读
  • CEPH LIO iSCSI Gateway及其使用参考文档
    本文介绍了CEPH LIO iSCSI Gateway以及使用该网关的参考文档,包括Ceph Block Device、CEPH ISCSI GATEWAY、USING AN ISCSI GATEWAY等。同时提供了多个参考链接,详细介绍了CEPH LIO iSCSI Gateway的配置和使用方法。 ... [详细]
  • intellij idea的安装与使用(保姆级教程)
    intellijidea的安装与使用(保姆级教程)IntelliJ在业界被公认为最好的java开发工具,尤其在智能代码助手、代码自动提示、重构、JavaEE支持、各类版本工具(gi ... [详细]
  • Java源代码安全审计(二):使用Fortify-sca工具进行maven项目安全审计
    本文介绍了使用Fortify-sca工具对maven项目进行安全审计的过程。作者通过对Fortify的研究和实践,记录了解决问题的学习过程。文章详细介绍了maven项目的处理流程,包括clean、build、Analyze和Report。在安装mvn后,作者遇到了一些错误,并通过Google和Stack Overflow等资源找到了解决方法。作者分享了将一段代码添加到pom.xml中的经验,并成功进行了mvn install。 ... [详细]
  • Maven入门、什么是Maven、如何使用Maven、Maven的项目结构、简单的Mavenjava项目、Maven常用命令、Maven项目之间的引用、Maven依赖的传递、可选、排除day01
    目录第一节Maven入门1.1什么是Maven1.2如何使用Maven第一步:下载Maven第二步:配置Maven的环境变量第三步:了解什 ... [详细]
  • 如何查看电脑系统版本_腾讯云服务器系统版本怎么看?Windows和Centos版本怎么选?...
    腾讯云服务器系统版本怎么看?想要知道自己的腾讯云服务器系统版本是哪个,可以登录云服务器后台管理系统查看,或者使用命令行查询,如果不会操作& ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • IT方面的论坛太多了,有综合,有专业,有行业,在各个论坛里混了几年,体会颇深,以前是论坛哪里人多 ... [详细]
  • Jquery 跨域问题
    为什么80%的码农都做不了架构师?JQuery1.2后getJSON方法支持跨域读取json数据,原理是利用一个叫做jsonp的概念。当然 ... [详细]
  • 本文介绍了在Ubuntu系统中清理残余配置文件和无用内容的方法,包括清理残余配置文件、清理下载缓存包、清理不再需要的包、清理无用的语言文件和清理无用的翻译内容。通过这些清理操作可以节省硬盘空间,提高系统的运行效率。 ... [详细]
  • 1jdk去网站下载,然后拷贝到linux上;或直接wgethttp:download.oracle.comotn-pubjavajdk8u181-b1 ... [详细]
  • php网站设计实验报告,php网站开发实训报告
    本文目录一览:1、php动态网站设计的关键技术有哪些软件,及搭建步骤需要哪些页面,分别完成 ... [详细]
  • 转瞬即是2015,是该总结一下了。----------------------------------------2014大事件前往梦都跳槽外企脱离单身-------------- ... [详细]
author-avatar
小小小菜鸡
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有