技术员联盟提供win764位系统下载,win10,win7,xp,装机纯净版,64位旗舰版,绿色软件,免费软件下载基地!

当前位置:主页 > 教程 > 服务器类 >

Nginx负载均衡实战

来源:技术员联盟┆发布时间:2019-03-26 00:17┆点击:

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.8 nginx负载均衡位置: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/src 

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz 

tar -zxvf pcre-8.21.tar.gz 

cd pcre-8.21 

./configure 

make 

make install 

下载安装nginx

cd /usr/local/src 

wget  

tar -zxvf nginx-1.2.8.tar.gz 

cd nginx-1.2.8 

./configure --prefix=http://www.3lian.com/usr/local/nginx --with-pcre=http://www.3lian.com/usr/local/src/pcre-8.21 --user=nginx --group=nginx --with-http_stub_status_module 

make 

make install 

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

3.自定义nginx配置文件

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

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

内容如下:

#运行用户 

user nginx nginx; 

#启动进程 

worker_processes 2; 

#全局错误日志及PID文件 

error_log logs/error.log notice; 

pid logs/nginx.pid; 

#工作模式及每个进程连接数上限 

events { 

use epoll; 

worker_connections 1024;     #所以nginx支持的总连接数就等于worker_processes * worker_connections 

#设定http服务器,利用它的反向代理功能提供负载均衡支持 

http { 

#设定mime类型 

include mime.types;  #这个是说nginx支持哪些多媒体类型,可以到conf/mime.types查看支持哪些多媒体 

default_type application/octet-stream;   #默认的数据类型 

#设定日志格式 

log_format main '$remote_addr - $remote_user [$time_local] ' 

'"$request" $status $bytes_sent ' 

'"$http_referer" "$http_user_agent" ' 

'"$gzip_ratio"'; 

log_format download '$remote_addr - $remote_user [$time_local] ' 

'"$request" $status $bytes_sent ' 

'"$http_referer" "$http_user_agent" ' 

'"$http_range" "$sent_http_content_range"'; 

#设定请求缓冲 

client_header_buffer_size 1k; 

large_client_header_buffers 4 4k; 

#开启gzip模块 

#gzip on; 

#gzip_min_length 1100; 

#gzip_buffers 4 8k; 

#gzip_types text/plain; 

#output_buffers 1 32k; 

#postpone_output 1460; 

#设定access log 

access_log logs/access.log main; 

client_header_timeout 3m; 

client_body_timeout 3m; 

send_timeout 3m; 

sendfile on; 

tcp_nopush on; 

tcp_nodelay on; 

keepalive_timeout 65; 

#设定负载均衡的服务器列表 

upstream mysvr { 

#weigth参数表示权值,权值越高被分配到的几率越大 

server 192.168.207.129:80 weight=5; 

server 192.168.207.130:8080 weight=5; 

server 192.168.207.131:8080 weight=2; 

server { #这个是设置web服务的,监听8080端口 

listen        8080; 

server_name    192.168.207.131; 

index     index.html index.htm; 

root        /var/www/html; 

#error_page     500 502 503 504    /50x.html; 

#location = /50x.html { 

#    root     html; 

#} 

#设定虚拟主机 

server { 

listen 80; 

server_name 192.168.207.131; 

#charset gb2312; 

#设定本虚拟主机的访问日志 

access_log logs/three.web.access.log main; 

#如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid 

#如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好 

#location ~ ^/(img|js|css)/{ 

#   root /data3/Html; 

#   expires 24h; 

#} 

#对 "/" 启用负载均衡 

location / { 

proxy_pass ;  #以这种格式来使用后端的web服务器 

proxy_redirect off; 

proxy_set_header Host $host; 

proxy_set_header X-Real-IP $remote_addr; 

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

client_max_body_size 10m; 

client_body_buffer_size 128k; 

proxy_connect_timeout 90; 

proxy_send_timeout 90; 

proxy_read_timeout 90; 

proxy_buffer_size 4k; 

proxy_buffers 4 32k; 

proxy_busy_buffers_size 64k; 

proxy_temp_file_write_size 64k; 

#设定查看Nginx状态的地址 ,在安装时要加上--with-http_stub_status_module参数 

location /NginxStatus { 

stub_status on; 

access_log on; 

auth_basic "NginxStatus"; 

auth_basic_user_file conf/htpasswd;     #设置访问密码,htpasswd -bc filename username password 

 } 

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

先添加个nginx用户:

useradd nginx 

要不然会报错的:

/usr/local/nginx/sbin/nginx