• (转载)利用Nginx替代apache实现高性能的Web环境


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

    利用Nginx替代apache实现高性能的Web环境           
    更新时间:2007-11-27
    作者: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
    1. ./configure \
    2.         --prefix=/usr/local/php-fcgi \
    3.         --enable-fastcgi \
    4.         --enable-discard-path \
    5.         --enable-force-cgi-redirect \
    6.         --with-config-file-path=/usr/local/php-fcgi/etc \
    7.         --enable-zend-multibyte \
    8.         --with-mysql=/usr/local/mysql \
    9.         --with-libxml-dir=/usr/local/libxml2 \
    10.         --with-gd=/usr/local/gd2 \
    11.         --with-jpeg-dir \
    12.         --with-png-dir \
    13.         --with-bz2 \
    14.         --with-freetype-dir \
    15.         --with-iconv-dir \
    16.         --with-zlib-dir  \
    17.         --with-openssl=/usr/local/openssl \
    18.         --with-mcrypt=/usr/local/libmcrypt \
    19.         --enable-sysvsem \
    20.         --enable-inline-optimization \
    21.         --enable-soap \
    22.         --enable-gd-native-ttf \
    23.         --enable-ftp \
    24.         --enable-mbstring \
    25.         --enable-exif \
    26.         --disable-debug \
    27.         --disable-ipv6
    28.       
    29. make
    30. make install
    31. cp php.ini-dist /usr/local/php-fcgi/etc/php.ini
    复制代码
    注:关于如何安装gd库,mysql的编译安装,本 文将不介绍,本文重点在于介绍nginx的安装与配置,如想了解其它相关的问题可以到
    LinuxPk去找相关的贴子(http://bbs.linuxpk.com)

    二.Nginx编译安装
    1.创建nginx运行用户和虚拟主机目录
    1. groupadd www -g 48
    2. useradd -u 48 -g www www
    3. mkdir -p /data/www/wwwroot
    4. chown -R www:www /data/www/wwwroot
    复制代码
    2.安装lighttpd中附带的spawn- fcgi,用来启动php-cgi
    先编译安装lighttpd产生spawn-fcgi二进制文件.
    1. cd /usr/local/src/lighttpd-1.4.18
    2. 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
    1.   tar zxvf pcre-7.2.tar.gz
    2.   cd pcre-7.2/
    3.   ./configure
    4.   make && make install
    5.   cd ../

    6.   http://sysoev.ru/nginx/nginx-0.5.32.tar.gz
    7.   tar zxvf nginx-0.5.32.tar.gz
    8.   cd  nginx-0.5.32
    9. ./configure --user=www --group=www --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-

    10. openssl=/usr/local/openssl
    11. 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       //主配置文件
    1. user  www www;

    2. worker_processes 10;

    3. #error_log  logs/error.log;
    4. #error_log  logs/error.log  notice;
    5. #error_log  logs/error.log  info;

    6. pid        /var/run/nginx.pid;

    7. #Specifies the value for maximum file descriptors that can be opened by this process.
    8. worker_rlimit_nofile 51200;

    9. events
    10. {
    11.        use epoll;

    12.        #maxclient = worker_processes * worker_connections / cpu_number
    13.        worker_connections 51200;
    14. }

    15. http
    16. {
    17.        include       conf/mime.types;
    18.        default_type  application/octet-stream;

    19.        log_format  main  '$remote_addr - $remote_user [$time_local] $request '
    20.                          '"$status" $body_bytes_sent "$http_referer" '
    21.                          '"$http_user_agent" "$http_x_forwarded_for"';

    22.        #access_log  /data/www/logs/access.log  main;
    23.        #sendfile on;
    24.        tcp_nopush     on;
    25.        tcp_nodelay    off;

    26.        keepalive_timeout 60;
    27.    
    28.        client_header_timeout  3m;
    29.        client_body_timeout    3m;
    30.        send_timeout           3m;
    31.        connection_pool_size        256;
    32.        client_header_buffer_size    1k;
    33.        large_client_header_buffers    4 2k;
    34.        request_pool_size        4k;
    35.        output_buffers   4 32k;
    36.        postpone_output  1460;
    37.        client_max_body_size       10m;
    38.        client_body_buffer_size    256k;
    39.        client_body_temp_path /dev/shm/client_body_temp;
    40.        proxy_temp_path            /usr/local/nginx/proxy_temp;
    41.        fastcgi_temp_path          /usr/local/nginx/fastcgi_temp;
    42.      
    43.        #gzip
    44.        gzip on;
    45.        gzip_http_version 1.0;
    46.        gzip_comp_level 2;
    47.        gzip_proxied any;
    48.        gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    49.        gzip_min_length 1100;
    50.        gzip_buffers 4 8k;
    51.       
    52.        # The following includes are specified for virtual hosts   //以下是加载虚拟主机配置.
    53.        #www.redocn.com
    54.        include          conf/vhosts/www_redocn_com.conf;
    55.        #bbs.redocn.com
    56.        include          conf/vhosts/bbs_redocn_com.conf;
    57.        #blog.redocn.com
    58.        include          conf/vhosts/blog_redocn_com.conf;
    59.        #down.redocn.com
    60.        include          conf/vhosts/down_redocn_com.conf;
    61. }
    复制代码
    2. 配置支持Fastcgi模式的PHP
    [root@redocn conf]# cat enable_php5.conf
    1. fastcgi_pass  127.0.0.1:8085;
    2. fastcgi_index index.php;

    3. fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    4. fastcgi_param  SERVER_SOFTWARE    nginx;
    5. #new ac upload
    6. #fastcgi_pass_request_body off;
    7. #client_body_in_file_only clean;
    8. #fastcgi_param  REQUEST_BODY_FILE  $request_body_file;
    9. #

    10. fastcgi_param  QUERY_STRING       $query_string;
    11. fastcgi_param  REQUEST_METHOD     $request_method;
    12. fastcgi_param  CONTENT_TYPE       $content_type;
    13. fastcgi_param  CONTENT_LENGTH     $content_length;

    14. fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    15. fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    16. fastcgi_param  REQUEST_URI        $request_uri;
    17. fastcgi_param  DOCUMENT_URI       $document_uri;
    18. fastcgi_param  DOCUMENT_ROOT      $document_root;
    19. fastcgi_param  SERVER_PROTOCOL    $server_protocol;

    20. fastcgi_param  REMOTE_ADDR        $remote_addr;
    21. fastcgi_param  REMOTE_PORT        $remote_port;
    22. fastcgi_param  SERVER_ADDR        $server_addr;
    23. fastcgi_param  SERVER_PORT        $server_port;
    24. fastcgi_param  SERVER_NAME        $server_name;

    25. # PHP only, required if PHP was built with --enable-force-cgi-redirect
    26. fastcgi_param  REDIRECT_STATUS    200;
    复制代码
    四,多虚拟主机应用配置案例.

    #mkdir /usr/local/nginx/conf/vhosts         //建立虚拟主机配置存放目录.
    1.www.redocn.com                            //首站配置
    [root@redocn vhosts]#vi www_redocn_com.conf
    1. server
    2.        {
    3.                listen       80;
    4.                server_name  www.redocn.com;
    5.                index index.html index.htm index.php;
    6.                root  /data/www/wwwroot;
    7.                error_page 404 http://bbs.redocn.com;
    8.                rewrite ^/bbs/(.*) http://bbs.redocn.com/$1;
    9.                location ~ .*\.php?$
    10.                {
    11.                        include conf/enable_php5.conf;
    12.                }
    13.        }
    复制代 码
    注: 关于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.conf
    1. server
    2.        {
    3.                listen       80;
    4.                server_name  bbs.redocn.com yan.redocn.com majia.redocn.com wt.redocn.com;
    5.                index index.html index.htm index.php;
    6.                root  /home/www/htdocs/bbs;
    7.                access_log /var/log/nginx/access_bbs.redocn.com.log  combined;
    8.                location / {
    9.                #bbs rewrite
    10.                          rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$   /archiver/index.php?$1 last;
    11.                          rewrite ^/forum-([0-9]+)-([0-9]+)\.html$   /forumdisplay.php?fid=$1&page=$2 last;
    12.                          rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$  /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;
    13.                          rewrite ^/space-(username|uid)-(.+)\.html$   /space.php?$1=$2 last;
    14.                          rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
    15.                          break;
    16.                #error
    17.                error_page 404 /index.php;
    18.                #redirect server error pages to the static page /50x.html
    19.                error_page   500 502 503 504  /50x.html;
    20.                location = /50x.html {
    21.                 root   html;
    22.                   }
    23.                          }
    24.                #Preventing hot linking of images and other file types
    25.                location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
    26.                        valid_referers none blocked server_names
    27.                               *.redocn.com redocn.com *.taobao.com taobao.com
    28.                               bbs.blueidea.com bbs.asiaci.com bbs.arting365.com forum.chinavisual.com softbbs.pconline.com.cn
    29.                               bbs.chinaddu.com bbs.photops.com *.baidu.com *.google.com *.google.cn *.soso.com *.yahoo.com.cn
    30.                               *.yahoo.cn;
    31.                if ($invalid_referer) {
    32.                 rewrite   ^/   http://www.redocn.com/images/redocn.gif;
    33.                 #return   403;
    34.                                      }
    35.     }
    36.      
    37.                #support php
    38.                location ~ .*\.php?$
    39.                {
    40.                        include conf/enable_php5.conf;
    41.                }
    42.          
    43.        }
    复制代 码
    注:
    1.红动中国采用高性能的Discuz!论坛,原apache的rewrite规则几乎不要做什么修改即可全部移植到nginx下.
    静态化配置见面上面的:#bbs rewrite部分.
    2.一般论坛都希望实现防盗链功能,在apache很轻松实现?在nginx下是否容易实现呢?答案是肯定的.
    1.          #Preventing hot linking of images and other file types
    2.                      valid_referers none blocked server_names *.redocn.com redocn.com ...你允许连接的网址;
    3.                if ($invalid_referer) {
    4.                 rewrite   ^/   http://www.redocn.com/images/redocn.gif;  //让别人盗链时显示你指定的图片.
    5.                 #return   403;
    6.                                      }
    复制代码
    3.blog.redocn.com
    [root@redocn vhosts]#vi blog_redocn_com.conf
    1. server
    2.        {
    3.                listen       80;
    4.                server_name  blog.redocn.com;
    5.                index index.html index.htm index.php;
    6.                root  /data/www/wwwroot/blog;
    7.                error_page 404 http://bbs.redocn.com;
    8.                #supsite rewrite
    9.                rewrite ^([0-9]+)/spacelist(.*)$ index.php?$1/action_spacelist$2;
    10.                rewrite ^([0-9]+)/viewspace_(.+)$ index.php?$1/action_viewspace_itemid_$2;
    11.                rewrite ^([0-9]+)/viewbbs_(.+)$ index.php?$1/action_viewbbs_tid_$2;
    12.                rewrite ^([0-9]+)/(.*)$ index.php?$1/$2;
    13.                rewrite ^([0-9]+)$ index.php?$1;
    14.                rewrite ^action_(.+)$ index.php?action_$1;
    15.                rewrite ^category_(.+)$ index.php?action_category_catid_$1;
    16.                rewrite ^itemlist_(.+)$ index.php?action_itemlist_catid_$1;
    17.                rewrite ^viewnews_(.+)$ index.php?action_viewnews_itemid_$1;
    18.                rewrite ^viewthread_(.+)$ index.php?action_viewthread_tid_$1;
    19.                rewrite ^index([\.a-zA-Z0-9]*)$ index.php;
    20.                rewrite ^html/([0-9]+)/viewnews_itemid_([0-9]+)\.html$ index.php?action_viewnews_itemid_$2;


    21.                rewrite ^/([0-9]+)/spacelist(.+)$ /index.php?uid/$1/action/spacelist/type$2;
    22.                rewrite ^/([0-9]+)/viewspace(.+)$ /index.php?uid/$1/action/viewspace/itemid$2;
    23.                rewrite ^/([0-9]+)/viewbbs(.+)$ /index.php?uid/$1/action/viewbbs/tid$2;
    24.                rewrite ^/([0-9]+)/(.*)$ /index.php?uid/$1/$2;
    25.                rewrite ^/([0-9]+)$ /index.php?uid/$1;

    26.                rewrite ^/action(.+)$ /index.php?action$1;
    27.                rewrite ^/category(.+)$ /index.php?action/category/catid$1;
    28.                rewrite ^/viewnews(.+)$ /index.php?action/viewnews/itemid$1;
    29.                rewrite ^/viewthread(.+)$ /index.php?action/viewthread/tid$1;
    30.                rewrite ^/mygroup(.+)$ /index.php?action/mygroup/gid$1;

    31.                location ~ .*\.php?$
    32.                {
    33.                        include conf/enable_php5.conf;
    34.                }
    35.        }
    复制代 码
    注:blog采用功能强大的Supesite作为Blog站点: http://www.supesite.com/
    1.Blog如何在Nginx里实现静态化,具体设置见,上面的#supesite rewrite

    4.down.redocn.com
    [root@redocn vhosts]# vi down_redocn_com.conf
    1. limit_zone   one  $binary_remote_addr  10m;
    2. server
    3.        {
    4.                listen       80;
    5.                server_name  down.redocn.com;
    6.                index index.html index.htm index.php;
    7.                root   /data/www/wwwroot/down;
    8.                error_page 404 /index.php;
    9.                # redirect server error pages to the static page /50x.html
    10.                error_page   500 502 503 504  /50x.html;
    11.                location = /50x.html {
    12.                 root   html;
    13.                   }
    14.                #Zone limit
    15.                location / {
    16.                    limit_conn   one  1;
    17.                    limit_rate  20k;
    18.                }

    19.                
    20.                # serve static files
    21.                location ~ ^/(images|javascript|js|css|flash|media|static)/  {
    22.                root    /data/www/wwwroot/down;
    23.                expires 30d;
    24.                 }
    25.        }
    复制 代码
    注:
    由于现在的BT下载软件越来越多了,我们如何限制下载的并发数和速率呢?apache需要三方模块,nginx就不用了
    在nginx利用两个指令即可实现:limit_zone(limit_conn) 来限制并发数,limit_rate来限制下载的速率,请看上面的配置实例.

    5.启动nginx服务
    1. /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
    2. /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
  • 相关阅读:
    delphi利用文件内存共享的简单小说阅读器
    Delphi中共享内存学习
    JavaWeb初学者配置数据库连接报错(此处是MSSQL)
    Delphi的Json学习之一
    Delphi记录类型指针的使用
    汇编——段寄存器
    SQL 标量函数-----日期函数 day() 、month()、year() 转载
    项目一总结 滚动监听
    一阶段项目总结 导航栏 滚动监听固定
    简单的 js手写轮播图
  • 原文地址:https://www.cnblogs.com/showker/p/1715637.html
Copyright © 2020-2023  润新知