Chinaunix首页 | 论坛 | 博客
  • 博客访问: 728764
  • 博文数量: 256
  • 博客积分: 3502
  • 博客等级: 中校
  • 技术积分: 3988
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-17 21:13
文章分类

全部博文(256)

文章存档

2014年(11)

2013年(134)

2012年(111)

我的朋友

分类: 系统运维

2013-05-13 11:16:36

Nginx先容 :
Nginx发音为[engine x],是由俄罗斯人Igor Sysoev成立 的项目,基于BSD答应。
听说 他当初是F5的成员之一,英文主页:。俄罗斯的一些大网站已经应用 它高出 两年多了, 不停 示意特殊,信赖想了解 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去找干系 的贴子()

二.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
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库:
   复制内容到剪贴板 代码:    tar zxvf pcre-7.2.tar.gz
   cd pcre-7.2/
   ./configure
   make && make install
   cd ../

  
   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 //以下是加载假造 主机设置 .
   #[url][/url]
   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.conf 复制内容到剪贴板 代码: fastcgi_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.                         //首站设置
[root@redocn vhosts]#vi www_redocn_com.conf 复制内容到剪贴板 代码: server
   {
            listen    80;
            server_name   ;
            index index.html index.htm index.php;
            root   /data/www/wwwroot;
            error_page 404
            rewrite ^/bbs/(.*)
            location ~ .*\.php?$
            {
                      include conf/enable_php5.conf;
            }
   }
注: 关于rewite需求,红动中国渴望 当用户拜访的时间 主动 转至
在原apache中操作redirect实现  
Redirect /bbs

本文中在nginx下操作rewrite实现:
rewrite ^/bbs/(.*) $1;


2.[root@redocn vhosts] vi bbs_redocn_com.conf 复制内容到剪贴板 代码: server
   {
            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:///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:///images/redocn.gif;   //让别人盗链时表现 你指定的图片.
            #return 403;
                                 }
3.blog.redocn.com
[root@redocn vhosts]#vi blog_redocn_com.conf 复制内容到剪贴板 代码: server
   {
            listen    80;
            server_name   blog.redocn.com;
            index index.html index.htm index.php;
            root   /data/www/wwwroot/blog;
            error_page 404
            #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站点:
1.Blog如安在 Nginx里实现静态化,具体 设置见,上面的#supesite rewrite

4.down.redocn.com
[root@redocn vhosts]# vi down_redocn_com.conf 复制内容到剪贴板 代码: limit_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
你可以把上面两条下令 制做成体系 启动做事脚本,干系 的脚本在网上也很多 ,本文就不再贴出来了,给出一个实例链接:


五.题目 及履历 总结:
1.安装Discuz论坛后,无法上传大于M以上的附件?
在主设置 文件里介入:client_max_body_size    10m; 具体 指令阐发 请拜见 (六)供给 的Wiki链接.

2.Discuz附件无法下载附件?
近来 碰到 一个稀疏 的题目 在nginx下discuz论坛无法下载附件,其后打开error_log才知道,仍后一看/usr/local分区满了,
清了一大堆无用文件后,立刻 就正常了.
阅读(495) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~