当前位置: 主页 > 服务器技术 > Web服务器 > 用Nginx替代apache实现高性能的Web环境

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

时间:2009-11-16来源:互联网 点击:

复制代码四,多虚拟主机应用配置案例.

#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\=$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分区满了,
清了一大堆无用文件后,马上就正常了.

站长资讯网
.
分页: [1] [2]
TAG: APACHE NGINX WEB 高性能 环境
推荐内容最近更新人气排行
关于我们 | 友情链接 | 网址推荐 | 常用资讯 | 网站地图 | RSS | 留言