首页 > 学院 > 网络通信 > 正文

Nginx负载均衡实战

2019-11-03 23:48:37
字体:
来源:转载
供稿:网友

Nginx是一款面向性能设计的HTTP服务器,相较于Apache、lighttpd具有占有内存少,稳定性高等优势。与旧版本(<=2.2)的Apache不同,nginx不采用每客户机一线程的设计模型,而是充分使用异步逻辑,削减了上下文调度开销,所以并发服务能力更强。整体采用模块化设计,有丰富的模块库和第三方模块库,配置灵活。 在linux操作系统下,nginx使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或FreeBSD操作系统上采用类似于epoll的高效事件模型kqueue。nginx同时是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

想仔细了解nginx的朋友,给两个地址给你们,一个是张宴的blog,他是中国较早研究nginx的人,还出了一个本nginx的书,讲的很具体,叫《实战nginx:取代Apache的高性能服务器》,另一个是51的nginx专题。

而今天我的主题呢,主要是nginx负载均衡实验,把做的步骤记录下来,作为一个学习笔记吧,也可以给大家做下参考。

1.实验环境

系统版本:CentOS release 5.9 (Final) x86 32位nginx版本:   1.2.8nginx负载均衡位置:192.168.207.131 80端口WEB_1:192.168.207.129 80端口WEB_2:192.168.207.130 8080端口WEB_3:192.168.207.131 8080端口

这里呢,我在web_1和web_2上使用的是系统自带的apache,按要求改变一下监听端口就ok了,当然也可以安装nginx,这个你自己看着办吧,我在192.168.207.131上安装nginx,作为负载均衡器和web服务器使用,负载均衡使用的端口是80,而web服务使用的是8080端口。

2.下载和安装nginx

安装nginx前需要先安装pcre库,PCRE(Perl Compatible Regular ExPRessions)是一个Perl库,包括 perl 兼容的正规表达式库,这个就是为之后的地址重新,location匹配啊等,让nginx支持正则:

    cd/usr/local/srcwgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gztar-zxvfpcre-8.21.tar.gzcdpcre-8.21./configuremakemakeinstall

下载安装nginx

    cd/usr/local/srcwgethttp://nginx.org/download/nginx-1.2.8.tar.gztar-zxvfnginx-1.2.8.tar.gzcdnginx-1.2.8./configure--prefix=/usr/local/nginx--with-pcre=/usr/local/src/pcre-8.21--user=nginx--group=nginx--with-http_stub_status_modulemakemakeinstall

注意--with-pcre指向的pcre的源码路径,如果要安装zlib的话也是这样,添加个--with-zlib,后面加个源码路径。

3.自定义nginx配置文件

我这里呢,配置文件的参数就多写点,让大家多了解一下nginx的参数:

    vi/usr/local/nginx/conf/nginx.conf

内容如下:

    #运行用户usernginxnginx;#启动进程worker_processes2;#全局错误日志及PID文件error_loglogs/error.lognotice;pidlogs/nginx.pid;#工作模式及每个进程连接数上限events{useepoll;worker_connections1024;#所以nginx支持的总连接数就等于worker_processes*worker_connections}#设定http服务器,利用它的反向代理功能提供负载均衡支持http{#设定mime类型includemime.types;#这个是说nginx支持哪些多媒体类型,可以到conf/mime.types查看支持哪些多媒体default_typeapplication/octet-stream;#默认的数据类型#设定日志格式log_formatmain'$remote_addr-$remote_user[$time_local]''"$request"$status$bytes_sent''"$http_referer""$http_user_agent"''"$gzip_ratio"';log_formatdownload'$remote_addr-$remote_user[$time_local]''"$request"$status$bytes_sent''"$http_referer""$http_user_agent"''"$http_range""$sent_http_content_range"';#设定请求缓冲client_header_buffer_size1k;large_client_header_buffers44k;#开启gzip模块#gzipon;#gzip_min_length1100;#gzip_buffers48k;#gzip_typestext/plain;#output_buffers132k;#postpone_output1460;#设定accesslogaccess_loglogs/access.logmain;client_header_timeout3m;client_body_timeout3m;send_timeout3m;sendfileon;tcp_nopushon;tcp_nodelayon;keepalive_timeout65;#设定负载均衡的服务器列表upstreammysvr{#weigth参数表示权值,权值越高被分配到的几率越大server192.168.207.129:80weight=5;server192.168.207.130:8080weight=5;server192.168.207.131:8080weight=2;}server{#这个是设置web服务的,监听8080端口listen8080;server_name192.168.207.131;indexindex.htmlindex.htm;root/var/www/html;#error_page500502503504/50x.html;#location=/50x.html{#roothtml;#}}#设定虚拟主机server{listen80;server_name192.168.207.131;#charsetgb2312;#设定本虚拟主机的访问日志access_loglogs/three.web.access.logmain;#如果访问/img/*,/js/*,/CSS/*资源,则直接取本地文件,不通过squid#如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好#location~^/(img|js|css)/{#root/data3/Html;#expires24h;#}#对"/"启用负载均衡location/{proxy_passhttp://mysvr;#以这种格式来使用后端的web服务器proxy_redirectoff;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;client_max_body_size10m;client_body_buffer_size128k;proxy_connect_timeout90;proxy_send_timeout90;proxy_read_timeout90;proxy_buffer_size4k;proxy_buffers432k;proxy_busy_buffers_size64k;proxy_temp_file_write_size64k;}#设定查看Nginx状态的地址,在安装时要加上--with-http_stub_status_module参数location/NginxStatus{stub_statuson;access_logon;auth_basic"NginxStatus";auth_basic_user_fileconf/htpasswd;#设置访问密码,htpasswd-bcfilenameusernamepassWord} }}

4.启动所以服务器,查看效果

先添加个nginx用户:

    useraddnginx

要不然会报错的:

    /usr/local/nginx/sbin/nginx

默认的配置文件就在conf/nginx.conf,所以啊,如果你要把配置文件放在别的地方,就加上个-c /path/nginx.conf。启动好了,访问http://192.168.207.131就可以按算法的分配来访问后台的三个web服务器了。

访问http://192.168.207.131/NginxStatus,然后输入用户名和密码就可以查看nginx的一些记录信息了,当然啦你可以使用其他的工具,比如说cacti,MRTG等工具。

    Activeconnections:1serveracceptshandledrequests191991Reading:0Writing:1Waiting:0

5.nginx负载均衡的最简化模型

    worker_processes1;events{worker_connections1024;}http{upstreammyproject{#这里指定多个源服务器,ip:端口,80端口的话可写可不写server192.168.43.158:80;server192.168.41.167;}server{listen8080;location/{proxy_passhttp://myproject;}}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表