在Linux操作系统下,nginx使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或FreeBSD操作系统上采用类似于epoll的高效事件模型kqueue。nginx同时是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
现在主要是做个nginx负载均衡简单小实验,也可以给大家做下参考。
1.实验环境
nginx负载均衡位置:192.168.1.111 80端口
WEB_1:192.168.1.109 80端口
WEB_2:192.168.1.110 8080端口
WEB_3:192.168.1.111 8080端口
在192.168.1.111上安装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 http://nginx.org/download/nginx-1.2.8.tar.gz
tar -zxvf nginx-1.2.8.tar.gz
cd nginx-1.2.8
./configure --prefix=/usr/local/nginx --with-pcre=/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.1.109:80 weight=5;
server 192.168.1.110:8080 weight=5;
server 192.168.1.111:8080 weight=2;
}
server { #这个是设置web服务的,监听8080端口
listen 8080;
server_name 192.168.1.111;
index index.html index.htm;
root /var/www/html;
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}
}
#设定虚拟主机 #这个是设置负载均衡的,监听80端口
server {
listen 80;
server_name 192.168.1.111;
#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 http://mysvr; #以这种格式来使用后端的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
默认的配置文件就在conf/nginx.conf,所以,如果你要把配置文件放在别的地方,就加上个-c /path/nginx.conf。启动好后,访问http://192.168.1.111就可以按算法的分配来访问后台的三个web服务器了。
访问http://192.168.1.111/NginxStatus,然后输入用户名和密码就可以查看nginx的一些记录信息。
Active connections: 1
server accepts handled requests
19 19 91
Reading: 0 Writing: 1 Waiting: 0
5.nginx负载均衡的最简化模型
worker_processes 1;
events {
worker_connections 1024;
}
http{
upstream myproject {
#这里指定多个源服务器,ip:端口,80端口的话可写可不写
server 192.168.1.109:80;
server 192.168.1.110:8080;
}
server {
listen 8080;
location / {
proxy_pass http://myproject;
}
}
}
原文地址:http://blog.csdn.net/xifeijian/article/details/21043309