日期:2012-04-03 01:39:00  来源:本站整理

强盛的负载均衡+静态文件WEB服务器nginx实战[服务器安全]

赞助商链接



  本文“强盛的负载均衡+静态文件WEB服务器nginx实战[服务器安全]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

当前对比风行的负载均衡前端服务器主要有apache(with mod_proxy),nginx,lighttpd,squid,perlbal,pound,大概假如你的域名服务商供应DNS级别的负载均衡,也可 以(就是一个域名随机指向多个IP,定制性不高).

从前自己常用pound作为前端,它专注于负载均衡,支持https协议,配置还算简单,不过渐渐发现功效不够强盛,转而研究其他一些既可以做负载均衡, 又能做web服务器的高性能工具吧.Perlbal是第一个看的,大牛Danga的杰作,它们开辟的memcached(分布式内存cache系统)非常 好用,Perlbal也不差,固然是基于Perl的,速度上比纯C开辟的大概稍逊,但不得不说Danga大牛实力不凡.不过公司的机械都是 perl5.8.5,而Perlbal必须perl5.8.8以上,进级大概有兼容性问题,故只能作罢.

转而研究nginx:Nginx (”engine X”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯拜候量第二的 Rambler.ru 站点开辟的,它已经在该站点运行超越两年半了.Igor 将源代码以类BSD答应证的情势公布.固然还是测试版,但是,Nginx 已经因为它的安定性、丰富的功效集、示例配置文件和低系统资源的损耗而闻名了.

中文维基地址:http://wiki.codemongers.com/NginxChs

模块依靠:
1 gzip支持,需求zlib http://www.zlib.net/ 下载最新版便可
2 rewrite module requires pcre library http://www.pcre.org/ 下载最新版便可
3 ssl 功效需求 openssl 库 http://www.openssl.org/ => http://www.openssl.org/source/ LASTEST版本便可

安装历程:

#下载以上source到/usr/local/src/nginx/目录下,解压,则该目录下情形以下:
[root@s16 nginx]# ls
nginx-0.6.32 nginx-0.6.32.tar.gz openssl-0.9.8i openssl-0.9.8i.tar.gz pcre-7.8 pcre-7.8.tar.gz zlib-1.2.3 zlib-1.2.3.tar.gz

cd nginx-0.6.32
./configure –with-pcre=../pcre-7.8 –with-zlib=../zlib-1.2.3 –with-openssl=../openssl-0.9.8i
make
make install

#OK,安装完成
#改正配置:
cd /usr/local/nginx
vi conf/nginx.conf
#比方,去掉例子中的8000端口的服务器配置的注释
sbin/nginx -t -c conf/nginx.conf (测试配置文件能否精确)
[root@s16 nginx]# sbin/nginx -t -c conf/nginx.conf
2008/09/17 15:26:55 [info] 15879#0: the configuration file conf/nginx.conf syntax is ok
2008/09/17 15:26:55 [info] 15879#0: the configuration file conf/nginx.conf was tested successfully

sbin/nginx (启动)
ps aux | grep nginx | grep -v grep (查看能否正常启动了)
#假如没有正常启动,查看errorlog,默许位置:/usr/local/nginx/logs/error.log

#经过apache bench测试,nginx在serve静态文件方面性能不比apache(with mod_perl)好多少,基本上,以65K为分界点,小文件时nginx性能好(最高可以到达3倍左右速度),大文件时apache性能好(不过差别有 限),所以纯从速度上来说,nginx并不比apache强,不过nginx玲珑,损耗资源少,假如你有很多静态小文件需求serve,的确是个不错的选 择哦.

这里举荐一种架构:

1 前端nginx,并serve静态文件,如图片,js,css等,nginx是支持gzip紧缩的

2 后端动态程序用fastcgi(lighttpd的spawn_fcgi便可),可以支持php,perl等多种脚本语言了

下面介绍一下nginx的常用配置:

1. 静态文件用nginx直接serve:

Nginx代码

1. #css|js|ico|gif|jpg|jpeg|png|txt|html|htm|xml|swf|wav这些都是静态文件,但应辨别,js、css大概常常会变,过期时间应小一些,图片、html基本不变,过期时间可以设长一些
2. location ~* ^.+\.(ico|gif|jpg|jpeg|png|html|htm)$ {
3. root /var/www/poseidon/root/static;
4. access_log off;
5. expires 30d;
6. }
7. location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
8. root /var/www/poseidon/root/static;
9. access_log off;
10. expires 24h;
11. }
12. #注:location不包含?背面带的参数,所以以上正则可以匹配http://192.168.1.16/image/sxxx.jpg?a=xxx

#css|js|ico|gif|jpg|jpeg|png|txt|html|htm|xml|swf|wav这些都是静态文件,但应辨别,js、css大概常常会变,过期时间应小一些,图片、html基本不变,过期时间可以设长一些
location ~* ^.+\.(ico|gif|jpg|jpeg|png|html|htm)$ {
root /var/www/poseidon/root/static;
access_log off;
expires 30d;
}
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
root /var/www/poseidon/root/static;
access_log off;
expires 24h;
}
#注:location不包含?背面带的参数,所以以上正则可以匹配http://192.168.1.16/image/sxxx.jpg?a=xxx

1. 翻开gzip,紧缩传输
Nginx代码
1. gzip on;
2. gzip_comp_level 7;
3. gzip_min_length 1100; #需求紧缩的最小长度
4. gzip_buffers 4 8k;
5. gzip_types text/plain application/javascript text/css text/xml application/x-httpd-php; #指定需求紧缩的文件范例
6. output_buffers 1 32k;
7. postpone_output 1460;

gzip on;
gzip_comp_level 7;
gzip_min_length 1100; #需求紧缩的最小长度
gzip_buffers 4 8k;
gzip_types text/plain application/javascript text/css text/xml application/x-httpd-php; #指定需求紧缩的文件范例
output_buffers 1 32k;
postpone_output 1460;

1. 查看nginx的状况
Nginx代码
1. #设定查看Nginx状况的地址(非默许安装模块,需求在编译时加上–with-http_stub_status_module)
2. location /NginxStatus {
3. stub_status on;
4. access_log on;
5. auth_basic “NginxStatus”;
6. auth_basic_user_file /var/www/poseidon/root/passwd;
7. }

#设定查看Nginx状况的地址(非默许安装模块,需求在编译时加上–with-http_stub_status_module)
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus”;
auth_basic_user_file /var/www/poseidon/root/passwd;
}

2. 利用nginx的rewrite模块
Nginx代码
1. #强盛的rewrite模块:
2. #文档:http://wiki.codemongers.com/NginxHttpRewriteModule
3. #经典示例:rewrites http://www.mydomain.nl/foo => http://mydomain.nl/foo
4. if ($host ~* www\.(.*)) {
5. set $host_without_www $1;
6. rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains ‘/foo’, not ‘www.mydomain.nl/foo’
7. }
8.
9. #我们的利用:rewrites 全部非www.popovivi.com的拜候 => http://www.popovivi.com/xxx
10. if ($host != “www.popovivi.com”) {
11. rewrite ^(.*)$ http://www.popovivi.com$1 permanent;
12. }

#强盛的rewrite模块:
#文档:http://wiki.codemongers.com/NginxHttpRewriteModule
#经典示例:rewrites http://www.mydomain.nl/foo => http://mydomain.nl/foo
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains ‘/foo’, not ‘www.mydomain.nl/foo’
}

#我们的利用:rewrites 全部非www.popovivi.com的拜候 => http://www.popovivi.com/xxx
if ($host != “www.popovivi.com”) {
rewrite ^(.*)$ http://www.popovivi.com$1 permanent;
}

3. 最常见的nginx+fastcgi+php的利用
Shell代码
1. #nginx+fastcgi+php-cgi套路:
2. wget lighttpd1.4.19(or later)
3. wget php5.2.6(or later)
4. ./configure –prefix=/usr/local/lighttpd
5. make & make install
6. ./configure –prefix=/usr/local/php-5.2.6 –enable-fastcgi –enable-sockets –enable-force-cgi-redirect –with-gd –enable-mbstring –with-zlib –with-mysql –with-gettext –with-mcrypt –with-mime-magic –with-openssl
7. make & make test & make install(php.ini的默许读取位置为[prefix]/lib)
8. cp php.ini-dist /usr/local/php-5.2.6/lib/php.ini
9. /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 10005 -u nobody -g nobody -f /usr/local/php-5.2.6/bin/php-cgi -P /var/run/fastcgi.pid -C 15
10.
11. #改正nginx的配置文件,利用fastcgi_pass http://127.0.0.1:10005作为后端
12. kill -HUP `cat /var/run/nginx.pid` #重启nginx

#nginx+fastcgi+php-cgi套路:
wget lighttpd1.4.19(or later)
wget php5.2.6(or later)
./configure –prefix=/usr/local/lighttpd
make & make install
./configure –prefix=/usr/local/php-5.2.6 –enable-fastcgi –enable-sockets –enable-force-cgi-redirect –with-gd –enable-mbstring –with-zlib –with-mysql –with-gettext –with-mcrypt –with-mime-magic –with-openssl
make & make test & make install(php.ini的默许读取位置为[prefix]/lib)
cp php.ini-dist /usr/local/php-5.2.6/lib/php.ini
/usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 10005 -u nobody -g nobody -f /usr/local/php-5.2.6/bin/php-cgi -P /var/run/fastcgi.pid -C 15

#改正nginx的配置文件,利用fastcgi_pass http://127.0.0.1:10005作为后端
kill -HUP `cat /var/run/nginx.pid` #重启nginx

4. nginx+fastcgi+catalyst(for perl users):
Shell代码
1. #Catalyst自带文档:
2. #http://dev.catalyst.perl.org/wiki//gettingstarted/howtos/deploy/lighttpd_fastcgi.view?rev=22
3. #以上文档介绍的是lighttpd和catalyst的结合,本质是一样的
4. #实际上也就是用自动生成的script/[myapp]_fastcgi.pl来启动,剩下的事,就随便啦(只是用什么来做前端罢了)
5. #首先安装FCGI模块
6. cpan
7. install FCGI
8. install FCGI::ProcManager
9.
10. cd /var/www/project/script
11. chmod 755 project_fastcgi.pl
12. ./project_fastcgi.pl -listen 127.0.0.1:3003 -nproc 10 -pidfile /var/run/fcgi_catalyst.pid -daemon
13.
14. #nginx中,配置:
15. location / {
16. fastcgi_pass 127.0.0.1:3003;
17. include /var/www/project/root/fastcgi.conf;
18. }
19. #fastcgi.conf具体(注意点:将SCRIPT_NAME替换成PATH_INFO便可)
20. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
21. fastcgi_param QUERY_STRING $query_string;
22. fastcgi_param REQUEST_METHOD $request_method;
23. fastcgi_param CONTENT_TYPE $content_type;
24. fastcgi_param CONTENT_LENGTH $content_length;
25. #fastcgi_param SCRIPT_NAME $fastcgi_script_name;
26. fastcgi_param PATH_INFO $fastcgi_script_nam
  以上是“强盛的负载均衡+静态文件WEB服务器nginx实战[服务器安全]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • 强盛的负载均衡+静态文件WEB服务器nginx实战
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .