失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 强大的负载均衡+静态文件WEB服务器nginx实战

强大的负载均衡+静态文件WEB服务器nginx实战

时间:2018-10-07 08:04:06

相关推荐

强大的负载均衡+静态文件WEB服务器nginx实战

独角兽企业重金招聘Python工程师标准>>>

当前比较流行的负载均衡前端服务器主要有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 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

中文维基地址:/NginxChs

模块依赖:

1 gzip支持,需要zlib / 下载最新版即可

2 rewrite module requires pcre library / 下载最新版即可

3 ssl 功能需要 openssl 库 / => /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

/09/17 15:26:55 [info] 15879#0: the configuration file conf/nginx.conf syntax is ok

/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的常用配置:

静态文件用nginx直接serve:

Nginx代码

#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_logoff;

expires30d;

}

location~*^.+\.(css|js|txt|xml|swf|wav)${

root/var/www/poseidon/root/static;

access_logoff;

expires24h;

}

#注:location不包括?后面带的参数,所以以上正则可以匹配http://192.168.1.16/image/sxxx.jpg?a=xxx

打开gzip,压缩传输

Nginx代码

gzipon;

gzip_comp_level7;

gzip_min_length1100;#需要压缩的最小长度

gzip_buffers48k;

gzip_typestext/plainapplication/javascripttext/csstext/xmlapplication/x-httpd-php;#指定需要压缩的文件类型

output_buffers132k;

postpone_output1460;

查看nginx的状态

Nginx代码

#设定查看Nginx状态的地址(非默认安装模块,需要在编译时加上--with-http_stub_status_module)

location/NginxStatus{

stub_statuson;

access_logon;

auth_basic"NginxStatus";

auth_basic_user_file/var/www/poseidon/root/passwd;

}

使用nginx的rewrite模块

Nginx代码

#强大的rewrite模块:

#文档:/NginxHttpRewriteModule

#经典示例:rewriteshttp://www.mydomain.nl/foo=>http://mydomain.nl/foo

if($host~*www\.(.*)){

set$host_without_www$1;

rewrite^(.*)$http://$host_without_www$1permanent;#$1contains'/foo',not'www.mydomain.nl/foo'

}

#我们的应用:rewrites所有非的访问=>/xxx

if($host!=""){

rewrite^(.*)$$1permanent;

}

最常见的nginx+fastcgi+php的使用

Shell代码

#nginx+fastcgi+php-cgi套路:

wgetlighttpd1.4.19(orlater)

wgetphp5.2.6(orlater)

./configure--prefix=/usr/local/lighttpd

make&makeinstall

./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&maketest&makeinstall(php.ini的默认读取位置为[prefix]/lib)

cpphp.ini-dist/usr/local/php-5.2.6/lib/php.ini

/usr/local/nginx/sbin/spawn-fcgi-a127.0.0.1-p10005-unobody-gnobody-f/usr/local/php-5.2.6/bin/php-cgi-P/var/run/fastcgi.pid-C15

#修改nginx的配置文件,使用fastcgi_passhttp://127.0.0.1:10005作为后端

kill-HUP`cat/var/run/nginx.pid`#重启nginx

nginx+fastcgi+catalyst(for perl users):

Shell代码

#Catalyst自带文档:

#http://dev./wiki//gettingstarted/howtos/deploy/lighttpd_fastcgi.view?rev=22

#以上文档介绍的是lighttpd和catalyst的结合,本质是一样的

#实际上也就是用自动生成的script/[myapp]_fastcgi.pl来启动,剩下的事,就随意啦(只是用什么来做前端而已)

#首先安装FCGI模块

cpan

installFCGI

installFCGI::ProcManager

cd/var/www/project/script

chmod755project_fastcgi.pl

./project_fastcgi.pl-listen127.0.0.1:3003-nproc10-pidfile/var/run/fcgi_catalyst.pid-daemon

#nginx中,配置:

location/{

fastcgi_pass127.0.0.1:3003;

include/var/www/project/root/fastcgi.conf;

}

#fastcgi.conf详细(注意点:将SCRIPT_NAME替换成PATH_INFO即可)

fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;

fastcgi_paramQUERY_STRING$query_string;

fastcgi_paramREQUEST_METHOD$request_method;

fastcgi_paramCONTENT_TYPE$content_type;

fastcgi_paramCONTENT_LENGTH$content_length;

#fastcgi_paramSCRIPT_NAME$fastcgi_script_name;

fastcgi_paramPATH_INFO$fastcgi_script_name;

fastcgi_paramREQUEST_URI$request_uri;

fastcgi_paramDOCUMENT_URI$document_uri;

fastcgi_paramDOCUMENT_ROOT$document_root;

fastcgi_paramSERVER_PROTOCOL$server_protocol;

fastcgi_paramGATEWAY_INTERFACECGI/1.1;

fastcgi_paramSERVER_SOFTWAREnginx/$nginx_version;

fastcgi_paramREMOTE_ADDR$remote_addr;

fastcgi_paramREMOTE_PORT$remote_port;

fastcgi_paramSERVER_ADDR$server_addr;

fastcgi_paramSERVER_PORT$server_port;

fastcgi_paramSERVER_NAME$server_name;

最后,因为nginx没有方便的控制命令可用,经常要ps,kill等直接控制,比较麻烦,可以为它写一个启动脚本,例子如下:

Shell代码

#!/bin/sh

#

#description:Starts,stopsnginx

#

#chkconfig:23452080

#dscription:StartupscriptfornginxwebserveronCentOS.Placein/etc/init.d

#

#Author:Touya

set-e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DESC="nginxdaemon"

NAME=nginx

DAEMON=/usr/local/nginx/sbin/$NAME

CONFIGFILE=/var/www/poseidon/root/nginx.conf

PIDFILE=/var/run/$NAME.pid

SCRIPTNAME=/etc/init.d/$NAME

#Gracefullyexitifthepackagehasbeenremoved.

test-x$DAEMON||exit0

d_start(){

echo"Starting$DESC:$NAME"

$DAEMON-c$CONFIGFILE||echo"alreadyrunning"

}

d_stop(){

echo"Stopping$DESC:$NAME"

test-f$PIDFILE&&kill-QUIT`cat$PIDFILE`

}

d_reload(){

echo"Reloading$DESCconfiguration…"

kill-HUP`cat$PIDFILE`||echo"can’treload"

}

case"$1"in

'start')

d_start

echo"started."

;;

'stop')

d_stop

echo"stoped."

;;

'reload')

d_reload

echo"reloaded."

;;

'restart')

echo"Restarting$DESC:$NAME..."

d_stop

#Onesecondmightnotbetimeenoughforadaemontostop,

#ifthishappens,d_startwillfail(anddpkgwillbreakif

#thepackageisbeingupgraded).Changethetimeoutifneeded

#be,orchanged_stoptohavestart-stop-daemonuse--retry.

#Noticethatusing--retryslowsdowntheshutdownprocesssomewhat.

sleep3

d_start

echo"done."

;;

'list')

psauxf|egrep'(PID|nginx)'|grep-vgrep

;;

'test')

$DAEMON-t-c$CONFIGFILE

;;

*)

echo"Usage:$SCRIPTNAME{reload|list|test|start|stop|restart}">&2

exit3

;;

esac

exit0

保存文件,并chmod 755 /etc/init.d/nginx

用chkconfig --list nginx查看是否是一个可用后台启动服务,如果是的话,可以直接执行chkconfig --add nginx,这个后台服务搞定(代码中不可省略:#chkconfig: 2345 20 80)

接下可以用service nginx start|restart|stop来操作你的nginx服务器(restart时重新读入config)

怎么样?是不是方便多了?

小结:本文是我自己实践nginx的整个经验总结,包括了前期准备、安装、配置、架构设计、和现有动态程序结合(公司使用的是Catalyst)、启动脚本等等,希望对大家有帮助,少走歪路。

如果觉得《强大的负载均衡+静态文件WEB服务器nginx实战》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。